0
0
SQLquery~5 mins

Running total without window functions in SQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a running total in SQL?
A running total is a cumulative sum of values in a column, calculated row by row, adding each value to the sum of all previous values.
Click to reveal answer
intermediate
How can you calculate a running total without using window functions?
You can calculate a running total by using a correlated subquery that sums all rows up to the current row based on an ordering column.
Click to reveal answer
intermediate
Write a simple SQL query snippet to calculate a running total without window functions.
SELECT t1.id, t1.amount, (SELECT SUM(t2.amount) FROM table_name t2 WHERE t2.id <= t1.id) AS running_total FROM table_name t1 ORDER BY t1.id;
Click to reveal answer
intermediate
Why might using window functions be preferred over correlated subqueries for running totals?
Window functions are usually more efficient and easier to read because they avoid repeated subqueries and can process rows in a single pass.
Click to reveal answer
beginner
What is a correlated subquery in the context of running totals?
A correlated subquery is a subquery that refers to the outer query's current row, allowing it to calculate sums up to that row for the running total.
Click to reveal answer
Which SQL clause is essential to order rows when calculating a running total without window functions?
AGROUP BY
BORDER BY
CHAVING
DWHERE
What does a correlated subquery do in a running total calculation?
ACalculates the sum of all rows up to the current row
BFilters rows based on a condition
CJoins two tables
DDeletes duplicate rows
Which of these is a disadvantage of using correlated subqueries for running totals?
AThey do not require ordering
BThey are always faster than window functions
CThey can be slow on large datasets
DThey cannot calculate sums
If you cannot use window functions, what SQL feature helps to calculate running totals?
AStored procedures
BTriggers
CIndexes
DCorrelated subqueries
In the query: SELECT t1.id, (SELECT SUM(t2.amount) FROM table t2 WHERE t2.id <= t1.id) FROM table t1, what does 't2.id <= t1.id' ensure?
AIt sums amounts from the first row up to the current row
BIt filters out all rows
CIt sums only the current row
DIt orders the rows randomly
Explain how to calculate a running total in SQL without using window functions.
Think about summing all previous rows for each row.
You got /3 concepts.
    Describe the advantages and disadvantages of using correlated subqueries for running totals compared to window functions.
    Consider performance and compatibility.
    You got /3 concepts.