Window Functions · Question 16 · Hard

Window Functions question 16

Table Deposits:
dep_id | day_no | amount
1 | 1 | 100
2 | 1 | 200
3 | 2 | 50
4 | 3 | 400

Two deposits were made on day 1. What does this query return (dep_id, total)?

SELECT dep_id,
       SUM(amount) OVER (ORDER BY day_no)
FROM Deposits
ORDER BY dep_id;
  1. 1, 100; 2, 300; 3, 350; 4, 750
  2. 1, 750; 2, 750; 3, 750; 4, 750
  3. 1, 300; 2, 300; 3, 50; 4, 400
  4. 1, 300; 2, 300; 3, 350; 4, 750
Answer and explanation

Answer: D. 1, 300; 2, 300; 3, 350; 4, 750

Explanation: With ORDER BY and no frame clause, the default frame runs from the start up to the current row and all rows tied with it on the ORDER BY value. Both day-1 rows are peers, so each gets 100 + 200 = 300. Adding ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW (with a tie-breaking sort key) would give 100 and 300.

Previous: Table Scores: player | team | runs Arjun | Royals | 82 Bhuvan | Royals | 82… · Next: Table DailySales: day_no | amount 1 | 100 2 | 250 3 | 150 4 | 300 What does…

Practise window functions

Try a fresh set of window functions questions with an explanation after every answer, free and without signing up: Window Functions practice. Or sit a full placement mock test.

More window functions questions

More sql practice

NULL Handling · SQL Functions & CASE · UNION, INTERSECT & EXCEPT · Tables, Keys & Constraints · DML & Transactions · SELECT, WHERE & ORDER BY · SQL Joins · Aggregates & GROUP BY

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge