Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Join Patient Records and Aggregate

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

Your question is Join Patient Records and Aggregate. 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

Novo Nordisk's clinical analytics team needs a patient-level summary of diagnostic activity from its clinical data platform. Write a PostgreSQL query that joins patient records with diagnostic codes and aggregates confirmed diagnoses recorded during 2025.

Requirements

  1. Return every patient, including patients with no qualifying diagnoses.
  2. Count qualifying diagnosis records and count distinct diagnostic codes per patient.
  3. Return diagnostic codes as a comma-separated, alphabetically ordered list, or NULL when no code matches.
  4. Return the most recent qualifying diagnosis date for each patient, ordered by patient ID.

Apply the date and status conditions without removing patients who have no qualifying records.

Schema

patients
ColumnTypeDescription
patient_idPKINTUnique patient identifier
patient_nameVARCHAR(100)Patient display name
patient_diagnoses
ColumnTypeDescription
diagnosis_idPKINTUnique diagnosis record identifier
patient_idINTReferences patients.patient_id
diagnostic_code_idINTReferences diagnostic_codes.diagnostic_code_id
diagnosed_onDATEDate on which the diagnosis was recorded
statusVARCHAR(20)Diagnosis workflow status
diagnostic_codes
ColumnTypeDescription
diagnostic_code_idPKINTUnique diagnostic code identifier
codeVARCHAR(20)Standardized diagnostic code
descriptionVARCHAR(200)Human-readable code description
Tablespatientspatient_diagnosesdiagnostic_codes
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results