DataLane

11. Seven-day moving average

hardProNot started~14 minWindow functionsTime series

Asked in screens shaped like: Netflix, Spotify, Uber

Daily revenue is noisy, so the dashboard smooths it over a trailing week. The frame has to include the current day and the six before it — off-by-one here is the most common review comment on this query.

Requirements

  • Columns: order_day, revenue, avg_7d
  • Only status = 'completed'
  • avg_7d averages the current day plus the 6 preceding days
  • Both numeric columns rounded to 2 decimals, ordered by order_day

Expected output

order_day, revenue, avg_7d

Row order is graded, so ORDER BY matters here.

SQL editor

Loading SQL engine…

Ctrl/Cmd + Enter submits

Run your query to see the result set.

Pro problem

Hard problems unlock with Pro

Easy pads stay free. Medium and hard SQL and Python problems — editor, tests, hints, and solutions — open after you upgrade to Pro or coaching.

See plansPractice free easy problems
↑↓ navigate openesc close