← RGPV CS-502 DBMS November 2022 solved question paper
RGPV CS-502 (GS) | November 2022 | Q3(b) | 7 marks
Question (restated): Using the supplied employee data, solve salary comparisons against P and N, ANY and ALL comparisons, matching salaries, second highest salary and department averages.
Answer
Given Employee relation
| E_ID | E_Name | Salary | Manager_ID | Dept_ID |
|---|---|---|---|---|
| 1 | A | 8000 | NULL | 101 |
| 2 | R | 5000 | 1 | 101 |
| 3 | T | 2000 | 1 | 101 |
| 4 | M | 2000 | 2 | 102 |
| 5 | N | 4000 | 2 | 102 |
| 6 | S | 2000 | 1 | 103 |
| 7 | P | 4500 | 1 | 103 |
| 8 | H | 3000 | 1 | 104 |
Subqueries return comparison values from the same relation. ANY requires at least one true comparison; ALL requires every comparison to be true. The supplied salary data contains no NULL values.
Salary above P (4500)
SELECT E_ID,E_Name FROM Employee WHERE Salary > (SELECT Salary FROM Employee WHERE E_Name='P');
Result: 1 A; 2 R
Department IDs for salary above N (4000)
SELECT Dept_ID FROM Employee WHERE Salary > (SELECT Salary FROM Employee WHERE E_Name='N');
Result: 101, 101, 103 (use DISTINCT for unique IDs). The printed D_ID is interpreted as Dept_ID.
Salary above any employee in department 102
SELECT E_ID,E_Name FROM Employee WHERE Salary > ANY (SELECT Salary FROM Employee WHERE Dept_ID=102);
Result: 1 A; 2 R; 5 N; 7 P; 8 H. The minimum is 2000.
Salary above all employees in department 103
SELECT E_ID,E_Name FROM Employee WHERE Salary > ALL (SELECT Salary FROM Employee WHERE Dept_ID=103);
Result: 1 A; 2 R. The maximum is 4500.
Salary equal to a salary in department 103
SELECT E_ID,E_Name FROM Employee WHERE Salary IN (SELECT Salary FROM Employee WHERE Dept_ID=103);
Result: 3 T; 4 M; 6 S; 7 P.
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: R, 5000, 101.
Departments with average salary above 3000
SELECT Dept_ID FROM Employee GROUP BY Dept_ID HAVING AVG(Salary)>3000;
Result: 101 (5000), 103 (3250). Departments 102 and 104 average 3000 exactly.
Further reading: Silberschatz, Korth and Sudarshan, Database System Concepts.
Discussion
Ask a question, share your thoughts or report an error.
Sign in to participate in the discussion.
Join the discussion