Bird
0
0

Given the table sales with rows:

medium📝 query result Q13 of 15
SQL - Advanced Query Patterns
Given the table sales with rows:
id | amount
1 | 10
2 | 20
3 | 15

What is the output of this query?
SELECT id, amount, (SELECT SUM(amount) FROM sales WHERE id <= s.id) AS running_total FROM sales s ORDER BY id;
A1 | 10 | 10<br>2 | 20 | 20<br>3 | 15 | 15
B1 | 10 | 10<br>2 | 20 | 30<br>3 | 15 | 45
C1 | 10 | 10<br>2 | 20 | 25<br>3 | 15 | 40
D1 | 10 | 10<br>2 | 20 | 30<br>3 | 15 | 15
Step-by-Step Solution
Solution:
  1. Step 1: Calculate running total for each row

    For id=1: sum amounts where id <=1 is 10.
    For id=2: sum amounts where id <=2 is 10+20=30.
    For id=3: sum amounts where id <=3 is 10+20+15=45.
  2. Step 2: Match calculated values with options

    1 | 10 | 10
    2 | 20 | 30
    3 | 15 | 45 matches these running totals exactly.
  3. Final Answer:

    1 | 10 | 10
    2 | 20 | 30
    3 | 15 | 45
    -> Option B
  4. Quick Check:

    Running total sums all previous rows [OK]
Quick Trick: Add amounts cumulatively by id order [OK]
Common Mistakes:
  • Summing only current row amount
  • Not including all previous rows in sum
  • Mixing up running total values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes