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 lead, COUNT(CASE WHEN budget >= 400 THEN 1 END) AS big
FROM Proj
GROUP BY lead
HAVING SUM(CASE WHEN dept = 'Ops' THEN budget END) IS NULL;
  1. A. 1 row: ('Uma', 1)
  2. B. 2 rows: ('Xia', 1), ('Yash', 0)
  3. C. 3 rows: ('Uma', 1), ('Xia', 1), ('Yash', 1)
  4. D. 3 rows: ('Uma', 1), ('Xia', 1), ('Yash', 0)

Correct answer: D. 3 rows: ('Uma', 1), ('Xia', 1), ('Yash', 0)

A CASE with no ELSE yields NULL when no branch matches, and SUM over only NULLs is NULL. Uma's Ops project has a NULL budget, so her Ops sum is NULL and she qualifies; Xia and Yash have no Ops project at all, so theirs is NULL too. Vik (250) and Wen (400) have real Ops sums and are removed. COUNT counts only the rows where the CASE gives 1: Uma has 900, Xia has 650, and Yash's only budget is NULL, so his count is 0 - COUNT never returns NULL. Leaving Uma out confuses 'has an Ops project' with 'has a non-NULL Ops total'.

More DBMS and SQL questions

Exams that ask this topic