001Level 1

Chapter 7 · CTEs

26. Patient Journey Summary with CTEs

Hard40 XPHealthcare

Build a patient journey summary using CTEs. For each patient, show: total visits, total prescriptions, total lab tests, total billed amount, and days since first visit (as of '2025-01-01'). Return patient full name and all metrics. Only include patients with at least one visit. Order by total_billed descending.

Tables

  • PKidINT
  • first_nameTEXT
  • last_nameTEXT
  • date_of_birthTEXT
  • genderTEXT
  • emailTEXT
  • phoneTEXT
  • blood_typeTEXT
  • cityTEXT
  • stateTEXT
  • FKinsurance_id→ insurance.idINT
  • created_atTIMESTAMP
  • PKidINT
  • FKpatient_id→ patients.idINT
  • FKdoctor_id→ doctors.idINT
  • visit_dateTEXT
  • diagnosisTEXT
  • notesTEXT
  • follow_up_dateTEXT
  • statusTEXT
  • PKidINT
  • FKvisit_id→ visits.idINT
  • medicationTEXT
  • dosageTEXT
  • frequencyTEXT
  • start_dateTEXT
  • end_dateTEXT
  • refillsINT
  • PKidINT
  • FKvisit_id→ visits.idINT
  • test_nameTEXT
  • result_valueTEXT
  • unitTEXT
  • reference_rangeTEXT
  • is_abnormalINT
  • tested_atTEXT
  • 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.