A guest wants room 101 from 3 to 5 December. Is it free? A doctor has two appointments; do they clash? Two staff leave requests; do they overlap? These are all the same question: do two date ranges overlap? There are many ways two ranges can sit against each other, but one short rule covers them all.
In this lesson you will learn
- The one-line overlap test
- Why “half-open” ranges make life easier
- How to check whether a new booking is free
- How to find every double booking with a self join
- How to count the overlap length
You should know date functions and the self join.
The Overlap Rule
Two ranges A and B overlap when A starts before B ends, and B starts before A ends:
a_start < b_end AND b_start < a_end
It is easier to see why by thinking of the opposite. Two ranges do not overlap only if A ends before B starts, or B ends before A starts. Flip that statement and you get the rule above. It handles every case: partial overlap at either end, one range inside the other, and identical ranges.
Half-open ranges
Store the end as the first moment that is not included: the check-out day, or the time the slot ends. A stay from 1 to 4 December covers the nights of the 1st, 2nd and 3rd. Then a guest leaving on the 4th and another arriving on the 4th do not clash, and the rule uses plain < with no awkward “minus one day”.
Is the Room Free?
The example uses a small bookings table with a room, a guest, a check_in and a check_out. Dates are stored as YYYY-MM-DD text, which sorts and compares correctly.
-- Is room 101 free from 3 to 5 December (leaving on the 5th)?
SELECT id, guest, check_in, check_out
FROM bookings
WHERE room = '101'
AND check_in < '2026-12-05' -- existing stay starts before the new one ends
AND check_out > '2026-12-03' -- and ends after the new one starts
ORDER BY check_in;
Output
+----+-------+------------+------------+
| id | guest | check_in | check_out |
+----+-------+------------+------------+
| 1 | Karim | 2026-12-01 | 2026-12-04 |
| 2 | Rina | 2026-12-04 | 2026-12-06 |
+----+-------+------------+------------+
(2 rows)
Two existing stays overlap the requested 3–5 December, so the room is not free. Karim is still there on the night of the 3rd, and Rina arrives on the 4th. If the query had returned no rows, the booking could go ahead. In a real system, run this check and the INSERT inside one transaction, so two people cannot book the same gap at once.
Finding All Double Bookings
-- Find every double booking: two stays in the same room that overlap
SELECT a.room,
a.guest AS guest_1, a.check_in AS in_1, a.check_out AS out_1,
b.guest AS guest_2, b.check_in AS in_2, b.check_out AS out_2
FROM bookings AS a
JOIN bookings AS b
ON a.room = b.room
AND a.id < b.id
AND a.check_in < b.check_out
AND b.check_in < a.check_out;
-- Nights each stay overlaps with the 1 to 7 December report window
SELECT guest, room,
MAX(0, julianday(MIN(check_out, '2026-12-08')) - julianday(MAX(check_in, '2026-12-01'))) AS nights_in_window
FROM bookings
ORDER BY room, check_in;
Output
+------+---------+------------+------------+---------+------------+------------+
| room | guest_1 | in_1 | out_1 | guest_2 | in_2 | out_2 |
+------+---------+------------+------------+---------+------------+------------+
| 101 | Rina | 2026-12-04 | 2026-12-06 | Joya | 2026-12-05 | 2026-12-08 |
+------+---------+------------+------------+---------+------------+------------+
(1 row)
+-------+------+------------------+
| guest | room | nights_in_window |
+-------+------+------------------+
| Karim | 101 | 3.0 |
| Rina | 101 | 2.0 |
| Joya | 101 | 3.0 |
| Tuhin | 102 | 6.0 |
| Mitu | 102 | 0 |
+-------+------+------------------+
(5 rows)
- The self join compares every pair of bookings for the same room.
a.id < b.idlists each pair once. - Rina (4–6 Dec) and Joya (5–8 Dec) clash on the night of the 5th. Karim leaving on the 4th and Rina arriving on the 4th is fine, and the half-open rule correctly ignores it.
- The second query clips each stay to the report window from 1 to 8 December (exclusive) with
MAXof the starts andMINof the ends, then counts the nights.MAX(0, …)turns a negative length, meaning no overlap, into 0, as for Mitu.
PostgreSQL has built-in range types and an && “overlaps” operator, and it can even forbid overlapping bookings with an exclusion constraint. In other databases, the rule above is the standard approach.
Common Mistakes
- Checking only whether the start falls inside the other range. That misses a range that completely covers the other.
- Mixing inclusive and exclusive ends. Decide once, ideally half-open, and use it everywhere.
- Comparing dates stored as DD-MM-YYYY text. Text comparison only works for YYYY-MM-DD.
- Forgetting to filter cancelled bookings before checking for clashes.
Practice Exercises
Exercise 1. Is room 102 free from 10 to 11 December?
Show answer
SELECT guest FROM bookings
WHERE room = '102'
AND check_in < '2026-12-11' AND check_out > '2026-12-10';
Mitu (10–12 Dec) is returned, so the room is not free. Tuhin leaves on the 10th, which does not clash.
Exercise 2. Do the ranges 1–5 and 5–9 overlap under the half-open rule? What about 1–6 and 5–9?
Show answer
1–5 and 5–9: is 1 < 9 and 5 < 5? No, so they do not overlap. 1–6 and 5–9: 1 < 9 and 5 < 6, so they overlap.
Frequently Asked Questions
How do I check if two date ranges overlap in SQL?
Use the condition a_start < b_end AND b_start < a_end. It is true for every kind of overlap, including when one range is completely inside the other, and false when the ranges only touch at the end.
How do I find double bookings in SQL?
Join the bookings table to itself on the same room, with a.id < b.id to avoid duplicate pairs, and add the overlap condition. Every returned row is a pair of bookings that clash.
Summary
- Overlap rule:
a_start < b_end AND b_start < a_end. - Store ranges as half-open, with the end excluded.
- A self join with the rule finds all clashes.
- Clip with
MAXof starts andMINof ends to measure overlap length.
Next: SQL Funnel Analysis.
