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
SQL for Patient Data Extraction
00:00
5 left

SQL for Patient Data Extraction

MediumSQL · PostgreSQL

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
Interviewer

Your question is SQL for Patient Data Extraction. 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.