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). 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 COUNT(*)
FROM Staff e LEFT JOIN Staff b ON e.boss = b.id
WHERE b.dept = 'Ops' OR b.id IS NULL;
  1. A. 1
  2. B. 6
  3. C. 3
  4. D. 4

Correct answer: D. 4

Bala and Chitra report to Anu (Ops) and Farid to Bala (Ops); Anu has no boss, so the outer join pads b with NULLs and b.id IS NULL keeps her. Dev and Esha report to Chitra (Tech). Total 4.

More DBMS and SQL questions

Exams that ask this topic