Challenge - 5 Problems
Running Total Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Calculate running total using self-join
Given a table sales with columns
id and amount, which query correctly calculates the running total of amount ordered by id without using window functions?SQL
SELECT a.id, a.amount, SUM(b.amount) AS running_total FROM sales a JOIN sales b ON b.id <= a.id GROUP BY a.id, a.amount ORDER BY a.id;
Attempts:
2 left
💡 Hint
Think about joining the table to itself to accumulate sums up to the current row.
✗ Incorrect
Option C uses a self-join to sum all amounts with id less than or equal to the current row's id, correctly calculating the running total without window functions. Option C uses window functions, which is not allowed. Option C is a correlated subquery which works but is not the option asked for here. Option C sums amounts per id without running total logic.
❓ query_result
intermediate2:00remaining
Running total with correlated subquery
Which query returns the running total of
amount from the transactions table ordered by transaction_id without using window functions?SQL
SELECT t1.transaction_id, t1.amount, (SELECT SUM(t2.amount) FROM transactions t2 WHERE t2.transaction_id <= t1.transaction_id) AS running_total FROM transactions t1 ORDER BY t1.transaction_id;
Attempts:
2 left
💡 Hint
Use a subquery that sums amounts up to the current transaction id.
✗ Incorrect
Option A uses a correlated subquery to sum amounts for all transactions with id less than or equal to the current row, correctly calculating the running total without window functions. Option A uses window functions which are not allowed. Option A groups by transaction_id but does not calculate running totals. Option A reverses the join condition causing incorrect sums.
📝 Syntax
advanced2:00remaining
Identify syntax error in running total query
Which option contains a syntax error when trying to calculate running total without window functions?
Attempts:
2 left
💡 Hint
Check if all non-aggregated columns in SELECT are in GROUP BY.
✗ Incorrect
Option A causes a syntax error because it selects a.amount but does not include it in the GROUP BY clause, which is required in standard SQL. Options A, C, and D are syntactically valid.
❓ optimization
advanced2:00remaining
Optimize running total query without window functions
Which query is the most efficient way to calculate running total on
orders table without using window functions?Attempts:
2 left
💡 Hint
Consider which approach reduces repeated subqueries.
✗ Incorrect
Option D uses a self-join with aggregation which is generally more efficient than correlated subqueries (Option D) because it avoids repeated subquery execution per row. Option D uses window functions which are disallowed. Option D does not calculate running totals.
🧠 Conceptual
expert2:00remaining
Why avoid window functions for running totals in some cases?
Which reason best explains why someone might calculate running totals without window functions?
Attempts:
2 left
💡 Hint
Think about compatibility and database features.
✗ Incorrect
Option B is correct because some older or simpler database systems do not support window functions, so alternatives like self-joins or correlated subqueries are used. Option B is false; window functions are often more efficient. Option B is false; window functions are designed for running totals. Option B is an exaggeration and incorrect.