You're working for an airline reservation system that needs to analyze flight bookings and manage passenger seating efficiently. The system has two main components:
๐ Database Tables
Flights Table: Contains flight information with columns flight_id (unique identifier) and capacity (maximum number of seats).
Passengers Table: Contains passenger bookings with columns passenger_id (unique identifier) and flight_id (the flight they want to book).
๐ฏ The Challenge
When passengers book tickets, the airline follows a simple rule: first come, first served. If there are available seats when a passenger books, they get confirmed. If the flight is already at full capacity, they go on the waitlist.
Your task: Write a SQL query to calculate for each flight:
- Number of passengers with confirmed seats
- Number of passengers on the waitlist
Return results ordered by flight_id in ascending order.
Input & Output
Visualization
Time & Space Complexity
ROW_NUMBER() requires sorting passengers within each flight partition
Space for window function results and grouping
Constraints
- 1 โค flights.length โค 104
- 1 โค passengers.length โค 105
- 1 โค flight_id, passenger_id โค 106
- 0 โค capacity โค 104
- All flight_id values in Flights table are unique
- All passenger_id values in Passengers table are unique
- All passenger bookings reference valid flights