KatInterview

Rebooking Rate After Initial Cancellation

SQL & DataAuto-graded
MediumAuto-gradedSQL & Datasqlwindow-functionscte

Last updated

Calculate what fraction of users whose earliest appointment was cancelled never scheduled another confirmed or returned appointment afterward.

You work with a dataset tracking appointment scheduling behavior.

Given a table with appointment records:

Table: appointments

  • appointment_id (BIGINT, primary key)
  • user_id (BIGINT, not null)
  • scheduled_start_at (TIMESTAMP, not null) — planned start time
  • status (VARCHAR, not null) — final outcome: 'cancelled', 'returned', 'confirmed', 'completed', 'no_show'
  • status_updated_at (TIMESTAMP, not null) — timestamp when status was finalized

Rules

  • Users may have multiple appointments
  • A user's earliest appointment is determined by the minimum scheduled_start_at; if multiple appointments share that timestamp, use the smallest appointment_id
  • A user falls into the initial cancellation cohort if their earliest appointment has status = 'cancelled'
  • A user rebooked if they have any appointment with scheduled_start_at strictly greater than their earliest appointment's start time AND status IN ('returned', 'confirmed')

Your task

Write SQL to determine what percentage of users in the initial cancellation cohort never rebooked.

Return one row with:

  • first_cancelled_users (integer) — total users in cohort
  • never_returned_confirmed_users (integer) — cohort members who never rebooked
  • pct_never_returned_confirmed (decimal, 0 to 1) — the ratio

Use CTEs and window functions as needed.

Hints

Hint 1

Use ROW_NUMBER with a two-part ordering to identify each user's earliest appointment deterministically

Hint 2

Apply the cancelled filter after identifying the earliest appointment, not before

Hint 3

Use EXISTS or a LEFT JOIN with COUNT to check for rebooking without creating duplicate rows per user