CS FundamentalsVery hardTCS Digital · Infosys SP · GATE CS

Table Staff(id, name, dept, pay, boss) rows: (1, 'Anu', 'Ops', 40, NULL), (2, 'Bala', 'Ops', 55, 1), (3, 'Chitra', 'Tech', 70, 1), (4, 'Dev', 'Tech', NULL, 3), (5, 'Esha', NULL, 35, 3), (6, 'Farid', 'Tech', 70, 2). What does this query return?

Table Staff(id, name, dept, pay, boss) rows: (1, 'Anu', 'Ops', 40, NULL), (2, 'Bala', 'Ops', 55, 1), (3, 'Chitra', 'Tech', 70, 1), (4, 'Dev', 'Tech', NULL, 3), (5, 'Esha', NULL, 35, 3), (6, 'Farid', 'Tech', 70, 2). What does this query return?
SELECT s.name
FROM Staff s JOIN (SELECT dept, MAX(pay) AS m FROM Staff GROUP BY dept) t
  ON s.dept = t.dept AND s.pay = t.m
ORDER BY s.name;
  1. A. Bala, Chitra, Esha, Farid
  2. B. Bala, Chitra, Farid
  3. C. Bala, Chitra
  4. D. Chitra, Farid

Correct answer: B. Bala, Chitra, Farid

Group maxima: Ops 55, Tech 70, NULL-dept group 35. Joining back on s.dept = t.dept fails for Esha because NULL = NULL is unknown, so the result is Bala, Chitra and Farid (both 70s tie).

More DBMS and SQL questions

Exams that ask this topic