Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Window Functions Rolling Comparison
00:00
5 left

Window Functions Rolling Comparison

HardSQL · PostgreSQL

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
Interviewer

Your question is Window Functions Rolling Comparison. Start with the requirements and the one table in the Question tab.

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.
CodePostgreSQL
You need to log in / sign up to run or submit.Ln 1
Run your query to see results here.