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
Patient Metrics with Window Functions
00:00
5 left

Patient Metrics with Window Functions

MediumSQL · PostgreSQL

Problem

Given a highly normalized database schema, write a SQL query to extract a specific set of patient metrics, utilizing window functions.

Use the normalized patient, facility, and encounter data. Include all patients and calculate metrics for encounters beginning in calendar year 2025.

Output

  1. One row per patient, including patients with no qualifying encounters.
  2. Columns: patient_id, patient_name, facility_id, encounter_count, average_length_hours, latest_encounter_date, and facility_encounter_rank.
  3. Rank patients within each facility by encounter count descending, with ties sharing a rank.
  4. Order by facility, rank, and patient ID, placing patients without a facility last.

Schema

facilities
ColumnTypeDescription
facility_idPKINTUnique facility identifier
facility_nameVARCHAR(100)Facility display name
patients
ColumnTypeDescription
patient_idPKINTUnique patient identifier
first_nameVARCHAR(50)Patient first name
last_nameVARCHAR(50)Patient last name
facility_idINTAssigned facility identifier
encounters
ColumnTypeDescription
encounter_idPKINTUnique encounter identifier
patient_idINTPatient associated with the encounter
facility_idINTFacility where the encounter occurred
admit_tsTIMESTAMPEncounter start timestamp
discharge_tsTIMESTAMPEncounter discharge timestamp
Tablespatientsfacilitiesencounters
Interviewer

Your question is Patient Metrics with Window Functions. 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.