Peak number of bookings at once
An SQL exercise from Querydrill. Solve it with a SQL editor in your browser, checked on the rows it returns and again on changed data, and it is checked the moment you submit.
Try it yourself
A booking occupies its room from check_in up to, but not including, check_out. Return one number: the highest number of bookings that are in the hotel at the same time, over all rooms.
bookings (9 rows)
| id | room | guest | check_in | check_out |
|---|---|---|---|---|
| 1 | A | Noor | 2025-06-01 | 2025-06-05 |
| 2 | A | Omar | 2025-06-04 | 2025-06-08 |
| 3 | A | Pia | 2025-06-08 | 2025-06-10 |
| 4 | B | Quinn | 2025-06-02 | 2025-06-06 |
| 5 | B | Rosa | 2025-06-03 | 2025-06-04 |
| 6 | B | Sven | 2025-06-10 | 2025-06-12 |
| 7 | C | Tara | 2025-06-05 | 2025-06-07 |
| 8 | A | Uma | 2025-06-15 | 2025-06-18 |
| 9 | B | Vik | 2025-06-05 | 2025-06-09 |
Free, no account needed. PostgreSQL runs in your browser and your answer is checked again on changed data.
What this exercise teaches
This is the sweep line pattern. Every arrival adds 1 and every departure takes 1 away, and the running total is the number in the hotel. On a day with both, the departures must come first, because a guest who checks out no longer counts.
A hint if you get stuck
Turn each booking into two rows: +1 on check_in and -1 on check_out. A running total shows how many are in the hotel.
Background you need
Common mistakes on this exercise
- Counts the arrivals of a day before its departures, so guests who leave that day are still counted.
- Counts all bookings, not the ones that are there at the same time.
Each of these returns a result that looks plausible. The checker catches them because it runs your query on changed data as well.
Keep going on the practice page
Your first 8 exercises on Querydrill are free, with no account. The practice page has the full editor, timed mode and all the exercises in order. A free account shows 3 reference solutions a day; Pro unlocks every exercise, every solution, timed mode and the certificate.
Open this exerciseMore practice on the same topic
More exercises in Interview classics
- Third highest salary
- Second highest salary per department
- Earns more than the manager
- Departments above average
- Salary against the department average
- Pivot the statuses
- Repeat purchase rate
- Median salary
- Orders in consecutive months
- Runs of the same status
- Cohort retention
- Products bought together
- First order not paid
- A month with no sales still shows up (generate_series)
- Daily active users
- Monthly active users
- Funnel: users at each step
- Signup to purchase conversion
- New and returning users per day
- Day-1 retention
- Retention curve
- Sessions per user (30-minute rule)
- Rolling 7-day active users
- Longest streak of active days
- Monthly churn rate
- Reactivated subscribers
- Merge bookings into occupied periods