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 30-Day Event Counts
00:00
5 left

Rolling 30-Day Event Counts

HardSQL · PostgreSQL

Problem

Write a SQL query using window functions to calculate each patient's rolling 30-day event count and rank the most recent events.

Return one row for every recorded event. The rolling count includes the event itself and other events for the same patient from the preceding 30 days through the event timestamp. Rank events separately for each patient by event timestamp descending, allowing events at the same timestamp to share a rank.

Output

  1. Return patient_id, patient_name, event_id, event_timestamp, event_type_name, rolling_30_day_event_count, and recency_rank.
  2. Sort by patient_id, recency_rank, then event_id.

Schema

patients
ColumnTypeDescription
patient_idPKINTUnique patient identifier
patient_nameVARCHAR(100)Patient display name
date_of_birthDATEPatient date of birth
patient_events
ColumnTypeDescription
event_idPKINTUnique event identifier
patient_idINTPatient associated with the event
event_timestampTIMESTAMPTimestamp when the event occurred
event_type_idINTOptional event type identifier
event_types
ColumnTypeDescription
event_type_idPKINTUnique event type identifier
event_type_nameVARCHAR(100)Human-readable event type
Tablespatientspatient_eventsevent_types
Interviewer

Your question is Rolling 30-Day Event Counts. 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.