0
0
SQLquery~20 mins

Running totals with SUM OVER in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Running Total Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Calculate running total of sales by date

Given a table sales with columns sale_date and amount, which query correctly calculates the running total of amount ordered by sale_date?

SQL
SELECT sale_date, amount, SUM(amount) OVER (ORDER BY sale_date) AS running_total FROM sales;
ASELECT sale_date, amount, SUM(amount) OVER (PARTITION BY sale_date) AS running_total FROM sales;
BSELECT sale_date, amount, SUM(amount) FROM sales GROUP BY sale_date ORDER BY sale_date;
CSELECT sale_date, amount, SUM(amount) OVER (ORDER BY sale_date) AS running_total FROM sales;
DSELECT sale_date, amount, SUM(amount) OVER () AS running_total FROM sales;
Attempts:
2 left
💡 Hint

Think about how to accumulate sums in order of dates.

📝 Syntax
intermediate
2:00remaining
Identify the syntax error in running total query

Which option contains a syntax error when trying to calculate a running total of amount ordered by sale_date?

ASELECT sale_date, amount, SUM(amount) OVER (ORDER BY sale_date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS running_total FROM sales;
BSELECT sale_date, amount, SUM(amount) OVER (ORDER BY sale_date ROWS UNBOUNDED PRECEDING) AS running_total FROM sales;
CSELECT sale_date, amount, SUM(amount) OVER (ORDER BY sale_date RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS running_total FROM sales;
DSELECT sale_date, amount, SUM(amount) OVER (ORDER BY sale_date) AS running_total FROM sales;
Attempts:
2 left
💡 Hint

Check the syntax of the frame clause after ORDER BY.

optimization
advanced
2:00remaining
Optimize running total query for large datasets

You have a large sales table and want to calculate running totals by sale_date. Which query is most efficient?

ASELECT sale_date, amount, SUM(amount) OVER (PARTITION BY sale_date ORDER BY sale_date) AS running_total FROM sales;
BSELECT sale_date, amount, (SELECT SUM(amount) FROM sales s2 WHERE s2.sale_date <= s1.sale_date) AS running_total FROM sales s1 ORDER BY sale_date;
CSELECT sale_date, amount, SUM(amount) FROM sales GROUP BY sale_date ORDER BY sale_date;
DSELECT sale_date, amount, SUM(amount) OVER (ORDER BY sale_date) AS running_total FROM sales;
Attempts:
2 left
💡 Hint

Consider how window functions perform compared to subqueries.

🔧 Debug
advanced
2:00remaining
Debug incorrect running total result

A query calculates running totals but the totals reset unexpectedly. Which option explains why?

SQL
SELECT sale_date, amount, SUM(amount) OVER (PARTITION BY sale_date ORDER BY sale_date) AS running_total FROM sales;
AThe PARTITION BY clause resets the running total for each sale_date, causing unexpected resets.
BThe ORDER BY clause is missing, so running totals are unordered and reset.
CThe SUM() function cannot be used with OVER clause for running totals.
DThe query lacks a GROUP BY clause, so running totals are incorrect.
Attempts:
2 left
💡 Hint

Think about what PARTITION BY does in window functions.

🧠 Conceptual
expert
2:00remaining
Understanding frame clauses in running totals

Which frame clause correctly defines a running total that includes all rows from the start up to the current row ordered by sale_date?

AROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
BROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
CRANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
DROWS BETWEEN UNBOUNDED FOLLOWING AND CURRENT ROW
Attempts:
2 left
💡 Hint

Consider how frame boundaries define the window of rows included.