SQL • LESSON 28

Running Totals

Learn how to calculate cumulative values across ordered rows using window functions.

🔴 Advanced⏱ 3 Minutes⭐ 500 XP
🤔 THE QUESTION

How can we keep a running total as we move through a list of orders?

💡 WHAT IS IT?

A running total continuously adds the current row's value to all previous values.

🎯 WHAT IS IT USED FOR?

Running totals are useful for sales dashboards, cumulative revenue, account balances, inventory tracking, and time-based analytics.

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

Mission Brief

Practice using SUM() as a window function to calculate a running total of order amounts.

Objectives

  • SELECT
  • SUM()
  • OVER
  • ORDER BY
  • Running totals