Senior
Intervals
SQL & Databases
How do you find overlapping date ranges?
Short answer: Two ranges [a,b] and [c,d] overlap when a <= d AND c <= b (for closed intervals). Self-join bookings/events with that predicate and exclude the same Id.
Sample solution
T-SQL
SELECT a.Id AS Id1, b.Id AS Id2
FROM Bookings a
INNER JOIN Bookings b ON a.Id < b.Id
AND a.StartDate <= b.EndDate
AND b.StartDate <= a.EndDate;
Clarify inclusive/exclusive end times — hotel checkout logic often uses half-open intervals.
Share this Q&A
Share preview image: https://www.toolliyo.com/images/toolliyo-logo.png