0
0
SQLquery~20 mins

Running total without window functions 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 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;
ASELECT id, amount, SUM(amount) OVER (ORDER BY id) AS running_total FROM sales ORDER BY id;
BSELECT id, amount, (SELECT SUM(amount) FROM sales WHERE id <= s.id) AS running_total FROM sales s ORDER BY id;
CSELECT 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;
DSELECT id, amount, SUM(amount) FROM sales GROUP BY id ORDER BY id;
Attempts:
2 left
💡 Hint
Think about joining the table to itself to accumulate sums up to the current row.
query_result
intermediate
2: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;
ASELECT 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;
BSELECT transaction_id, amount, SUM(amount) OVER (ORDER BY transaction_id) AS running_total FROM transactions ORDER BY transaction_id;
CSELECT t1.transaction_id, t1.amount, SUM(t2.amount) FROM transactions t1 JOIN transactions t2 ON t2.transaction_id >= t1.transaction_id GROUP BY t1.transaction_id, t1.amount ORDER BY t1.transaction_id;
DSELECT transaction_id, amount, SUM(amount) FROM transactions GROUP BY transaction_id ORDER BY transaction_id;
Attempts:
2 left
💡 Hint
Use a subquery that sums amounts up to the current transaction id.
📝 Syntax
advanced
2:00remaining
Identify syntax error in running total query
Which option contains a syntax error when trying to calculate running total without window functions?
ASELECT 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 ORDER BY a.id;
BSELECT id, amount, (SELECT SUM(amount) FROM sales WHERE id <= s.id) AS running_total FROM sales s ORDER BY id;
CSELECT id, amount, SUM(amount) FROM sales GROUP BY id ORDER BY id;
DSELECT 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
Check if all non-aggregated columns in SELECT are in GROUP BY.
optimization
advanced
2: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?
ASELECT order_id, amount, SUM(amount) FROM orders GROUP BY order_id ORDER BY order_id;
BSELECT order_id, amount, SUM(amount) OVER (ORDER BY order_id) AS running_total FROM orders ORDER BY order_id;
CSELECT o1.order_id, o1.amount, (SELECT SUM(o2.amount) FROM orders o2 WHERE o2.order_id <= o1.order_id) AS running_total FROM orders o1 ORDER BY o1.order_id;
DSELECT o1.order_id, o1.amount, SUM(o2.amount) AS running_total FROM orders o1 JOIN orders o2 ON o2.order_id <= o1.order_id GROUP BY o1.order_id, o1.amount ORDER BY o1.order_id;
Attempts:
2 left
💡 Hint
Consider which approach reduces repeated subqueries.
🧠 Conceptual
expert
2:00remaining
Why avoid window functions for running totals in some cases?
Which reason best explains why someone might calculate running totals without window functions?
ABecause window functions are always slower than self-joins or subqueries.
BBecause some database systems do not support window functions or have limited support.
CBecause running totals cannot be calculated with window functions.
DBecause window functions require complex syntax that is impossible to learn.
Attempts:
2 left
💡 Hint
Think about compatibility and database features.