Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
SQL for Unique Patient Records
00:00
5 left

SQL for Unique Patient Records

MediumSQL · PostgreSQL

Problem

Write a SQL query to extract unique patient records from a clinical database.

Use the patients and clinical_records tables. Return only patients with at least one clinical record, selecting the most recent record for each patient. If timestamps tie, use the larger record_id as the tie-breaker.

Output

  1. One row per patient with a clinical record
  2. Columns: patient_id, full_name, recorded_at, diagnosis, record_status
  3. Sort by patient_id in ascending order

Schema

patients
ColumnTypeDescription
patient_idPKINTUnique patient identifier
full_nameVARCHAR(100)Patient's full name
date_of_birthDATEPatient date of birth
sexVARCHAR(20)Recorded patient sex
clinical_records
ColumnTypeDescription
record_idPKINTUnique clinical record identifier
patient_idINTPatient associated with the clinical record
recorded_atTIMESTAMPDate and time the record was created
diagnosisVARCHAR(150)Diagnosis documented in the record
record_statusVARCHAR(30)Current status of the clinical record
Tablespatientsclinical_records
Interviewer

Your question is SQL for Unique Patient Records. Start with the requirements and the two 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
You need to log in / sign up to run or submit.Ln 1
Run your query to see results here.