// window functions

Running totals & moving averages: the aggregates you know, with OVER added

SUM and AVG become cumulative or rolling totals the moment you add OVER. The difference between the two is one word in the frame.

Published 12 Jul 20267 min read32 reads

SUM, AVG, and the other aggregates you already know can become window functions too, you just add OVER (...). That single change turns "one total for everything" into "a running total that grows row by row."

🎯 Explain Like I'm Hired A running total is like a bank balance: each row shows "everything up to and including today," not just today's amount. SUM(amount) OVER (ORDER BY order_date) does exactly that. Example: "show cumulative revenue by day, so far this quarter" is this one line, no self-join, no subquery needed.

SELECT
  order_date,
  amount,
  SUM(amount) OVER (ORDER BY order_date) AS running_total
FROM orders;

Adding ORDER BY inside OVER(...), with no other instructions, defaults to "add up everything from the start through the current row," which is exactly a running total.

Sign up to keep reading

Sign up free to unlock the worked examples, edge cases, and interview traps below.