Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Top Diagnoses Per Hospital
00:00
5 left

Top Diagnoses Per Hospital

MediumSQL · PostgreSQL

Problem

Write a SQL query to find the top 3 most frequent diagnoses per hospital.

Use the provided hospital and patient diagnosis data. Exclude records with a NULL diagnosis. If frequencies tie, order diagnoses alphabetically to produce a deterministic top-three result.

Output

  1. One row per selected hospital and diagnosis, with columns hospital_name, diagnosis, diagnosis_count, and diagnosis_rank.
  2. Include only the top three diagnoses for each hospital, ordered by hospital name and rank.

Schema

hospitals
ColumnTypeDescription
hospital_idPKINTUnique hospital identifier
hospital_nameVARCHAR(100)Hospital name
regionVARCHAR(50)Hospital service region
patient_diagnoses
ColumnTypeDescription
diagnosis_idPKINTUnique diagnosis record identifier
hospital_idINTHospital associated with the diagnosis record
diagnosisVARCHAR(100)Recorded patient diagnosis
diagnosed_onDATEDate the diagnosis was recorded
Tableshospitalspatient_diagnoses
Interviewer

Your question is Top Diagnoses Per Hospital. 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.