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_datedaily_revenue
2025-01-011000.0
2025-01-021200.0
2025-01-03900.0

Keep going