CS FundamentalsHardGATE CS
Emp(id, dept, sal) = (1, A, 50), (2, A, 70), (3, A, NULL), (4, B, 40), (5, B, 60), (6, B, 60), (7, NULL, 90), (8, C, 30). How many rows does SELECT id FROM Emp e1 WHERE sal > (SELECT AVG(sal) FROM Emp e2 WHERE e2.dept = e1.dept AND e2.id <> e1.id) return?
- A. 5
- B. 2
- C. 4
- D. 3
Correct answer: D. 3
For each row the subquery averages the other employees of the same department. NULL departments match nothing (the subquery is empty, so AVG is NULL), a NULL salary never satisfies the comparison, and a lone employee in a department also fails. Only the rows with a salary strictly above the average of their colleagues qualify: 3 rows.
More DBMS and SQL questions
- Ed(a, b) = (1, 2), (1, 3), (2, 4), (3, 4), (4, 5), (4, 6), (5, 7), (6, 7), (2, 5) is a directed acyclic graph. The query WITH RECURSIVE…
- O(cust, item) = (c1, x), (c1, x), (c1, y), (c2, z), (c2, NULL), (c3, NULL), (c3, NULL), (c4, w), (c4, w), (c5, p), (c5, q), (c5, p). How…
- R(x) = (1, 2, 2, 3, NULL, NULL, 5), S(x) = (2, 3, 3, NULL, 6), T(x) = (2, 2, 3, NULL, 5, 7) (one column each). How many rows does (SELECT…
- P(g, v) = (a, 5), (a, 7), (a, 7), (b, 3), (b, NULL), (b, 9), (c, 4), (c, 4), (d, NULL), (NULL, 1), (NULL, 2). How many rows does SELECT…
- T(g, v) = (g1, 5), (g1, 15), (g2, 8), (g2, 9), (g2, NULL), (g3, 50), (g4, NULL), (g4, NULL), (g5, 12), (g5, 3). How many groups does…
- Tk(s, c) = (s1, c1), (s1, c2), (s1, c3), (s2, c1), (s2, c2), (s3, c1), (s3, c2), (s3, c3), (s3, c4), (s4, c3), (s5, c2), (s5, c3), (s5…