001Level 1

Chapter 7 · CTEs

28. Executive Dashboard Metrics with CTEs

Hard40 XPHealthcare

Build an executive dashboard with a single query. Using CTEs, compute: total_patients, total_doctors, total_visits, total_revenue (sum of billing amount), avg_revenue_per_visit (average billing amount per billing record, rounded to 2), and pct_completed_visits (rounded to 1). Return a single row with all metrics.

Tables

  • PKidINT
  • first_nameTEXT
  • last_nameTEXT
  • date_of_birthTEXT
  • genderTEXT
  • emailTEXT
  • phoneTEXT
  • blood_typeTEXT
  • cityTEXT
  • stateTEXT
  • FKinsurance_id→ insurance.idINT
  • created_atTIMESTAMP
  • PKidINT
  • first_nameTEXT
  • last_nameTEXT
  • specialtyTEXT
  • FKdepartment_id→ departments.idINT
  • emailTEXT
  • phoneTEXT
  • hire_dateTEXT
  • salaryREAL
  • PKidINT
  • FKpatient_id→ patients.idINT
  • FKdoctor_id→ doctors.idINT
  • visit_dateTEXT
  • diagnosisTEXT
  • notesTEXT
  • follow_up_dateTEXT
  • statusTEXT
  • PKidINT
  • FKvisit_id→ visits.idINT
  • amountREAL
  • insurance_coveredREAL
  • patient_responsibilityREAL
  • statusTEXT
  • billed_atTEXT
  • paid_atTEXT

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.

Peek at the solution?

You can still earn the XP afterwards by submitting a working query — but you'll learn more if you give it one more try.