0% found this document useful (0 votes)
4 views

SQL_Programming_Practice_Paper_Set_2

The document is a SQL programming practice paper consisting of various sections covering basics, DDL/DML, SELECT queries, JOINS, subqueries, aggregations, and practical challenges. Each section includes questions with corresponding SQL commands and explanations. The paper is designed to test and enhance SQL skills across different concepts and functionalities.

Uploaded by

Sanjay Chopra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

SQL_Programming_Practice_Paper_Set_2

The document is a SQL programming practice paper consisting of various sections covering basics, DDL/DML, SELECT queries, JOINS, subqueries, aggregations, and practical challenges. Each section includes questions with corresponding SQL commands and explanations. The paper is designed to test and enhance SQL skills across different concepts and functionalities.

Uploaded by

Sanjay Chopra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

SQL Programming Practice Paper – Set 2 (With Answers)

Section A: Basics & DDL/DML (10 Marks)


1. Q1. Define Primary Key vs Unique Key.

- Primary Key ensures each row is unique and cannot contain NULLs.
- Unique Key also ensures uniqueness but allows a single NULL value.

2. Q2. Create a `Departments` table.

CREATE TABLE Departments (


DeptID INT PRIMARY KEY,
DeptName VARCHAR(100),
Location VARCHAR(50)
);

3. Q3. Insert a record into `Departments`.

INSERT INTO Departments (DeptID, DeptName, Location)


VALUES (1, 'Finance', 'Mumbai');

4. Q4. Delete department where `DeptName = 'Finance'`.

DELETE FROM Departments WHERE DeptName = 'Finance';

5. Q5. Rename column `Location` to `City`.

ALTER TABLE Departments RENAME COLUMN Location TO City;

Section B: SELECT Queries & Filtering (10 Marks)


6. Q6. Get all employees from 'HR' department.

SELECT * FROM Employees WHERE Department = 'HR';

7. Q7. Find employees whose names end with 'a'.

SELECT * FROM Employees WHERE FirstName LIKE '%a';

8. Q8. List employees who joined in 2023.

SELECT * FROM Employees WHERE YEAR(JoinDate) = 2023;

9. Q9. Find employees not in 'Finance' department.

SELECT * FROM Employees WHERE Department <> 'Finance';

10. Q10. Show salary of employees in ascending order.


SELECT FirstName, LastName, Salary FROM Employees ORDER BY Salary ASC;

Section C: JOINS & Relationships (15 Marks)


11. Q11. Get list of employees along with their department name.

SELECT E.FirstName, D.DeptName FROM Employees E


JOIN Departments D ON E.DeptID = D.DeptID;

12. Q12. Fetch employees with or without departments.

SELECT E.FirstName, D.DeptName FROM Employees E


LEFT JOIN Departments D ON E.DeptID = D.DeptID;

13. Q13. List departments without any employees.

SELECT D.DeptName FROM Departments D


LEFT JOIN Employees E ON D.DeptID = E.DeptID
WHERE E.EmpID IS NULL;

14. Q14. Count employees in each city.

SELECT City, COUNT(*) AS EmployeeCount FROM Employees GROUP BY City;

15. Q15. Get department name for each employee using aliases.

SELECT E.FirstName, D.DeptName AS Department FROM Employees E


INNER JOIN Departments D ON E.DeptID = D.DeptID;

Section D: Subqueries & Aggregations (15 Marks)


16. Q16. Get employee(s) with lowest salary.

SELECT * FROM Employees WHERE Salary = (SELECT MIN(Salary) FROM Employees);

17. Q17. Get department-wise highest salary.

SELECT DeptID, MAX(Salary) FROM Employees GROUP BY DeptID;

18. Q18. List employees earning more than department average.

SELECT * FROM Employees E1


WHERE Salary > (SELECT AVG(Salary) FROM Employees E2 WHERE E2.DeptID =
E1.DeptID);

19. Q19. Get all employees whose salary is not among top 3.

SELECT * FROM Employees


WHERE Salary NOT IN (
SELECT DISTINCT Salary FROM Employees ORDER BY Salary DESC LIMIT 3
);
20. Q20. Find second highest salary using subquery.

SELECT MAX(Salary) FROM Employees


WHERE Salary < (SELECT MAX(Salary) FROM Employees);

Section E: Group By, Having, Case & Window Functions (20 Marks)
21. Q21. Total and average salary per department.

SELECT DeptID, SUM(Salary) AS Total, AVG(Salary) AS Average


FROM Employees GROUP BY DeptID;

22. Q22. Departments with more than 5 employees.

SELECT DeptID, COUNT(*) AS EmpCount FROM Employees


GROUP BY DeptID HAVING COUNT(*) > 5;

23. Q23. Assign performance rating by salary slab.

SELECT *, CASE
WHEN Salary >= 90000 THEN 'Excellent'
WHEN Salary >= 70000 THEN 'Good'
ELSE 'Average' END AS Rating
FROM Employees;

24. Q24. Find Dense Rank of salary within departments.

SELECT *, DENSE_RANK() OVER (PARTITION BY DeptID ORDER BY Salary DESC) AS


RankInDept
FROM Employees;

25. Q25. Calculate running total salary per department.

SELECT *, SUM(Salary) OVER (PARTITION BY DeptID ORDER BY JoinDate) AS RunningTotal

FROM Employees;

Section F: Practical SQL Challenge (30 Marks)


26. Q26. Top 2 highest spending customers from 'Orders'.

SELECT CustomerID, SUM(Amount) AS TotalSpent


FROM Orders GROUP BY CustomerID ORDER BY TotalSpent DESC LIMIT 2;

27. Q27. Find customers who placed at least 3 orders.

SELECT CustomerID FROM Orders


GROUP BY CustomerID HAVING COUNT(*) >= 3;

28. Q28. Show daily order count in April 2024.


SELECT OrderDate, COUNT(*) AS Orders
FROM Orders
WHERE OrderDate BETWEEN '2024-04-01' AND '2024-04-30'
GROUP BY OrderDate;

29. Q29. Identify most recent order per customer.

SELECT * FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY CustomerID ORDER BY OrderDate
DESC) AS rn
FROM Orders
) AS sub WHERE rn = 1;

30. Q30. Calculate order amount percent contribution per day.

SELECT OrderDate,
Amount,
ROUND(Amount * 100.0 / SUM(Amount) OVER (PARTITION BY OrderDate), 2) AS
PercentContribution
FROM Orders;

You might also like