Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
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 feedbackCoaching1:1 coaching with people at your target companiesSuccess 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
Rolling Averages With Windows
00:00
5 left

Rolling Averages With Windows

MediumSQL · PostgreSQL

Problem

Write a SQL query using SQL window functions to calculate rolling thirty-day active user averages across multiple regional patient registries.

Use the supplied registry, patient, and activity data. Include every registry for every calendar date in the activity range, treating dates without activity as zero active users.

Output

  1. One row per registry and calendar date, including registries with no activity.
  2. Columns: registry_id, registry_name, region_code, activity_date, active_users, and rolling_30_day_average.
  3. Count distinct active patients with activity on each date. Order by registry_id, then activity_date.

Schema

registries
ColumnTypeDescription
registry_idPKINTUnique patient registry identifier
registry_nameVARCHAR(100)Registry name
region_codeVARCHAR(10)Regional code for the registry
patients
ColumnTypeDescription
patient_idPKINTUnique patient identifier
registry_idINTRegistry associated with the patient
statusVARCHAR(20)Patient participation status
activity_events
ColumnTypeDescription
event_idPKINTUnique activity event identifier
patient_idINTPatient associated with the activity
activity_dateDATEDate of patient activity
Tablesregistriespatientsactivity_events
Interviewer

Your question is Rolling Averages With Windows. Start with the requirements and the three tables 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.