209. Cost Efficiency by Specialty

Hard Efficiency Analysis
Query Rules
  • The query columns and order must match the expected result schema.
  • NULL values should be handled appropriately where applicable.
  • The grader compares output values and sorting order to evaluate.

Schema Browser

Click a table's header to toggle expand/collapse.
patients
10 cols
doctors
4 cols
admissions
7 cols
province_names
2 cols

Reference Solution

Expected SQL
SELECT d.specialty, COUNT(DISTINCT a.patient_id) AS total_patients, ROUND(AVG(JULIANDAY(a.discharge_date) - JULIANDAY(a.admission_date)), 2) AS avg_length_of_stay, ROUND(AVG(a.admission_cost / NULLIF(JULIANDAY(a.discharge_date) - JULIANDAY(a.admission_date), 0)), 2) AS avg_cost_per_day, RANK() OVER (ORDER BY AVG(a.admission_cost / NULLIF(JULIANDAY(a.discharge_date) - JULIANDAY(a.admission_date), 0))) AS efficiency_rank
FROM doctors d
JOIN admissions a ON d.doctor_id = a.attending_doctor_id
WHERE a.discharge_date IS NOT NULL
GROUP BY d.specialty
HAVING COUNT(DISTINCT a.patient_id) >= 10
ORDER BY avg_cost_per_day;
SQL Editor
Ctrl+Enter to run
Console Output

Console Terminal

Write your query and click "Run Query" (Ctrl + Enter) to see results and testcase validation.