Write a query to find all patients who missed at least two scheduled appointments in the past year.

Answer

Count missed ("no-show") appointments per patient within the past year, then keep patients with at least two.

Solution

SELECT patient_id, COUNT(*) AS missed_count
FROM Appointments
WHERE status = 'no-show'
  AND appointment_date >= CURRENT_DATE - INTERVAL '1 year'
GROUP BY patient_id
HAVING COUNT(*) >= 2;

Step by Step

  1. WHERE status = 'no-show' filters to only missed appointments, and the date condition restricts to the trailing 12 months.
  2. GROUP BY patient_id collapses each patient's missed appointments into one row.
  3. HAVING COUNT(*) >= 2 keeps only patients who missed two or more appointments in that window.

Alternative: Distinguishing "Missed" from "Cancelled"

SELECT patient_id, COUNT(*) AS missed_count
FROM Appointments
WHERE status IN ('no-show', 'missed')
  AND appointment_date >= CURRENT_DATE - INTERVAL '1 year'
GROUP BY patient_id
HAVING COUNT(*) >= 2;

Real scheduling systems often distinguish "no-show" (patient didn't cancel and didn't attend) from "cancelled" (patient proactively cancelled, sometimes with notice) - these represent very different behaviors, and only genuine no-shows/missed visits should typically count toward this kind of flag, not late cancellations.

Key Exam Traps

  • "Missed" vs "cancelled" is the key business distinction here - conflating the two status values would unfairly flag patients who gave proper notice alongside those who simply didn't show up, a meaningfully different clinical/operational signal.
  • "At least two" means >= 2, not > 2 - a common off-by-one mistake when translating "at least" into a comparison operator.
  • Same-day duplicate appointment records: If a rescheduled appointment creates two rows for what was really one missed visit, this query could double-count - worth checking whether the appointment table has a way to distinguish original bookings from reschedules.
Written by Lorens Mishra

Software Engineer Notes Management System Administrator

Discussion

0 comments
Sign in to join the discussion.

No comments yet. Be the first to say something.