Count Overlapping Room Bookings
Problem
An office tracks meeting-room reservations in `room_bookings`. Each row has a `room_id`, a `starts_at` time, and an `ends_at` time. Two bookings for the same room overlap when one booking starts strictly before another booking ends, while that other booking has already started — in other words, for the same `room_id`, a pair (A, B) overlaps when A starts before B and A has not yet ended by the time B starts (A.ends_at > B.starts_at).
For each room, count how many such overlapping pairs exist. Return `room_id` and `overlap_count`, and include only rooms that have at least one overlapping pair. Order the result by `room_id`.
Tables
Example rows — the live problem includes the full dataset.
| room_id | starts_at | ends_at |
|---|---|---|
| 1 | 09:00:00 | 10:30:00 |
| 1 | 10:00:00 | 11:00:00 |
| 1 | 11:30:00 | 12:00:00 |
Expected output
Your answer should return 2 rows with the columns room_id, overlap_count.
Starter code (SQL)
SELECT *
FROM room_bookings;Solve this SQL question free
Write SQL and run it instantly in your browser — even on your phone. No signup needed to try.
Solution & explanation
Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.
Sign up free to unlock