Browse SQL Topics ▼

Running Totals & Moving Averages

📂 Window Functions 📘 Lesson 60 of 81

Compute cumulative bank account balances and rolling moving averages using ordered window frames.

Running Totals & Moving Averages is an essential topic in SQL and relational database engineering.

Overview

Building on foundational principles introduced in lag-lead-offsets, mastering running totals & moving averages provides the core mental model and practical query skills needed for production relational database engineering.

SELECT
    tx_date,
    amount,
    SUM(amount) OVER (ORDER BY tx_date) AS running_balance,
    AVG(amount) OVER (ORDER BY tx_date ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) AS rolling_3day_avg
FROM transactions;

Key Concepts

  • Understand the fundamental syntax, conventions, and mental model.
  • Avoid common beginner traps, data type mismatches, and performance pitfalls.
  • Apply practical idioms directly in production applications.

[!TIP] Master this concept to build a rock-solid foundation. Next, proceed to insert-into.

Test your understanding of Running Totals & Moving Averages

Quick interactive practice questions with instant feedback.

Take Quiz →