CS FundamentalsHardGATE CS (PYQ pattern)

EM(id, sal, mgr): (1,90,NULL), (2,70,1), (3,70,1), (4,95,2), (5,60,2), (6,40,3), (7,80,3), (8,NULL,4), (9,85,7). How many rows does SELECT e.id FROM EM e JOIN EM m ON e.mgr = m.id JOIN EM g ON m.mgr = g.id WHERE e.sal > g.sal return (employees who earn more than their manager's manager)?

  1. A. 3
  2. B. 1
  3. C. 2
  4. D. 4

Correct answer: C. 2

Employees with a grand-manager: 4, 5, 6, 7 (grand-manager 1, salary 90), 8 (grand-manager 2, 70) and 9 (grand-manager 3, 70). 4 (95 > 90) and 9 (85 > 70) qualify; 8 has a NULL salary, so the comparison is UNKNOWN and the row is dropped. Answer 2. Comparing with the direct manager instead gives 3 (4, 7 and 9).

More DBMS and SQL questions

Exams that ask this topic