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

SQL_Programming_Practice_Papers_Set_3_4

The document contains SQL programming practice papers, specifically Set 3 and Set 4, focusing on various SQL concepts including table management, SELECT statements, JOINS, aggregates, subqueries, and window functions. Each set includes questions with corresponding SQL commands and explanations. It serves as a resource for practicing SQL skills and understanding database operations.

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)
5 views

SQL_Programming_Practice_Papers_Set_3_4

The document contains SQL programming practice papers, specifically Set 3 and Set 4, focusing on various SQL concepts including table management, SELECT statements, JOINS, aggregates, subqueries, and window functions. Each set includes questions with corresponding SQL commands and explanations. It serves as a resource for practicing SQL skills and understanding database operations.

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/ 3

SQL Programming Practice Papers – Set 3 & Set 4 (With Answers)

Set 3

Section A: Basics & Table Management


1. Q1. Explain the difference between CHAR and VARCHAR.

- CHAR is fixed-length and pads with spaces. VARCHAR is variable-length and stores only
actual characters.

2. Q2. Create a `Products` table.

CREATE TABLE Products (


ProductID INT PRIMARY KEY,
ProductName VARCHAR(100),
Price DECIMAL(8,2),
Category VARCHAR(50)
);

3. Q3. Insert two products into the table.

INSERT INTO Products VALUES (1, 'Laptop', 75000.00, 'Electronics');


INSERT INTO Products VALUES (2, 'Chair', 3000.00, 'Furniture');

4. Q4. Update price of product with ID = 2.

UPDATE Products SET Price = 3200.00 WHERE ProductID = 2;

5. Q5. Delete all records from Products table.

DELETE FROM Products;

Section B: SELECT & Conditions


6. Q6. List products with price above 5000.

SELECT * FROM Products WHERE Price > 5000;

7. Q7. Retrieve products in Electronics category.

SELECT * FROM Products WHERE Category = 'Electronics';

8. Q8. Display all categories (no duplicates).

SELECT DISTINCT Category FROM Products;

9. Q9. Retrieve products not in Furniture category.


SELECT * FROM Products WHERE Category <> 'Furniture';

10. Q10. Sort products by price descending.

SELECT * FROM Products ORDER BY Price DESC;

Set 4

Section C: JOINS & Aggregates


11. Q11. List employees and their department names.

SELECT E.FirstName, D.DeptName FROM Employees E


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

12. Q12. List employees with department even if department is null.

SELECT E.FirstName, D.DeptName FROM Employees E


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

13. Q13. Find departments with no employees.

SELECT D.DeptName FROM Departments D


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

14. Q14. Total employees in each department.

SELECT DeptID, COUNT(*) FROM Employees GROUP BY DeptID;

15. Q15. Average salary per department.

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

Section D: Subqueries, CASE, and Window Functions


16. Q16. Employees earning more than avg salary.

SELECT * FROM Employees WHERE Salary > (SELECT AVG(Salary) FROM Employees);

17. Q17. Display employees with rank by salary.

SELECT *, RANK() OVER (ORDER BY Salary DESC) AS SalaryRank FROM Employees;

18. Q18. Add experience level based on years.

SELECT *, CASE
WHEN DATEDIFF(CURDATE(), JoinDate)/365 > 5 THEN 'Senior'
WHEN DATEDIFF(CURDATE(), JoinDate)/365 > 2 THEN 'Mid'
ELSE 'Junior' END AS ExperienceLevel
FROM Employees;
19. Q19. Find total salary in each department using window function.

SELECT *, SUM(Salary) OVER (PARTITION BY DeptID) AS TotalDeptSalary FROM


Employees;

20. Q20. Fetch top 2 earners in each department.

SELECT * FROM (
SELECT *, RANK() OVER (PARTITION BY DeptID ORDER BY Salary DESC) AS rk FROM
Employees
) sub WHERE rk <= 2;

You might also like