Chapter 6 · Subqueries
45. Most Common Diagnosis per Department
Medium20 XPHealthcare
For each department, find the most frequently occurring diagnosis. Return the department name, the top diagnosis, and its count. If several diagnoses tie for the top count, pick the one that comes first alphabetically.
Tables
- PKidINT
- first_nameTEXT
- last_nameTEXT
- specialtyTEXT
- FKdepartment_id→ departments.idINT
- emailTEXT
- phoneTEXT
- hire_dateTEXT
- salaryREAL
- PKidINT
- nameTEXT
- floorINT
- FKhead_doctor_id→ doctors.idINT
- budgetREAL
- PKidINT
- FKpatient_id→ patients.idINT
- FKdoctor_id→ doctors.idINT
- visit_dateTEXT
- diagnosisTEXT
- notesTEXT
- follow_up_dateTEXT
- statusTEXT
Need help?
Ask Sol
AI tutor — looks at your current query and nudges you, without spoiling the answer.
Solution
Try the hints first — solving it yourself is what makes it stick.