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

SQL_Programming_Paper_with_Answers

The document is an SQL programming paper that includes questions and answers covering basic to intermediate SQL concepts, including DDL/DML operations, SELECT queries, JOINS, subqueries, aggregations, and practical SQL challenges. It provides examples of SQL commands for creating tables, inserting records, updating data, and performing various queries. Each section is designed to test knowledge and skills in SQL programming with specific tasks and expected outputs.

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

SQL_Programming_Paper_with_Answers

The document is an SQL programming paper that includes questions and answers covering basic to intermediate SQL concepts, including DDL/DML operations, SELECT queries, JOINS, subqueries, aggregations, and practical SQL challenges. It provides examples of SQL commands for creating tables, inserting records, updating data, and performing various queries. Each section is designed to test knowledge and skills in SQL programming with specific tasks and expected outputs.

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 Paper with Answers

(Basic to Intermediate)
Section A: Basics & DDL/DML (10 Marks)
 Q1. What is the difference between DELETE, TRUNCATE, and DROP?

A1.
- DELETE removes rows based on a condition and can be rolled back.
- TRUNCATE removes all rows quickly without logging individual row deletions and cannot be rolled
back.
- DROP deletes the entire table structure and data permanently.

 Q2. Write the SQL command to create a table `Employees`.

A2.
CREATE TABLE Employees (
EmpID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Salary DECIMAL(10,2),
JoinDate DATE
);

 Q3. Insert a record into `Employees`.

A3.
INSERT INTO Employees (EmpID, FirstName, LastName, Salary, JoinDate)
VALUES (101, 'John', 'Doe', 50000, '2020-04-01');

 Q4. Modify the table to add `Department`.

A4.
ALTER TABLE Employees ADD Department VARCHAR(30);

 Q5. Update the salary of employee with EmpID = 101.

A5.
UPDATE Employees SET Salary = 55000 WHERE EmpID = 101;

Section B: SELECT Queries & Filtering (10 Marks)


 Q6. Fetch employees with Salary > 50000.

A6.
SELECT * FROM Employees WHERE Salary > 50000;
 Q7. Fetch FirstName, LastName, JoinDate for employees joined after '2022-01-01'.

A7.
SELECT FirstName, LastName, JoinDate FROM Employees WHERE JoinDate > '2022-01-01';

 Q8. Fetch unique department names.

A8.
SELECT DISTINCT Department FROM Employees;

 Q9. Salary between 40000 and 60000.

A9.
SELECT * FROM Employees WHERE Salary BETWEEN 40000 AND 60000;

 Q10. First names starting with 'J'.

A10.
SELECT * FROM Employees WHERE FirstName LIKE 'J%';

Section C: JOINS & Relationships (15 Marks)


 Q11. Employees with department names.

A11.
SELECT E.FirstName, E.LastName, D.DeptName FROM Employees E
JOIN Departments D ON E.DeptID = D.DeptID;

 Q12. Employees even if no department.

A12.
SELECT E.FirstName, E.LastName, D.DeptName FROM Employees E
LEFT JOIN Departments D ON E.DeptID = D.DeptID;

 Q13. Departments with no employees.

A13.
SELECT D.DeptName FROM Departments D
LEFT JOIN Employees E ON D.DeptID = E.DeptID
WHERE E.EmpID IS NULL;

 Q14. Employee count per department.

A14.
SELECT DeptID, COUNT(*) AS EmployeeCount FROM Employees GROUP BY DeptID;

 Q15. Names of employees in IT.

A15.
SELECT E.FirstName, E.LastName FROM Employees E
JOIN Departments D ON E.DeptID = D.DeptID
WHERE D.DeptName = 'IT';
Section D: Subqueries & Aggregations (15 Marks)
 Q16. Employee(s) with highest salary.

A16.
SELECT * FROM Employees WHERE Salary = (SELECT MAX(Salary) FROM Employees);

 Q17. Average salary department-wise.

A17.
SELECT DeptID, AVG(Salary) AS AvgSalary FROM Employees GROUP BY DeptID;

 Q18. Employees earning above average salary.

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

 Q19. Salary > every HR employee.

A19.
SELECT * FROM Employees WHERE Salary > ALL (
SELECT Salary FROM Employees E
JOIN Departments D ON E.DeptID = D.DeptID WHERE D.DeptName = 'HR');

 Q20. Second highest salary.

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

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

A21.
SELECT DeptID, SUM(Salary) AS TotalSalary FROM Employees GROUP BY DeptID;

 Q22. Departments with avg salary > 60000.

A22.
SELECT DeptID, AVG(Salary) FROM Employees GROUP BY DeptID HAVING AVG(Salary) > 60000;

 Q23. Add SalaryGrade.

A23.
SELECT *, CASE
WHEN Salary > 80000 THEN 'A'
WHEN Salary BETWEEN 50000 AND 80000 THEN 'B'
ELSE 'C' END AS SalaryGrade FROM Employees;

 Q24. Rank by salary within department.


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

 Q25. Cumulative salary per department.

A25.
SELECT *, SUM(Salary) OVER (PARTITION BY DeptID ORDER BY Salary) AS CumulativeSalary FROM
Employees;

Section F: Practical SQL Challenge (30 Marks)


 Q26. Top 3 customers by purchase amount.

A26.
SELECT CustomerID, SUM(Amount) AS TotalAmount FROM Orders GROUP BY CustomerID ORDER
BY TotalAmount DESC LIMIT 3;

 Q27. Customers with no orders.

A27.
SELECT * FROM Customers WHERE CustomerID NOT IN (SELECT DISTINCT CustomerID FROM
Orders);

 Q28. Month-wise sales.

A28.
SELECT DATE_FORMAT(OrderDate, '%Y-%m') AS Month, SUM(Amount) AS MonthlySales FROM
Orders GROUP BY Month;

 Q29. Customer with max number of orders.

A29.
SELECT CustomerID, COUNT(*) AS OrderCount FROM Orders GROUP BY CustomerID ORDER BY
OrderCount DESC LIMIT 1;

 Q30. Percentage contribution of each customer.

A30.
SELECT CustomerID,
ROUND(SUM(Amount) * 100.0 / (SELECT SUM(Amount) FROM Orders), 2) AS
PercentageContribution
FROM Orders GROUP BY CustomerID;

You might also like