SQL
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
);
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’
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;
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)
);
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’);
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’;
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
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 );
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;
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
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.
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.
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;
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