Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL for Patient Data Extraction

MediumSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is SQL for Patient Data Extraction. Start with the requirements and the three tables on the right.

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.

Problem

Grady Health System needs a patient-level summary of active patients who had multiple completed visits during calendar year 2025. Write a PostgreSQL query using the provided tables.

Requirements

  1. Include only patients whose status is Active and whose visits were completed between January 1 and December 31, 2025.
  2. Return the patient identifier, MRN, full name, completed visit count, total charges, latest visit date, and department for the latest visit.
  3. Include only patients with at least two qualifying visits, and sort by total charges descending, then patient ID ascending.

Use a CTE with ROW_NUMBER() to identify each patient's latest qualifying visit. A patient whose latest visit has no department should still appear with a NULL department.

Schema

patients
ColumnTypeDescription
patient_idPKINTUnique patient identifier
mrnVARCHAR(20)Medical record number
first_nameVARCHAR(50)Patient first name
last_nameVARCHAR(50)Patient last name
date_of_birthDATEPatient date of birth
sexVARCHAR(20)Recorded sex
statusVARCHAR(20)Patient status
visits
ColumnTypeDescription
visit_idPKINTUnique visit identifier
patient_idINTReferenced patient
department_idINTReferenced department
visit_dateDATEDate of service
visit_typeVARCHAR(30)Visit type
visit_statusVARCHAR(20)Visit completion status
chargeNUMERIC(10,2)Billed charge
departments
ColumnTypeDescription
department_idPKINTUnique department identifier
department_nameVARCHAR(100)Grady Health System department name
Tablespatientsvisitsdepartments
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results