CS FundamentalsHardGATE CS · SQL

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). How many rows 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). How many rows does this query return?
SELECT dept, MAX(pay) - MIN(pay)
FROM Staff
GROUP BY dept
HAVING COUNT(pay) = COUNT(*);
  1. A. 3
  2. B. 2
  3. C. 0
  4. D. 1

Correct answer: B. 2

GROUP BY makes three groups: Ops, Tech and NULL (Esha). Tech fails HAVING because Dev's pay is NULL (COUNT(pay) = 2 ≠ 3). Ops and the NULL group both pass, so 2 rows.

More DBMS and SQL questions

Exams that ask this topic