0
0
SQLquery~10 mins

Date range overlap detection in SQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Date range overlap detection
Start with two date ranges
Check if start1 <= end2
Yes
Check if end1 >= start2
Yes
Date ranges overlap
No
No overlap
End
We check if two date ranges overlap by verifying if the start of one range is before or on the end of the other, and vice versa.
Execution Sample
SQL
SELECT * FROM events e1
JOIN events e2 ON e1.id != e2.id
WHERE e1.start_date <= e2.end_date
  AND e1.end_date >= e2.start_date;
This query finds pairs of events where their date ranges overlap.
Execution Table
Stepe1.start_datee1.end_datee2.start_datee2.end_dateCondition e1.start_date <= e2.end_dateCondition e1.end_date >= e2.start_dateOverlap?
12024-01-012024-01-102024-01-052024-01-15TrueTrueYes
22024-01-012024-01-102024-01-112024-01-20TrueFalseNo
32024-02-012024-02-102024-01-252024-02-05TrueTrueYes
42024-03-012024-03-102024-03-112024-03-20TrueFalseNo
52024-04-012024-04-102024-03-252024-04-01TrueTrueYes
62024-05-012024-05-102024-05-112024-05-20FalseFalseNo
Exit------No more pairs to check
💡 All pairs checked; no more rows to compare.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6Final
e1.start_date2024-01-012024-01-012024-02-012024-03-012024-04-012024-05-01--
e1.end_date2024-01-102024-01-102024-02-102024-03-102024-04-102024-05-10--
e2.start_date2024-01-052024-01-112024-01-252024-03-112024-03-252024-05-11--
e2.end_date2024-01-152024-01-202024-02-052024-03-202024-04-012024-05-20--
Condition e1.start_date <= e2.end_dateTrueTrueTrueTrueTrueFalse--
Condition e1.end_date >= e2.start_dateTrueFalseTrueFalseTrueFalse--
Overlap?YesNoYesNoYesNo--
Key Moments - 2 Insights
Why do we check both conditions e1.start_date <= e2.end_date and e1.end_date >= e2.start_date?
Because both conditions together ensure the date ranges actually overlap. Checking only one condition can give false positives. See execution_table rows 2 and 4 where one condition is true but the other is false, so no overlap.
Why do we exclude pairs where e1.id = e2.id in the join?
Because an event cannot overlap with itself. The condition e1.id != e2.id avoids comparing the same row to itself, which would always overlap.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at step 3, what is the value of condition e1.end_date >= e2.start_date?
AUnknown
BTrue
CFalse
DNot checked
💡 Hint
Check the column 'Condition e1.end_date >= e2.start_date' at row 3 in execution_table.
At which step does the overlap condition evaluate to No because e1.end_date < e2.start_date?
AStep 2
BStep 1
CStep 5
DStep 6
💡 Hint
Look for rows where 'Overlap?' is No and 'Condition e1.end_date >= e2.start_date' is False.
If e1.start_date was after e2.end_date, what would happen to the overlap condition?
AOverlap would be Unknown
BOverlap would be Yes
COverlap would be No
DOverlap would depend on other conditions
💡 Hint
Refer to the first condition 'e1.start_date <= e2.end_date' in the concept_flow and execution_table.
Concept Snapshot
Date range overlap detection:
Check if two ranges overlap by verifying:
start1 <= end2 AND end1 >= start2
If both true, ranges overlap.
Use in SQL JOIN conditions to find overlapping rows.
Full Transcript
This visual execution shows how to detect if two date ranges overlap in SQL. We compare two date ranges by checking if the start of the first range is on or before the end of the second, and if the end of the first range is on or after the start of the second. The execution table walks through example pairs of dates, showing which conditions are true or false and whether the ranges overlap. The variable tracker shows how the dates and conditions change step by step. Key moments clarify why both conditions are needed and why we exclude comparing the same row. The quiz tests understanding of the conditions and their results. This method helps find overlapping events or bookings in databases.