How do you build a trailing 7- or 28-day rolling average in SQL, and how do RANGE and ROWS frames differ?

Quick Overview

Asks how to compute a trailing 7- or 28-day rolling average with SQL window functions and how RANGE and ROWS frames behave differently. Tests frame bounds, gaps in daily data, tied dates, partial windows and gap filling with a calendar table.

How do you build a trailing 7- or 28-day rolling average in SQL, and how do RANGE and ROWS frames differ?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

How do you build a rolling time window such as a trailing 7- or 28-day average, and how does a time-based `RANGE` frame differ from a row-count `ROWS` frame here? Assume a table `daily_metrics(series, day, value)` with daily values per series, where some days can be missing. ```hint Frame units Ask what each frame counts in: physical rows, or values of the `ORDER BY` column. Then picture a series with one day missing. ``` ```hint Missing days Decide whether a missing day means zero activity or no data, and check which rows `AVG` actually divides by. ``` ### What a Strong Answer Covers - The window function shape: the aggregate, `PARTITION BY`, `ORDER BY` and a frame whose bounds give exactly 7 or 28 days - How `ROWS` and `RANGE` behave on gaps and on tied order values - What a missing day does to the average, and how a calendar table or a fixed divisor handles it - Partial windows at the start of a series, and how to flag or drop them ### Follow-up Questions - How would you compute a trailing 28-day count of distinct active users, given that PostgreSQL does not allow `COUNT(DISTINCT ...)` as a window function? - How do you return NULL for days that do not yet have a full 7 days of history? - The table holds raw events with timestamps. What changes when you want the 168 hours before each event? - How would you write the same rolling average without window functions?

Overview: Asks how to compute a trailing 7- or 28-day rolling average with SQL window functions and how RANGE and ROWS frames behave differently. Tests frame bounds, gaps in daily data, tied dates, partial windows and gap filling with a calendar table.

PracHub logo
General
Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

How do you build a rolling time window such as a trailing 7- or 28-day average, and how does a time-based RANGE frame differ from a row-count ROWS frame here?

Assume a table daily_metrics(series, day, value) with daily values per series, where some days can be missing.

What a Strong Answer Covers Guidance

  • The window function shape: the aggregate, PARTITION BY , ORDER BY and a frame whose bounds give exactly 7 or 28 days
  • How ROWS and RANGE behave on gaps and on tied order values
  • What a missing day does to the average, and how a calendar table or a fixed divisor handles it
  • Partial windows at the start of a series, and how to flag or drop them

Follow-up Questions Guidance

  • How would you compute a trailing 28-day count of distinct active users, given that PostgreSQL does not allow COUNT(DISTINCT ...) as a window function?
  • How do you return NULL for days that do not yet have a full 7 days of history?
  • The table holds raw events with timestamps. What changes when you want the 168 hours before each event?
  • How would you write the same rolling average without window functions?