It calculates a running total or cumulative sum of values over a specified order without collapsing rows.
Click to reveal answer
beginner
How do you specify the order for a running total using SUM() OVER()?
You use the ORDER BY clause inside the OVER() clause to define the order of rows for the running total.
Click to reveal answer
intermediate
What is the difference between SUM() OVER() and a regular SUM() with GROUP BY?
SUM() OVER() returns a running total for each row, keeping all rows visible. Regular SUM() with GROUP BY aggregates rows into groups, returning one row per group.
Click to reveal answer
beginner
Write a simple SQL query to calculate a running total of sales ordered by date.
SELECT date, sales, SUM(sales) OVER (ORDER BY date) AS running_total FROM sales_table;
Click to reveal answer
intermediate
Can SUM() OVER() be used without an ORDER BY clause? What happens?
Yes, but without ORDER BY, it sums over the entire partition and returns the same total for every row, not a running total.
Click to reveal answer
What does the ORDER BY clause inside SUM() OVER() control?
AThe filtering of rows before summing
BThe order of the final query result only
CThe order in which rows are summed for the running total
DThe grouping of rows for aggregation
✗ Incorrect
The ORDER BY inside OVER() defines the sequence in which rows are processed to calculate the running total.
Which SQL clause is necessary to calculate a running total with SUM() OVER()?
AHAVING
BGROUP BY
CWHERE
DORDER BY inside OVER()
✗ Incorrect
The running total requires ORDER BY inside the OVER() clause to define the order of accumulation.
What will SUM(sales) OVER () without ORDER BY return?
AThe total sales repeated on every row
BA running total by date
CAn error
DOnly the first row's sales
✗ Incorrect
Without ORDER BY, the sum is over the entire partition, so every row shows the same total.
Which of these is a correct use of SUM() OVER() to get a running total?
ASELECT SUM(sales) FROM sales_table;
BSELECT sales, SUM(sales) OVER (ORDER BY date) FROM sales_table;
CSELECT sales, SUM(sales) FROM sales_table GROUP BY date;
DSELECT sales, SUM(sales) OVER () FROM sales_table ORDER BY date;
✗ Incorrect
Option B correctly uses SUM() OVER (ORDER BY date) to calculate a running total.
What is the main benefit of using SUM() OVER() for running totals?
AIt calculates totals without losing individual row details
BIt filters rows based on sum
CIt reduces the number of rows returned
DIt sorts the table permanently
✗ Incorrect
Using SUM() OVER() keeps all rows and adds a running total column, preserving detail.
Explain how to calculate a running total of sales by date using SQL window functions.
Think about how to add a cumulative sum column without grouping rows.
You got /4 concepts.
Describe the difference between using SUM() with GROUP BY and SUM() OVER() for totals.
Consider how the number of rows changes and what totals represent.
You got /4 concepts.
Practice
(1/5)
1. What does the SQL expression SUM(amount) OVER (ORDER BY date) calculate?
easy
A. A running total of the amount column ordered by date
B. The total sum of amount for all rows without order
C. The average amount grouped by date
D. The maximum amount value in the table
Solution
Step 1: Understand SUM() with OVER()
The SUM() function adds values, and OVER() defines how to apply it across rows.
Step 2: Analyze ORDER BY in OVER()
ORDER BY date means the sum accumulates in date order, creating a running total.
Final Answer:
A running total of the amount column ordered by date -> Option A
Quick Check:
SUM() OVER(ORDER BY) = running total [OK]
Hint: SUM() with ORDER BY inside OVER() means running total [OK]
Common Mistakes:
Thinking SUM() without OVER() gives running total
Confusing running total with group total
Ignoring ORDER BY inside OVER()
2. Which of the following is the correct syntax to calculate a running total of sales ordered by sale_date?
easy
A. SELECT sales, SUM(sales) OVER (PARTITION BY sale_date) FROM sales_table;
B. SELECT sales, SUM(sales) OVER (ORDER BY sale_date) FROM sales_table;
C. SELECT sales, SUM(sales) FROM sales_table ORDER BY sale_date;
D. SELECT sales, SUM(sales) OVER () ORDER BY sale_date FROM sales_table;
Solution
Step 1: Identify correct use of SUM() OVER()
Running total requires SUM() OVER with ORDER BY clause inside OVER().
Step 2: Check each option's syntax
SELECT sales, SUM(sales) OVER (ORDER BY sale_date) FROM sales_table; uses SUM(sales) OVER (ORDER BY sale_date), which is correct syntax for running total.
Final Answer:
SELECT sales, SUM(sales) OVER (ORDER BY sale_date) FROM sales_table; -> Option B
Quick Check:
SUM() OVER (ORDER BY ...) = running total syntax [OK]
Hint: Use SUM() OVER (ORDER BY column) for running totals [OK]
Common Mistakes:
Using PARTITION BY instead of ORDER BY for running total
Omitting OVER() clause
Placing ORDER BY outside OVER()
3. 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;
Running totals: 100, 450, 250 ordered by id -> Option C
Quick Check:
Running total sums in sale_date order, output sorted by id [OK]
Hint: 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
4. Identify the error in this query intended to calculate a running total of points ordered by game_date:
SELECT player_id, points, SUM(points) OVER (PARTITION BY player_id) ORDER BY game_date FROM game_scores;
medium
A. ORDER BY clause is outside the OVER() function
B. PARTITION BY cannot be used with SUM()
C. Missing GROUP BY clause
D. SUM() cannot be used with window functions
Solution
Step 1: Check placement of ORDER BY
ORDER BY for running total must be inside OVER(), not outside the function.
Step 2: Understand correct syntax
Correct syntax: SUM(points) OVER (PARTITION BY player_id ORDER BY game_date).
Final Answer:
ORDER BY clause is outside the OVER() function -> Option A
Quick Check:
ORDER BY must be inside OVER() for running totals [OK]
Hint: Put ORDER BY inside OVER() for running totals [OK]
Common Mistakes:
Placing ORDER BY outside OVER()
Confusing PARTITION BY with GROUP BY
Thinking SUM() can't be window function
5. You have a sales table with columns region, sale_date, and amount. You want to calculate a running total of sales per region ordered by sale_date. Which query correctly achieves this?
hard
A. SELECT region, sale_date, amount, SUM(amount) OVER (ORDER BY sale_date) AS running_total FROM sales;
B. SELECT region, sale_date, amount, SUM(amount) OVER (PARTITION BY sale_date ORDER BY region) AS running_total FROM sales;
C. SELECT region, sale_date, amount, SUM(amount) FROM sales GROUP BY region, sale_date ORDER BY sale_date;
D. SELECT region, sale_date, amount, SUM(amount) OVER (PARTITION BY region ORDER BY sale_date) AS running_total FROM sales;
Solution
Step 1: Understand requirement for running total per region
We need to reset running total for each region, so PARTITION BY region is required.
Step 2: Check ORDER BY for running total
ORDER BY sale_date inside OVER() ensures running total accumulates by date within each region.
Step 3: Evaluate options
SELECT region, sale_date, amount, SUM(amount) OVER (PARTITION BY region ORDER BY sale_date) AS running_total FROM sales; correctly uses PARTITION BY region and ORDER BY sale_date inside SUM() OVER().
Final Answer:
SELECT region, sale_date, amount, SUM(amount) OVER (PARTITION BY region ORDER BY sale_date) AS running_total FROM sales; -> Option D
Quick Check:
PARTITION BY region + ORDER BY sale_date = running total per region [OK]
Hint: Use PARTITION BY for groups, ORDER BY for running total order [OK]
Common Mistakes:
Omitting PARTITION BY to separate groups
Using GROUP BY instead of window functions
Swapping ORDER BY columns inside OVER()
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.