Bird
0
0

Given the table sales_data with columns id, sale_date, and amount, what is the output of this query?

medium📝 query result Q13 of 15
SQL - Advanced Window Functions
Given the table sales_data with columns id, sale_date, and amount, what is the output of this query?
SELECT id, sale_date, amount, SUM(amount) OVER (ORDER BY sale_date) AS running_total FROM sales_data ORDER BY id;

Assume the data:
id | sale_date | amount
1 | 2024-01-01 | 100
2 | 2024-01-03 | 200
3 | 2024-01-02 | 150
ARunning totals: 100, 350, 450 ordered by id
BRunning totals: 100, 150, 350 ordered by id
CRunning totals: 100, 450, 250 ordered by id
DRunning totals: 100, 300, 450 ordered by id
Step-by-Step Solution
Solution:
  1. Step 1: Order rows by sale_date for running total

    Order by sale_date: id=1(2024-01-01), id=3(2024-01-02), id=2(2024-01-03).
  2. Step 2: Calculate running total in sale_date order

    Running totals: 100 (id=1), 100+150=250 (id=3), 250+200=450 (id=2).
  3. Step 3: Output ordered by id

    Order by id: id=1 (100), id=2 (450), id=3 (250).
  4. Final Answer:

    Running totals: 100, 450, 250 ordered by id -> Option C
  5. Quick Check:

    Running total sums in sale_date order, output sorted by id [OK]
Quick Trick: Running total sums by ORDER BY inside OVER, output order separate [OK]
Common Mistakes:
  • Assuming running total follows output ORDER BY
  • Adding amounts in id order instead of sale_date order
  • Confusing running total with simple sum

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes