Overlapping bookings for the same room
MediumP2Coding
Table bookings:
| Column | Type |
|---|---|
| id | int (primary key) |
| room_id | int |
| start_at | timestamp |
| end_at | timestamp (after start_at) |
Find every pair of bookings for the same room whose time ranges overlap. Two bookings overlap when each starts before the other ends; a booking that ends exactly when another starts does not overlap it. List each pair once, with the lower id first.
Output columns: booking_a, booking_b.
Schema
CREATE TABLE bookings ( id int PRIMARY KEY, room_id int NOT NULL, start_at timestamp NOT NULL, end_at timestamp NOT NULL CHECK (end_at > start_at) );
Example 1
- Input
- room 1: booking 1 from 10:00 to 12:00, booking 2 from 11:00 to 13:00
- Output
- (1, 2)
Example 2
- Input
- room 1: booking 1 from 10:00 to 12:00, booking 2 from 12:00 to 14:00
- Output
- no rows
- Why
- They touch at 12:00 but do not overlap.
Constraints
- PostgreSQL dialect.
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
PostgreSQL
Run checks the 3 visible tests, Submit all 7. ⌘Enter runs, ⌘⇧Enter submits.