CS FundamentalsVery hardProduct placement · SQL round

Table Sale(id, rep, region, amt) rows: (1, 'Ira', 'N', 100), (2, 'Jai', 'S', 250), (3, 'Ira', 'S', NULL), (4, 'Kim', 'N', 300), (5, 'Jai', 'N', 150), (6, 'Ira', 'N', 200), (7, 'Lal', NULL, 120). What does this query return?

Table Sale(id, rep, region, amt) rows: (1, 'Ira', 'N', 100), (2, 'Jai', 'S', 250), (3, 'Ira', 'S', NULL), (4, 'Kim', 'N', 300), (5, 'Jai', 'N', 150), (6, 'Ira', 'N', 200), (7, 'Lal', NULL, 120). What does this query return?
SELECT SUM(amt) OVER (ORDER BY id) AS run
FROM Sale
WHERE id = 4;
  1. A. 650
  2. B. 300
  3. C. 350
  4. D. NULL

Correct answer: B. 300

WHERE is applied before the window function, so the window sees only the single row id = 4 and its running sum is 300. Evaluating the window over the full table first (650) is the trap.

More DBMS and SQL questions

Exams that ask this topic