Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Overlapping Appointments

MediumSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is Overlapping Appointments. Start with the requirements and the three tables on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Providence uses appointment data from its electronic health record to identify patients who were scheduled for overlapping encounters. Write a PostgreSQL query that reports each overlapping appointment pair for March 10, 2025.

Requirements

  1. Consider only appointments with status scheduled, checked_in, or completed.
  2. Treat two appointments as overlapping when one starts before the other ends and the other starts before the first ends. Appointments that only touch at an endpoint do not overlap.
  3. Return each pair once, excluding self-pairs and reversed duplicates.
  4. Include patient details, both appointment intervals, both provider names, and the overlap duration in minutes.
  5. Preserve an overlap even when a provider is missing, and sort by patient ID, first appointment start time, and second appointment start time.

Schema

patients
ColumnTypeDescription
patient_idPKINTUnique patient identifier
first_nameVARCHAR(50)Patient first name
last_nameVARCHAR(50)Patient last name
providers
ColumnTypeDescription
provider_idPKINTUnique provider identifier
provider_nameVARCHAR(100)Provider display name
specialtyVARCHAR(80)Clinical specialty
appointments
ColumnTypeDescription
appointment_idPKINTUnique appointment identifier
patient_idINTPatient receiving the appointment
provider_idINTAssigned provider
appointment_dateDATECalendar date of the appointment
start_timeTIMESTAMPAppointment start timestamp
end_timeTIMESTAMPAppointment end timestamp
statusVARCHAR(20)Appointment lifecycle status
Tablesappointmentspatientsproviders
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results