Querydrill

Level 5: Interview classics Exercise 127 of 161 Hard Running totals and moving windows

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)

idroomguestcheck_incheck_out
1ANoor2025-06-012025-06-05
2AOmar2025-06-042025-06-08
3APia2025-06-082025-06-10
4BQuinn2025-06-022025-06-06
5BRosa2025-06-032025-06-04
6BSven2025-06-102025-06-12
7CTara2025-06-052025-06-07
8AUma2025-06-152025-06-18
9BVik2025-06-052025-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 exercise

More practice on the same topic

More exercises in Interview classics