Seven SQL queries on employee data

December 4, 2026 · Team EasyExamNotes · 2 min read (est.) · 0 comments

Share on WhatsApp

← RGPV CS-502 DBMS November 2023 solved question paper

RGPV CS-502 (GS) | November 2023 | Q3(b) | 7 marks

Question (restated): Using the supplied employee relation, solve seven filtering, grouping and salary comparison queries.

Answer

E_ID E_Name Gender Salary Manager_ID Dept_ID
1 A M 8000 NULL 101
2 B M 5000 1 101
3 C F 2000 1 101
4 D F 2000 2 102
5 E M 4000 2 102
6 F M 2000 1 103
7 G F 4500 1 103
8 H F 3000 1 104

1. Departments averaging above 4000

SELECT Dept_ID FROM Employee GROUP BY Dept_ID HAVING AVG(Salary)>4000;

Result: 101 (average 5000)

2. Male employee average above 2000

SELECT Dept_ID FROM Employee WHERE Gender='M' GROUP BY Dept_ID HAVING AVG(Salary)>2000;

Result: 101, 102

3. Employees reporting to manager 1

SELECT E_Name FROM Employee WHERE Manager_ID=1;

Result: B, C, F, G, H

4. Department 103 and manager 1

SELECT E_Name FROM Employee WHERE Dept_ID=103 AND Manager_ID=1;

Result: F, G

5. Second highest distinct salary

SELECT E_Name, Salary, Dept_ID FROM Employee WHERE Salary=(SELECT MAX(Salary) FROM Employee WHERE Salary<(SELECT MAX(Salary) FROM Employee));

Result: B, 5000, 101

6. Employees earning more than F

SELECT E_ID,E_Name FROM Employee WHERE Salary>(SELECT Salary FROM Employee WHERE E_Name='F');

Result: 1 A; 2 B; 5 E; 7 G; 8 H

7. Departments of employees earning more than E

SELECT DISTINCT Dept_ID FROM Employee WHERE Salary>(SELECT Salary FROM Employee WHERE E_Name='E');

Result: 101, 103

WHERE filters individual rows before grouping; HAVING filters groups after aggregation. The second-highest query finds the maximum salary below the overall maximum and returns all ties. Scalar subqueries assume F and E each identify one employee in the supplied data. The final question uses D_ID although the table names the column Dept_ID; the answer interprets it as Dept_ID. DISTINCT removes repeated department identifiers. SQL does not guarantee result order without ORDER BY.

Further reading: Silberschatz, Korth and Sudarshan, Database System Concepts; Elmasri and Navathe, Fundamentals of Database Systems.

← RGPV CS-502 DBMS November 2023 solved question paper

Discussion

Ask a question, share your thoughts or report an error.

Sign in to participate in the discussion.

Join the discussion