0
0
SQLquery~10 mins

Running totals with SUM OVER in SQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Running totals with SUM OVER
Start with table rows
Order rows by a column
For each row, calculate SUM of values from first row to current row
Add running total as new column
Return result with running totals
The query orders rows and calculates a running total by summing values from the start up to each row.
Execution Sample
SQL
SELECT id, amount,
       SUM(amount) OVER (ORDER BY id) AS running_total
FROM sales;
Calculates running total of 'amount' ordered by 'id' in the sales table.
Execution Table
StepRow idamountRunning total calculationrunning_total
11100SUM(amount) from id=1 to 1 = 100100
22200SUM(amount) from id=1 to 2 = 100 + 200300
33150SUM(amount) from id=1 to 3 = 100 + 200 + 150450
4450SUM(amount) from id=1 to 4 = 450 + 50500
55300SUM(amount) from id=1 to 5 = 500 + 300800
6EndNo more rows to process
💡 All rows processed, running totals calculated for each row.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
running_total0100300450500800800
Key Moments - 2 Insights
Why does the running total increase with each row?
Because SUM OVER adds the current row's amount to the sum of all previous rows, as shown in execution_table rows 1 to 5.
What happens if the ORDER BY clause is missing in SUM OVER?
Without ORDER BY, the running total is not calculated in a meaningful order, so results may be incorrect or unpredictable, unlike the ordered steps in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the running_total at step 3?
A150
B300
C450
D500
💡 Hint
Check the 'running_total' column at step 3 in the execution_table.
At which step does the running_total first exceed 400?
AStep 3
BStep 2
CStep 4
DStep 5
💡 Hint
Look at the running_total values in execution_table rows to find when it passes 400.
If the amount in row with id=4 was 100 instead of 50, what would be the running_total at step 4?
A600
B550
C500
D450
💡 Hint
Add the new amount 100 to the running_total at step 3 (450) to find the new total.
Concept Snapshot
SUM() OVER (ORDER BY column) calculates a running total.
It sums values from the first row up to the current row.
ORDER BY defines the order of rows for summing.
Running totals help track cumulative sums over ordered data.
Full Transcript
This visual execution shows how the SQL query calculates running totals using SUM OVER. The query orders rows by id and sums the amount column from the first row up to each current row. The execution table traces each step, showing how the running total grows as rows are processed. The variable tracker highlights the running_total value after each row. Key moments clarify why ordering is important and how the running total accumulates. The quiz tests understanding of running total values at specific steps and effects of changing data. This helps beginners see how running totals work step-by-step in SQL.