TheAnalyst / SQLAll lessons ↗
Day 19 · Advanced SQL · 10–20 minutes

Window functions: running totals

A window frame can calculate cumulative values across ordered rows.

01 · Learn

The idea

A window frame can calculate cumulative values across ordered rows. Read the query line by line and identify the input table, the transformation, and the shape of the result before running it.

SELECT order_id, order_date, amount, SUM(amount) OVER (ORDER BY order_date) AS running_revenue FROM orders ORDER BY order_date;
Read it actively

Before running the example, predict the output columns and whether the query returns one row, many rows, or possibly no rows.

02 · Recall

Explain it without looking

In your own words, explain the main SQL feature in today’s lesson and why it answers the question.

03 · Solve

Run your own query

Show each order and cumulative revenue in date order.

Loading browser SQL engine…
04 · Repeat

Close the loop