Chapter 8 · Window Functions
57. Days Until Next Visit per Patient
Hard40 XPHealthcare
For each patient's visits, compute the number of days until their next visit using LEAD(). Return patient_id, visit_date, next_visit_date, and days_until_next. Order by patient_id, visit_date.
Tables
- PKidINT
- FKpatient_id→ patients.idINT
- FKdoctor_id→ doctors.idINT
- visit_dateTEXT
- diagnosisTEXT
- notesTEXT
- follow_up_dateTEXT
- statusTEXT
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.