Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Hospital Cost Drivers

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

Your question is SQL Hospital Cost Drivers. Start with the requirements and the two 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

Huron Consulting Group is reviewing hospital spending for a client engagement. Write a PostgreSQL query that analyzes 2025 expenses by hospital department and identifies each department's largest cost driver.

Requirements

  1. Include every department, including departments with no qualifying 2025 expenses.
  2. Aggregate spending by department and expense category, treating NULL amounts as zero and NULL categories as Unclassified.
  3. Use a window function to rank cost categories within each department and return the largest driver. Return tied largest categories when applicable.
  4. Order the final results by total spending descending, then department name.

Schema

departments
ColumnTypeDescription
department_idPKINTUnique hospital department identifier
department_nameVARCHAR(100)Hospital department name
cost_centerVARCHAR(30)Financial cost center code
expenses
ColumnTypeDescription
expense_idPKINTUnique expense identifier
department_idINTDepartment associated with the expense
expense_categoryVARCHAR(80)Expense classification
expense_dateDATEDate the expense was incurred
amountNUMERIC(12,2)Expense amount in dollars
vendorVARCHAR(100)Expense vendor
Tablesdepartmentsexpenses
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results