SQL interview question: 7-Day Moving Average
7-Day Moving Average of Sales
Finance wants to smooth out daily sales volatility by calculating a 7-day rolling average.
Calculate the 7-day moving average of daily revenue. Only show dates with a full 7 days of data.
Return sale_date, daily_revenue, and avg_7_days (rounded to 2 decimal places). Use ROWS BETWEEN 6 PRECEDING AND CURRENT ROW.
Skills: window function with ROWS BETWEEN
Tables
daily_sales 15 rows
- sale_date text PK
- daily_revenue real
| sale_date | daily_revenue |
|---|---|
| 2025-01-01 | 1000.0 |
| 2025-01-02 | 1200.0 |
| 2025-01-03 | 900.0 |