Stop 08 · Window FunctionsTopics · 7/9

Window Frames and Moving Averages

Part of the SQL Roadmap

In short

Daily sales jump around so much that trends hide. A 7-day moving average smooths the noise, and window frames are the tool that tells SQL exactly which nearby rows to use.

A window frame, such as ROWS BETWEEN 6 PRECEDING AND CURRENT ROW, limits a window calculation to nearby rows. That is how you build moving averages and rolling sums. ROWS counts physical rows, while RANGE groups rows with equal ORDER BY values.

The jaadu presses its face to a moving train window, watching a short stretch of a crowded platform slide past as the train rolls on.

Think of it like this

The view from a train window

On the Kolkata local, your window shows only a short stretch of the platform at a time. As the train moves, old stretches slide out of view and new ones slide in. A window frame like ROWS BETWEEN 2 PRECEDING AND CURRENT ROW is that window: always the same width, always moving with you.

Watch it happen

3-day moving average: ROWS BETWEEN 2 PRECEDING AND CURRENT ROW

300
0
600
1
900
2
300
3
1200
4
600
5

1/6Six days of sales. The frame will cover today plus the two days before it.

The big ideas

What you need to know

Frames set the width

ROWS BETWEEN 6 PRECEDING AND CURRENT ROW means 'this row and the six before it', which is 7 rows.

ROWS vs RANGE

ROWS counts physical rows. RANGE groups rows with the same ORDER BY value, so tied dates move in or out together.

Early rows are partial

The first 6 days of a 7-day average use fewer rows. Count the rows in the frame and hide partial ones if needed.

Rows are not days

If a day has no sales row, a 7-row frame stretches over 8 calendar days. Fill missing dates first.

See it in code

Walk through the code

sql
1SELECT2  sale_date,3  revenue,4  ROUND(AVG(revenue) OVER (5    ORDER BY sale_date6    ROWS BETWEEN 6 PRECEDING AND CURRENT ROW7  ), 2) AS avg_7d,8  COUNT(*) OVER (9    ORDER BY sale_date10    ROWS BETWEEN 6 PRECEDING AND CURRENT ROW11  ) AS days_in_frame12FROM daily_sales13WHERE store = 'Hyderabad'14ORDER BY sale_date;

1/3Average over today and the six rows before it, rounded to paise.

A 7-day moving average plus a count showing how full each frame is. ROWS frames work in PostgreSQL, MySQL 8+ and SQL Server. PostgreSQL 11+ also supports RANGE BETWEEN INTERVAL '6 days' PRECEDING AND CURRENT ROW for true calendar windows.

Save yourself the pain

Common mistakes

Oops

Trusting the first six values of a 7-day average.

Do this instead

They average fewer days. Show days_in_frame or filter WHERE days_in_frame = 7 in an outer query.

Oops

Building a moving average across many stores with no PARTITION BY.

Do this instead

Add PARTITION BY store so each store's frame only slides over its own days.

Your mission

Smooth the noise

~25 min

Quick check

How many rows does ROWS BETWEEN 2 PRECEDING AND 2 FOLLOWING cover in the middle of a long table?

Go deeper

  1. 1Calculate a 7-day moving average of daily sales.
  2. 2Compare ROWS and RANGE frames on data with duplicate dates.
  3. 3Learn the default frame when ORDER BY is present (RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) and why it can surprise you.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge