Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

7-Day Rolling Revenue with Window Functions

MediumSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is 7-Day Rolling Revenue with Window Functions. Start with the requirements and the one table on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Write a SQL query using window functions to calculate a 7-day rolling average of revenue by customer.

Use revenue_events as the source table. Combine multiple events for the same customer and date into one daily total, and exclude rows with a null customer_id.

Output

  1. One row per customer and revenue date with columns customer_id, revenue_date, daily_revenue, and rolling_7_day_average.
  2. The average must cover the current date and preceding six calendar days for that customer.
  3. Order by customer_id, then revenue_date.

Schema

revenue_events
ColumnTypeDescription
event_idPKINTUnique revenue event identifier
customer_idINTCustomer associated with the revenue event
revenue_dateDATECalendar date on which revenue was recorded
revenueNUMERIC(12,2)Revenue amount for the event
Tablesrevenue_events
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results