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;
- A. 650
- B. 300
- C. 350
- 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
- A file of 10,000 records is physically ordered on the non-key field dept, which has 500 distinct values. A clustering index on dept has…
- Which expression is equivalent to R ÷ S, where R(A, B) and S(B)?
- Which is a correct reason why testing a schedule for VIEW serializability is rarely done in practice?
- 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'…
- Which statement about ARIES recovery is correct?
- Table Staff(id, name, dept, pay, boss) rows: (1, 'Anu', 'Ops', 40, NULL), (2, 'Bala', 'Ops', 55, 1), (3, 'Chitra', 'Tech', 70, 1), (4…