cte that selects bus_id, arrival_time, and the arrival time of the previous bus using the LAG function.LAG function to 0 to handle the first bus which has no previous bus.LEFT JOIN between the CTE cte and the Passengers table on the condition that the passenger's arrival time is greater than the previous bus's arrival time and less than or equal to the current bus's arrival time.bus_id from the CTE.passengers_cnt.bus_id in ascending order.erDiagram
Buses {
int bus_id PK
int arrival_time
}
Passengers {
int passenger_id PK
int arrival_time
}
Buses ||--o{ Passengers : "has"