Question: SELECT b . id , COUNT ( p . id ) AS passengers _ on _ board FROM buses b LEFT JOIN passengers p ON

SELECT b.id, COUNT(p.id) AS passengers_on_board
FROM buses b
LEFT JOIN passengers p
ON b.origin = p.origin AND b.destination = p.destination
AND p.time <= b.time
AND NOT EXISTS (
SELECT 1
FROM buses b2
WHERE b2.origin = p.origin AND b2.destination = p.destination
AND b2.time < b.time AND p.time <= b2.time
)
GROUP BY b.id
ORDER BY b.id ASC;

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!