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 under standard SQL semantics?

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 under standard SQL semantics?
SELECT COUNT(*) FROM Proj
WHERE budget > ALL (SELECT budget FROM Proj WHERE dept = 'HR');
  1. A. 8
  2. B. 6
  3. C. 0
  4. D. The query fails with an error

Correct answer: A. 8

No project belongs to 'HR', so the subquery returns an empty set. A comparison with ALL over an empty set is TRUE for every row - there is no value that could make it fail - and this holds even for the two rows whose budget is NULL, because no comparison is ever carried out. All 8 rows are counted. The answer 6 assumes the NULL budgets drop out; 0 would be the result of > ANY, which is FALSE over an empty set.

More DBMS and SQL questions

Exams that ask this topic