47. Identify Premium Patient Cohorts

Hard Uber CTEs & Advanced Subqueries
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
WITH PatientTotals AS (SELECT patient_id, SUM(admission_cost) as total_cost FROM admissions GROUP BY patient_id) SELECT p.first_name, p.last_name, pt.total_cost FROM patients p JOIN PatientTotals pt ON p.patient_id = pt.patient_id WHERE pt.total_cost > 1.5 * (SELECT AVG(total_cost) FROM PatientTotals);
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.