0% found this document useful (0 votes)
97 views18 pages

SQL

Uploaded by

Madhu Sudhan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
97 views18 pages

SQL

Uploaded by

Madhu Sudhan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 18

SQL

Q1. You are working with a database containing employee information. You want to find
employees who are either in the ‘Engineering’ department and have a salary greater than
$80,000 or employees in the ‘Sales’ department with a ‘Senior’ job title
a. SELECT * FROM employees WHERE (department = ‘Engineering’ AND salary >
80000) OR (department = ‘Sales AND job_title = ‘Senior’);
b. SELECT * FROM employees WHERE (department = ‘Engineering’ OR department =
‘Sales’) AND (salary > 80000 OR job_title = ‘Senior’);
c. SELECT * FROM employees WHERE (department = ‘Engineering’ AND salary >
80000) AND (department = ‘Sales’ AND job_title = ‘Senior’);
d. SELECT * FROM employees WHERE (department = ‘Engineering’ AND salary >
80000) XOR (department = ‘Sales’ AND job_title = ‘Senior’)

Q2. There is a Student Table that has two column IDs and names.
We need to update the name of the student with id=10 as ‘John’. Which of the following query
is correct?
a. UPDATE STUDENT SET NAME = “John” WHERE ID=10;
b. UPDATE NAME FROM STUDENT SET NAME = “John” WHERE ID=10;
c. UPDATE NAME = “John” FROM STUDENT WHERE ID = 10;
d. None
Q3. Assume that you work for a company that operates on e-commerce website. You are tasked
with updating the orders status for a particular customers orders. You are provided with the
given table schema.
CREATE TABLE customers (
id INT PRIMARY KEY,
name VARCHAR(50),
email VARCHAR(50) UNIQUE
);

CREATE TABLE orders (


id INT PRIMARY KEY,
customer_id INT,
status VARCHAR(20)
);
Which SQL statement can be used to update the status of all the orders for the customer with
the email “john@example.com” to shipped?
a. UPDATE orders
SET status = ‘shipped’
WHERE id IN (
SELECT id
FROM customers
WHERE email = ‘john@example.com’
);

b. UPDATE orders o
JOIN customers c ON o.customer_id = c.id
SET o.status = ‘shipped’
WHERE c.email = ‘john@example.com’

c. UPDATE orders
WHERE customer_id = (
SELECT id
FROM customers
WHERE email = ‘john@example.com’
)
SET status = ‘shipped’;

d. UPDATE orders o
SET status = 'shipped'
WHERE c.email = ‘john@example.com’
JOIN customers c ON o.customer_id = c.id;

Q4. Which SQL query retrieves information about the primary key columns of the customers
table?
a. SELECT CONSTRAINT_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE TABLE_NAME = ‘customers’ AND CONSTRAINT_TYPE =
‘PRIMARY’

b. SELECT CONSTRAINT_NAME, COLUMN_NAME


FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE REFERENCED_TABLE_NAME = ‘customers’ AND CONSTRAINT_TYPE
= ‘PRIMARY KEY’;

c. SELECT COLUMN_NAME, CONSTRAINT_NAME


FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_NAME_USAGE
WHERE TABLE_NAME = ‘customers’ AND CONSTRAINT TYPE=’PRIMARY
KEY’

d. SELECT COLUMN_NAME, CONSTRAINT_NAME


FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN _USAGE
WHERE REFERENCED_TABLE_NAME = ‘customers’ AND CONSTRAINT_TYPE
= ‘PRIMARY’;

Q5. Consider a scenario where a company posseses two tables the first one holds customers
details with columns such as customer_id, customer_name and email and the second one
contains ordered information with columns like order_id, customer_id, total_amount and
order_date.
Describe how you would formulate a sql query to combine these tables and obtain the total
amount spent by each cutomer.
a. SELECT customer.customer_id, SUM(total_amount) AS total_spent
FROM customer
INNER JOIN order on customer_id = order.customer_id
GROUP BY customer_id;

b. SELECT customer_id, SUM(order_amount) AS total_spent


FROM customer
INNER JOIN order on customer_id = order.customer_id
GROUP BY customer. customer_id;

c. SELECT customer .customer_id, SUM(order.total_amount) AS total_spent


FROM customer
INNER JOIN order on customer_id = order.customer_id
GROUP BY customer. customer_id;

Q6. Assume you are a database administrator for a company that is updating an existing SQL
database with new product data. The new data requires a change in the data type for the ‘price’
column in the ‘product’ table from decimal to an integer.
a. MODIFY ALTER COLUMN
b. ALTER COLUMN
c. CHANGE ALTER COLUMN
d. ALTER UPDATE COLUMN
Q7. You want to allow records to be inserted with a blank email ID field initially. However,
you need to ensure that this blank field is automatically populated with a random email ID
ending in ‘@gmail.com’ after insertion.
Which combination of constraints and mechanisms should you use to achieve this ?
a. CHECK
b. DEFAULT
c. NOT NULL
d. UNIQUE ‘@gmail.com’
Q8. Assume that your company has a database that stores information about employees and
their salaries. The HR department needs to update the salary of a specific employee who has
just received a raise.
Which DML command should the HR admin use to update the salary of the employee with
employee_id 101?
a. UPDATE employees SET salary = 7500 WHERE employe_id = 101;
b. DELETE FROM employees WHERE employee_id = 101;
c. SELECT * FROM employees WHERE employee_id = 101;
d. INSERT INTO employees (employee_id, first_name, last_name, salary) VALUES (101,
‘John’, ‘Doe’, 75000);

Q9. Assume that you are working as a database administrator for a company that has recently
implemented a new payroll system. As a part of this system, you need to create a new table
named “employees” that will store information about all the company’s employees. The
“employees” table should have the given columns.
emp_id (integer, auto-incremented, primary key)
first_name (string)
last_name (string)
job_title (string)
hire_date (date)
salary (decimal)
Which SQL statement can be used to create the “employees” table with the required columns?
Analyze the given choices and select the correct option.
a. CREATE TABLE employees (
emp_id SERIAL PRIMARY KEY,
first_name TEXT,
last_name TEXT,
job_title TEXT,
hire_date DATE,
salary NUMERIC(10, 2)
);

b. CREATE TABLE employees (


first_name NVARCHAR(255),
last_name NVARCHAR(255),
job_title NVARCHAR(255),
hire_date DATETIME,
salary MONEY
);

c. CREATE TABLE employees (


emp_id INT PRIMARY KEY AUTO_INCREMENT,
first_name VARCHAR(255),
last_name VARCHAR(255),
job_title VARCHAR(255),
hire_date DATE,
salary DECIMAL(10, 2)
);

d. CREATE TABLE employees (


emp_id INTEGER PRIMARY KEY,
first_name VARCHAR(255),
last_name VARCHAR(255),
job_title VARCHAR(255),
hire_date DATE,
salary DECIMAL(10, 2)
);

Q10. You have a table named ‘employees’ with the following columns:
‘employee_id’, ‘first_name’, last_name, ‘salary’, and department_id,
You are tasked with updating the salary of employees in the finance department to reflect a
10% salary increase.
Which SQL UPDATE statement accomplishes this task?
a. UPDATE employees SET salary = salary * 1.10 WHERE department_id = (SELECT
department_id FROM department_id FROM employees WHERE department_id =
‘Finance’);

b. UPDATE employees SET salary = salary * 1.10 WHERE department_id =


‘Finance’;

c. UPDATE employees SET salary = salary * 1.10 WHERE employee_id IN (SELECT


department_id FROM employees WHERE department_id = ‘Finance’);

d. UPDATE employees SET salary = salary * 1.10 WHERE department_id IN (SELECT


department_id FROM employees WHERE department_id = ‘Finance’ )

Q11. As a payroll manager for tech company with employees in diff. locations how can you use
SQL code to calculate the average_salary of employees in the IT department who are located
in a specific location for example ‘XYZ’?
Analyze the given choices and select the correct option.
a. SELECT AVG() AS average_salary
FROM payroll_table
Department = ‘IT’ AND location = ‘xyz’;

b. SELECT AVG(salary) AS average_salary


FROM payroll_table
WHERE department = ‘IT’ AND location = ‘xyz’;

c. SELECT AVG(salary) AS average__salary


FROM payroll__table__
WHERE department = ‘IT’ AND location = ‘xyz’;

Q12. Assume you are maintaining existing DB which is used to store customer data for retailing
company. New requirement has been added i.e. delete column that is no longer needed?
Ans: DROP COLUMN

Q13. Given a rank & display new row


Ans: SELECT *, rank() over(order by total.score) abc from student;
Q14. Nick assigned to task to isolated all student are between age 18 to 30 years. Table name is
student, with id, name, email, age fields.
Ans :(from pdf)
ALTER table student ADD constraint chk-age CHECK (age between 18 and 30);

Q15. Which of the following SQL statements defines a FOREIGN KEY constraint on the
DEPT NO column of the EMP table?
a. CREATE TABLE EMP (empno NUMBER (4), ename VARCHAR(35), deptno
NUMBER(7, 2) NOT NULL, CONSTRAINT emp_deptno_fk FOREIGN KEY deptno
REFERENCES dept deptno );

b. CREATE TABLE EMP (empno NUMBER (4), ename VARCHAR(35), deptno


NUMBER(7, 2) FOREIGN KEY REFERENCES dept deptno);

c. CREATE TABLE EMP (empno NUMBER (4), ename VARCHAR(35)) deptno


NUMBER(7,2) NOT NULL, CONSTRAINT emp_deptno_fk FOREIGN KEY
(deptno) REFERENCES dept (deptno));

d. CREATE TABLE EMP (empno NUMBER (4), ename VARCHAR(35), deptno


NUMBER(7,2) FOREIGN KEY CONSTRAINT emp deptno fk REFERENCES dept
(deptno));

Q16. Assume thet you have a database that contains information about a large number of
employees in a company .Each employee has a unique employee ID ,a first and last name and a
department ID that corresponds to the department they work in. Additionally ,there is table that
contains information about the departments ,including the department ID and the department
name .
You want to retrieve the first and the last names of the employees who work in departments
with more than 50 employees and also include the department name in the output .Which
query can you use will achieve this task?
a. SELECT e.first_name, e.last_name, d.department_name FROM employees e INNER
JOIN departments d ON e.department_id = d.department_id WHERE (SELECT
COUNT () FROM employees WHERE department_id =e.department_id) >50;

b. SELECT e.first _name, e.last_name, d.department_name FROM employees e INNER


JOIN departments d ON e.department_id =d.department_id WHERE COUNT () >50
GROUP BY e.first_name ,e.last_name, d.department_name ;

c. SELECT e.first _name, e.last_name, d.department_name FROM employees e INNER


JOIN departments d ON e.department_id =d.department_id HAVING COUNT () >50;

d. SELECT e.first _name, e.last_name, d.department_name FROM employees e


INNER JOIN departments d ON e.department_id = d.department_id WHERE
e.department _id IN (SELECT department_id FROM employee GROUP BY
department _id HAVING COUNT(*) > 50);

Q 17. Consider that you have a table called ‘Orders’ with the columns ‘OrderID’ ,
‘CustomerID’ , ‘OrderID’ and ‘Order Total’.
You need to write a query that will return all the orders placed in the year 2023 sorted by order
date in descending order.
Which query can you use to fullfill your requirement? Analyze the given choices and select the
correct option.
a. SELECT * FROM Orders WHERE YEAR(OrderDate) = 2023 ORDER BY OrderDate

b. SELECT * FROM Orders WHERE YEAR(OrderDate) = 2023 ORDER BY CustomerID


DESC
c. SELECT * FROM Orders WHERE YEAR(OrderDate) = 2023 ORDER BY
OrderDate DESC

d. SELECT * FROM Orders WHERE YEAR(OrderDate) = 2023 ORDER BY CostomerID


ASC

Q 18. Assume that you are asked to create a new table for the customer data of an online store.
The table should have columns for the customers name ,email address and phone number
Which SQL command should you use to create the table in this scenario?
a.CREATE
b.ALTER
c. INSERT
d. UPDATE

Q 20. Assume that you are working with the database that has a table called ‘sales_data’ with
the columns “customer_name”, “product_name”, “product_category” ,and “sales_amount”
.You want to create a view that shows the total sales for each product category. You also want
to create an index on the “product_category" column for performance optimization Given
here is the SQL query that you wrote.
CREATE VIEW total_sales_by_category AS
SELECT product_category , SUM (sales_amount) AS total_sales
FROM sales_data
GROUP BY product_category;
CREATE INDEX sales_by_category_idx ON sales_data(product_category);
Which option denotes the error in the SQL query that you wrote? Analyze the given choices
and select the correct option.

(Note : Recheck this)


a. The view should have been create with the name “total_sales_per_category”, not
“total_sales_per_category”.

b. The index should have been created on the “total_sales ” column not the “
product_category” column.

c. The index should have been created on the “total_sales_by_category” view not the
“sales_data” table.

d. There is no error in the SQL query.

Q21. Which of the following is the correct syntax to join two tables in a MS SQL Server
database?
a. Select * from Table_1 ON Table_2 INNER JOIN Table_1.Id = Table_2.Id;
b. Select * from Table_1 INNER JOIN Table_2 ON Table_1.Id = Table_2.Id;
c. Select * from Table_1 AND Table_2 INNER JOIN Table_1.Id = Table_2.Id;
d. None

SQL CODINGS
Q1: MySQL : Employee Data
Database Name to be used
EmployeeData
Problem Statement
Write a standard SQL query that will return the value of ‘EMPID’ and “EMPNAME” for those
employees who earn more than their managers. Additionally, the query will return the salary of
an employee as column name “EMPSALARY” and the salary of a manager as column name
“MANAGERSALARY”.
The rows should be returned in the increasing order of EMPID.
Column Name: EMPID, EMPNAME, EMPSALARY, MANAGERSALARY
Ans:
select E.EMPID, E.EMPNAME, E.SALARY AS EMPSALARY, M.SALARY AS
MANAGERSALARY
from TBLEMPLOYEE E join TBLEMPLOYEE M on E.MANAGERID = M.EMPID AND
E.SALARY > M.SALARY ORDER BY EMPID;

Q2. MySQL : Employee Data


Type of Database
MySQL
Database Name to be used
SocialData
Problem Statement
Write a SQL query to find the top 4 most active users on a social media platform, where activity
is defined as the maximum number of posts created.
The query should include a column called the user_id as userId, the username as userName, and
no_of_posts as topPost that indicates the user who created the maximum number of posts.
Return the output result based on topPost in descending order.
Table Description:
Users
Answer:
SELECT U.USER_ID AS userId, U.USERNAME AS userName, P.NO_OF_POST AS
topPost
FROM USERS U JOIN POSTS P ON U.USER_ID = P.USER_ID
ORDER BY P.NO_OF_POST DESC LIMIT 4;
Q3. MySQL : Employee with Highest Salary
Type of Database
MySQL
Database name to be used
CompanyDb
Problem Statement
Write a SQL query to retrieve a list of department_id, department_name and the first_name
& the last_name of the employee with their salary in each department. If a department has no
employee or salary information, it should still appear in the result with NULL values.
The rows should be returned in the increasing order of “department_id”.
Column Name : department_id, department_name, first_name, last_name, salary
Table Description
departments
Answer:
SELECT D.DEPARTMENT_ID, D.DEPARTMENT_NAME, A.FIRST_NAME,
A.LAST_NAME, A.SALARY
FROM DEPARTMENTS D LEFT JOIN
(SELECT E.FIRST_NAME, E.LAST_NAME, E.DEPARTMENT_ID, S.SALARY
FROM EMPLOYEES E JOIN SALARIES S ON E.EMPLOYEE_ID =
S.EMPLOYEE_ID) A
ON D.DEPARTMENT_ID = A.DEPARTMENT_ID
ORDER BY D.DEPARTMENT_ID
Q4. MySQL : Order placed by Customer
Database : Customerdb
Problem Statement :
Write a SQL query to find the customer who has placed more than two orders with a total
amount exceeding $500. Display the customer_id and the total number of orders placed by that
customer a total_orders.
The rows should be returned in the increasing order of “customer_id”
Column Name: customer_id, total_orders
Table Description :
Orders

Answer:
SELECT A.CUSTOMER_ID, B.TOTAL_ORDERS FROM
(SELECT CUSTOMER_ID, COUNT(ORDER_ID) AS TOTAL_ORDERS
FROM ORDERS WHERE TOTAL_AMOUNT > 500.00
GROUP BY CUSTOMER_ID) A JOIN
(
SELECT CUSTOMER_ID, COUNT(ORDER_ID) AS TOTAL_ORDERS
FROM ORDERS
GROUP BY CUSTOMER_ID
)B
ON A.CUSTOMER_ID = B.CUSTOMER_ID AND A.TOTAL_ORDERS>=2
ORDER BY A.CUSTOMER_ID;
Q5. MySQL : Course Fees

You might also like