Moving Averages & Rolling Windows (7-Day, 30-Day Rolling Metrics)
Smooth noisy series with rolling 7-day and 30-day window averages.
In plain words
For each day, average it together with the 6 days before it. As you move forward one day, the oldest day drops out.
Think of it like…
A cricket batter's average over their last 5 innings: each new innings pushes the oldest one out, so the number reflects current form, not their whole career.
Where you’ve seen it
A 7-day rolling average of daily signups, where days with no signups would otherwise be missing: join to a calendar first, then AVG(signups) OVER (ORDER BY day ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) over the complete series.
Words you’ll hear
- Window frame
- The rows included in each calculation: here, 6 preceding rows plus the current row.
- Trailing window
- Looks only backwards (today and the past N−1 days), as dashboards usually do.
- ROWS vs RANGE offset
- ROWS counts rows; RANGE BETWEEN INTERVAL '6 days' PRECEDING counts actual days.
- Warm-up period
- The first N−1 rows, which have fewer than N values in their window.
Visual diagram
Theory & example queries
Writing the frame
ROWS BETWEEN 6 PRECEDING AND CURRENT ROW means "this row and the 6 before it", which is 7 rows. Add PARTITION BY to compute separate rolling averages per product or region. A centered average (3 preceding and 3 following) smooths better for analysis but changes as new data arrives, so dashboards usually use trailing windows.
ROWS counts rows, not days. If Sunday has no row, the "7-day" window for Monday reaches back 8 days. Either fill the gaps by joining to a complete calendar (generate_series in PostgreSQL), or, in PostgreSQL 11+, use RANGE BETWEEN INTERVAL '6 days' PRECEDING AND CURRENT ROW, which looks at dates rather than row positions.
| id | country | city | signup_date |
|---|---|---|---|
| 1 | IN | Pune | 2026-10-01 |
| 2 | IN | Delhi | 2026-10-02 |
| 3 | NULL | Mumbai | 2026-10-03 |
WITH days AS ( SELECT d::date AS DAY FROM generate_series( '2026-10-01'::date, '2026-11-19', '1 day' ) d ), daily AS ( SELECT days.day, COUNT(u.id) AS signups FROM days LEFT JOIN users u ON u.created_at::date = days.day GROUP BY days.day )SELECT DAY, AVG(signups) OVER ( ORDER BY DAY ROWS BETWEEN 6 PRECEDING AND CURRENT ROW ) AS signups_7dFROM daily;PostgreSQL is the lesson baseline unless noted otherwise.
Read this line together with the surrounding statement as part of the same SQL expression.
First four sample dates4 sample rows
| day | signups | signups_7d |
|---|---|---|
| 2026-10-01 | 12 | 12 |
| 2026-10-02 | 18 | 15 |
| 2026-10-03 | 0 | 10 |
| 2026-10-04 | 10 | 10 |
Note: Sample signup counts are illustrative. The first windows are partial because only the rows from 2026-10-01 onward are available.
Key takeaway: Fill missing days first, or the window silently covers more than 7 days.
Warm-up rows and rolling rates
The first 6 rows don't have 6 predecessors, so their "7-day" average covers 1 to 6 days. Count the rows in the same frame (COUNT(*) OVER (… same frame …)) and show NULL, or filter, until it reaches 7.
For ratios such as conversion rate, a rolling average of daily rates weights a quiet day the same as a busy one. The correct 7-day rate is SUM(conversions) over the window divided by SUM(visits) over the window.
SUM(conversions) OVER w::numeric / NULLIF(SUM(visits) OVER w, 0) … WINDOW w AS (ORDER BY day ROWS BETWEEN 6 PRECEDING AND CURRENT ROW)
How it works, step by step
Follow one practical SQL workflow from start to finish. Select any step or play through the flow.
Aggregate to one row per day
Build a daily series first (per product or region if needed). Raw events can't be rolled by row.
GROUP BY created_at::dateWhen to use it & trade-offs
What it is
A moving (rolling) average averages each row with a fixed number of rows before it, such as the last 7 days, using a window frame: AVG(revenue) OVER (ORDER BY day ROWS BETWEEN 6 PRECEDING AND CURRENT ROW). It smooths out day-to-day noise so trends are visible.
Why it matters: Daily metrics jump around with weekends, holidays and campaigns; a 7-day average is what most dashboards actually chart. The traps are subtle: missing days make "7 rows" cover more than 7 days, and the first rows average over fewer days than you think. Analytics interviews test both.
When to reach for it
- 7-day and 28-day trend lines on dashboards
- Smoothing noisy daily metrics before comparing periods
- Rolling conversion or error rates from rolling sums
When to avoid or be careful
- Rolling over a series with missing days
- Presenting the first N−1 values as full averages
- Averaging daily rates instead of dividing rolling sums
Ways to compute a rolling average
Strengths: Simple and works in every engine with window functions.
Watch out: Requires filling missing days first.
Strengths: Measures actual days, not rows.
Watch out: Not supported in every database; days with no row still don't count as 0.
Strengths: Works everywhere.
Watch out: Much slower and more code.
Interview signals & numbers to know
When this topic comes up
- “7-day rolling average”
- “Smooth out the weekly pattern”
- Trailing 30 days
- Window frames
- Time-series dashboards
Benchmarks & rules of thumb
- 6 PRECEDING
- + current row = 7-day window
- N − 1
- warm-up rows with partial windows
- PostgreSQL 11
- RANGE with an interval offset
Common mistakes & follow-ups
Common interview mistakes
- Using ROWS 6 PRECEDING on a series where weekends are missing, so the window covers 9 days.
- Writing 7 PRECEDING and getting an 8-day average.
- Reporting a rolling average of daily conversion rates instead of the rolling sum ratio.
Likely interviewer follow-ups
- How do you make sure a 7-day window really covers 7 calendar days?
- How would you compute a 7-day rolling average per product?
- Why is the average of daily conversion rates different from the 7-day conversion rate?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Moving Averages & Rolling Windows (7-Day, 30-Day Rolling Metrics) with hands-on SQL practice and interview Q&A.