Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Window Functions Rolling Comparison

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

Your question is Window Functions Rolling Comparison. 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 compute a 7-day rolling average and compare it to the prior week for each user.

Assume user_daily_activity contains one row per user per calendar day. Return only dates where a prior-week comparison is available.

Output

  1. One row per user and activity date
  2. Columns: user_id, activity_date, rolling_7_day_avg, prior_week_rolling_avg, and difference_from_prior_week
  3. Include the current rolling average minus the prior-week rolling average as the difference
  4. Order by user_id, then activity_date

Schema

user_daily_activity
ColumnTypeDescription
activity_idPKINTUnique activity record identifier
user_idVARCHAR(50)User associated with the daily activity
activity_dateDATECalendar date of the activity measurement
activity_valueDECIMAL(12,2)Daily activity measure
Tablesuser_daily_activity
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results