0
0
SQLquery~5 mins

Running totals with SUM OVER in SQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the SUM() OVER() function do in SQL?
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
Which SQL clause is necessary to calculate a running total with SUM() OVER()?
AHAVING
BGROUP BY
CWHERE
DORDER BY inside OVER()
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
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;
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
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.