runtime
135 / 139

task Self joins · medium

Encounters that overlap in time

Return patient_id, first_encounter, second_encounter (first_encounter < second_encounter) and overlap_minutes for every pair of the same patient’s encounters whose [start_at, end_at) intervals overlap.

what to return

Columns
patient_idfirst_encountersecond_encounteroverlap_minutes in this order
Rows
158 rows · any order
Skill
Detect overlapping intervals for the same entity. Refresher: Self-joins & referral relationships →

expected result

Your query should return exactly these columns and values. Run it and compare.

expected result158 rows · any order
patient_idfirst_encountersecond_encounteroverlap_minutes
96627628139
11673173275
125792793141

first 3 of 158 rows

data you can query

Healthcare dataset: the first 6 rows of each table. Under each column name: the kind of value it holds, and which table it links to. Your queries run on all rows.

encounters9 columns · 20,447 rows
encounter_idnumber · unique idpatient_idnumber · links to patientsprovider_idnumber · links to providersencounter_classtextstart_atdate & timeend_atdate & timereason_codetexttotal_costdecimalpayer_paiddecimal
114ambulatory2022-08-07 13:00:002022-08-07 13:24:45T78.40XA175.6159.38
214telehealth2022-10-12 11:15:002022-10-12 11:59:48E11.9110.0295.53
3174telehealth2023-05-12 11:30:002023-05-12 12:13:27E11.983.6874.77
41349ambulatory2023-12-17 08:15:002023-12-17 08:39:33E11.9198.23187.3
514telehealth2024-05-09 16:45:002024-05-09 17:29:01E66.9115.0898.4
614ambulatory2024-09-07 13:00:002024-09-07 13:27:30E66.9153.61142.99
encounter_idnumber · unique id
1
patient_idnumber · links to patients
1
provider_idnumber · links to providers
4
encounter_classtext
ambulatory
start_atdate & time
2022-08-07 13:00:00
end_atdate & time
2022-08-07 13:24:45
reason_codetext
T78.40XA
total_costdecimal
175.6
payer_paiddecimal
159.38
encounter_idnumber · unique id
2
patient_idnumber · links to patients
1
provider_idnumber · links to providers
4
encounter_classtext
telehealth
start_atdate & time
2022-10-12 11:15:00
end_atdate & time
2022-10-12 11:59:48
reason_codetext
E11.9
total_costdecimal
110.02
payer_paiddecimal
95.53
encounter_idnumber · unique id
3
patient_idnumber · links to patients
1
provider_idnumber · links to providers
74
encounter_classtext
telehealth
start_atdate & time
2023-05-12 11:30:00
end_atdate & time
2023-05-12 12:13:27
reason_codetext
E11.9
total_costdecimal
83.68
payer_paiddecimal
74.77
encounter_idnumber · unique id
4
patient_idnumber · links to patients
1
provider_idnumber · links to providers
349
encounter_classtext
ambulatory
start_atdate & time
2023-12-17 08:15:00
end_atdate & time
2023-12-17 08:39:33
reason_codetext
E11.9
total_costdecimal
198.23
payer_paiddecimal
187.3
encounter_idnumber · unique id
5
patient_idnumber · links to patients
1
provider_idnumber · links to providers
4
encounter_classtext
telehealth
start_atdate & time
2024-05-09 16:45:00
end_atdate & time
2024-05-09 17:29:01
reason_codetext
E66.9
total_costdecimal
115.08
payer_paiddecimal
98.4
encounter_idnumber · unique id
6
patient_idnumber · links to patients
1
provider_idnumber · links to providers
4
encounter_classtext
ambulatory
start_atdate & time
2024-09-07 13:00:00
end_atdate & time
2024-09-07 13:27:30
reason_codetext
E66.9
total_costdecimal
153.61
payer_paiddecimal
142.99

first 6 of 20,447 rows · your queries use all of them

solution.sql
 

Hints and the answer are always available. Using them counts the question as solved with help.

Encounters that overlap in time — SQL practice · Runtime