AVG() OVER

SQL · Window Functions

Theory

AVG(column) OVER (...) computes an average over the window and attaches it to each row. Without ORDER BY it is the average of the whole partition, such as the department average shown on each employee. With ORDER BY and a ROWS frame it becomes a moving average, which is its most popular use.

AVG(revenue) OVER (ORDER BY month ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) is a 3-month moving average: for each month it averages that month and the two before it. In the first two months there are fewer than 3 rows available, so the average uses only 1 and then 2 rows, so the early values are not true 3-month averages. If that matters, show NULL until enough rows exist (for example with a CASE on COUNT(*) OVER the same frame). Moving averages smooth out noise so trends are easier to see in charts.

Why we use it

Daily and monthly numbers jump around. A moving average smooths these swings so the real trend shows, and a partition average lets you compare each row with its group's typical value, both in one query.

When to use it

Use AVG() OVER with a ROWS frame for moving averages over a fixed number of rows (assuming no missing periods), or with just PARTITION BY to compare rows with their group mean. Use GROUP BY AVG for one number per group.

How it works

  1. Rows sorted by the window ORDER BY
  2. Frame: current row and the n rows before it
  3. Fewer than n+1 rows so far? Yes: Average the rows that exist. No: Average exactly n+1 rows.
  4. Write the average on the current row
  5. Slide the frame one row forward
  6. Moving average for every row

Syntax

AVG(column) OVER (ORDER BY col ROWS BETWEEN n PRECEDING AND CURRENT ROW)

Example

SELECT name, salary,
  AVG(salary) OVER (ORDER BY id ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) AS moving_avg_3
FROM employees;

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic