AnalystPath

Count Overlapping Room Bookings

SQLMediumMid level~15 min

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_bookings
room_idstarts_atends_at
109:00:0010:30:00
110:00:0011:00:00
111:30:0012: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

Related SQL questions