intermediate

Running Totals

6 min readLast updated: 2026-07-23

Overview

A Running Total (or cumulative sum) calculates the ongoing total sum of values up to the current row in a sequence.

Learning Objectives

  • Compute cumulative sums using SUM() OVER (ORDER BY ...).
  • Reset running totals per category using PARTITION BY.
  • Analyze financial balances and cumulative growth.

Detailed Concept Explanation

When you add an ORDER BY clause inside an aggregate window function like SUM(), SQL automatically applies a cumulative frame (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW).


Code Examples

SQL

sql
-- Compute daily sales running total per store
SELECT 
    store_id,
    sale_date,
    daily_amount,
    SUM(daily_amount) OVER (
        PARTITION BY store_id 
        ORDER BY sale_date
    ) AS cumulative_sales
FROM daily_store_sales;

Interactive Challenges

Challenge 1: Calculate Cumulative Sum

Calculate running totals of 'amount' (alias 'running_total') ordered by 'order_date' from 'orders'.


Summary

Compute cumulative running totals using SUM(col) OVER (PARTITION BY ... ORDER BY ...).