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(*)
FROM Proj a JOIN Proj b ON a.budget = b.budget;
  1. A. 12
  2. B. 6
  3. C. 8
  4. D. 10

Correct answer: C. 8

Each row joins with every row that has an equal budget, including itself. Budgets 900, 650, 250 and 100 occur once, giving 1 pair each. Budget 400 occurs twice (pids 2 and 4), giving 2 x 2 = 4 pairs. The two NULL budgets match nothing, not even themselves, because NULL = NULL is unknown. Total 4 + 4 = 8. The answer 6 counts each non-NULL row once; 12 lets the two NULL rows pair up with each other (another 4 pairs).

More DBMS and SQL questions

Exams that ask this topic