Solve “Deduplicate events with ROW_NUMBER”

Free. Runs in your browser, no install.

SQL · Window Functions

What is a running total?

Mediumrunning totalcumulative sumwindow

Interview question

What is a running total?

How to answer

The short answer

A running total is a cumulative sum.

Example:

Output
Day   Sales
1     100
2     200
3     50

Running total:

Output
100
300
350

SQL:

SQL
SUM(sales) OVER (
    ORDER BY sale_date
    ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
)

The important part is:

SQL
ORDER BY sale_date

because SQL needs to know the sequence.

Lock it in with a real drill

You remember an answer longer once you have used it. Try a hands-on problem on this concept.

Open related drill