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). After this statement runs, which rows remain in Proj?

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). After this statement runs, which rows remain in Proj?
DELETE FROM Proj
WHERE budget < (SELECT AVG(budget) FROM Proj);
  1. A. 2 rows: pids 1, 5
  2. B. 4 rows: pids 1, 3, 5, 7
  3. C. 6 rows: every pid except 6 and 8
  4. D. 5 rows: pids 1, 2, 3, 5, 7

Correct answer: B. 4 rows: pids 1, 3, 5, 7

The subquery is evaluated once on the table as it was before the delete: AVG ignores the two NULLs, giving (900 + 400 + 400 + 650 + 250 + 100)/6 = 450. Rows with budget below 450 are deleted: pids 2, 4, 6 and 8. For pids 3 and 7 the test NULL < 450 is unknown, so they are not deleted. Remaining: 1, 3, 5, 7. Counting the NULLs as 0 would give an average of 337.5 and delete only pids 6 and 8; assuming NULL rows are also removed leaves only pids 1 and 5.

More DBMS and SQL questions

Exams that ask this topic