CS FundamentalsVery hardProduct company interview · GATE level

Table Proj(pid, lead, dept, budget, parent) rows: (1, 'Uma', 'R&D', 900, NULL), (2, 'Vik', 'R&D', 400, 1), (3, 'Uma', 'Ops', NULL, 1), (4, 'Wen', 'Ops', 400, 2), (5, 'Xia', NULL, 650, 2), (6, 'Vik', 'Ops', 250, 4), (7, 'Yash', 'R&D', NULL, NULL), (8, 'Wen', NULL, 100, 6). What does this query return?

Table Proj(pid, lead, dept, budget, parent) rows: (1, 'Uma', 'R&D', 900, NULL), (2, 'Vik', 'R&D', 400, 1), (3, 'Uma', 'Ops', NULL, 1), (4, 'Wen', 'Ops', 400, 2), (5, 'Xia', NULL, 650, 2), (6, 'Vik', 'Ops', 250, 4), (7, 'Yash', 'R&D', NULL, NULL), (8, 'Wen', NULL, 100, 6). What does this query return?
SELECT COUNT(DISTINCT dept), COUNT(*)
FROM (SELECT dept FROM Proj GROUP BY dept) g;
  1. A. (3, 3)
  2. B. (2, 3)
  3. C. (2, 2)
  4. D. (2, 8)

Correct answer: B. (2, 3)

GROUP BY treats all NULL depts as one group, so the inner query returns three rows: 'R&D', 'Ops' and NULL. COUNT(*) counts rows and gives 3. COUNT(DISTINCT dept) ignores NULL and gives 2. So NULLs are 'equal' for grouping but invisible to COUNT(column) - both rules apply in the same query.

More DBMS and SQL questions

Exams that ask this topic