Merge Overlapping Room Bookings
Problem
A coworking space records bookings for its meeting rooms. Each booking covers a continuous block of days in one room, from `from_day` to `to_day` inclusive.
Two bookings for the same room overlap if they share at least one day (a booking ending on a day and another starting on that same day overlap). Overlapping bookings should be collapsed into a single continuous block.
Write a query that, for each room, merges all overlapping bookings into consolidated blocks. Return one row per merged block with columns `room_id`, `from_day`, and `to_day`. The result may be returned in any order.
Tables
Example rows — the live problem includes the full dataset.
| room_id | from_day | to_day |
|---|---|---|
| 1 | 2023-01-13 | 2023-01-14 |
| 1 | 2023-01-14 | 2023-01-17 |
| 1 | 2023-01-18 | 2023-01-25 |
Expected output
Your answer should return 4 rows with the columns room_id, from_day, to_day.
Starter code (SQL)
SELECT *
FROM Booking;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