Introduction
We use date range overlap detection to find if two time periods share any common days. This helps avoid scheduling conflicts or double bookings.
SELECT * FROM table1 t1 JOIN table2 t2 ON t1.start_date <= t2.end_date AND t1.end_date >= t2.start_date;
SELECT * FROM bookings b1 JOIN bookings b2 ON b1.start_date <= b2.end_date AND b1.end_date >= b2.start_date WHERE b1.id != b2.id;
SELECT * FROM events e1 JOIN events e2 ON e1.start <= e2.finish AND e1.finish >= e2.start WHERE e1.event_id <> e2.event_id;
CREATE TABLE reservations ( id INT, start_date DATE, end_date DATE ); INSERT INTO reservations VALUES (1, '2024-06-01', '2024-06-05'), (2, '2024-06-04', '2024-06-10'), (3, '2024-06-11', '2024-06-15'); SELECT r1.id AS res1, r2.id AS res2 FROM reservations r1 JOIN reservations r2 ON r1.start_date <= r2.end_date AND r1.end_date >= r2.start_date WHERE r1.id < r2.id;
SELECT * FROM bookings WHERE start_date <= '2024-05-10' AND end_date >= '2024-05-05';(start1, end1) and (start2, end2)?events(event_id, start_date, end_date) with data:1, '2024-05-01', '2024-05-05'
2, '2024-05-04', '2024-05-10'
3, '2024-05-11', '2024-05-15'SELECT * FROM events WHERE start_date <= '2024-05-06' AND end_date >= '2024-05-04';SELECT * FROM bookings WHERE start_date > '2024-05-01' AND end_date < '2024-05-10';reservations(id, start_date, end_date). Write a query to find all reservations that overlap with a new booking from '2024-06-10' to '2024-06-15'. Which query correctly detects overlaps including edge cases?Pass the test and this pattern moves unknown