Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
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
Sign up free to run your codeLog inLn 1
Run your query to see results here.