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). Which pids 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). Which pids does this query return?
SELECT pid FROM (
  SELECT pid, budget,
         AVG(budget) OVER (PARTITION BY lead) AS a
  FROM Proj) t
WHERE budget > a;
  1. A. pids 2 and 4
  2. B. pids 1, 2 and 4
  3. C. pids 1 and 5
  4. D. pids 1, 2, 4 and 5

Correct answer: A. pids 2 and 4

AVG ignores NULLs. Uma: budgets 900 and NULL, average 900, and 900 > 900 is false. Vik: 400 and 250, average 325, so pid 2 qualifies. Wen: 400 and 100, average 250, so pid 4 qualifies. Xia has one project (650 is not above its own average) and Yash's only budget is NULL. Result: pids 2 and 4. Pid 1 appears only if the NULL is wrongly counted as 0 (average 450); pids 1 and 5 are the rows above the overall average, which ignores the PARTITION BY.

More DBMS and SQL questions

Exams that ask this topic