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 of this query's result show a count of 0?

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 of this query's result show a count of 0?
SELECT b.name, COUNT(*)
FROM Staff b LEFT JOIN Staff e ON e.boss = b.id
GROUP BY b.id, b.name;
  1. A. 0
  2. B. 6
  3. C. 1
  4. D. 3

Correct answer: A. 0

COUNT(*) counts rows in each group, and an employee with no subordinates still has one LEFT-JOIN row padded with NULLs, so the minimum count is 1. COUNT(e.id) would show 0 for Dev, Esha and Farid.

More DBMS and SQL questions

Exams that ask this topic