Number of Times a Driver Was a Passenger - Problem
In a ride-sharing service, we have a database table Rides that tracks all completed trips. Each row represents a single ride with three key pieces of information:
ride_id- unique identifier for each ridedriver_id- ID of the person who drovepassenger_id- ID of the person who was the passenger
Your task: Some people in the system act as both drivers and passengers at different times. We need to find out for each person who has been a driver, how many times they have also been a passenger in other rides.
Output format: Return a result table with two columns:
driver_id- ID of each drivercnt- number of times that driver was also a passenger
Note: If a driver has never been a passenger, they should still appear in the result with a count of 0.
Input & Output
example_1.sql โ Basic Example
$
Input:
Rides table:
+----------+----------+--------------+
| ride_id | driver_id | passenger_id |
+----------+----------+--------------+
| 1 | 10 | 20 |
| 2 | 20 | 10 |
| 3 | 30 | 40 |
+----------+----------+--------------+
โบ
Output:
+----------+-----+
| driver_id | cnt |
+----------+-----+
| 10 | 1 |
| 20 | 1 |
| 30 | 0 |
+----------+-----+
๐ก Note:
Driver 10 was a passenger 1 time (in ride 2), driver 20 was a passenger 1 time (in ride 1), and driver 30 was never a passenger (cnt = 0).
example_2.sql โ Multiple Passenger Rides
$
Input:
Rides table:
+----------+----------+--------------+
| ride_id | driver_id | passenger_id |
+----------+----------+--------------+
| 1 | 10 | 20 |
| 2 | 30 | 10 |
| 3 | 30 | 20 |
| 4 | 40 | 10 |
+----------+----------+--------------+
โบ
Output:
+----------+-----+
| driver_id | cnt |
+----------+-----+
| 10 | 0 |
| 30 | 0 |
| 40 | 0 |
+----------+-----+
๐ก Note:
None of the drivers (10, 30, 40) appear as passengers in any ride, so all have cnt = 0.
example_3.sql โ Same Person Multiple Roles
$
Input:
Rides table:
+----------+----------+--------------+
| ride_id | driver_id | passenger_id |
+----------+----------+--------------+
| 1 | 10 | 20 |
| 2 | 20 | 10 |
| 3 | 10 | 30 |
| 4 | 40 | 10 |
+----------+----------+--------------+
โบ
Output:
+----------+-----+
| driver_id | cnt |
+----------+-----+
| 10 | 2 |
| 20 | 1 |
| 40 | 0 |
+----------+-----+
๐ก Note:
Driver 10 was a passenger 2 times (rides 2 and 4), driver 20 was a passenger 1 time (ride 1), and driver 40 was never a passenger.
Constraints
- 1 โค rides.length โค 104
- 1 โค ride_id, driver_id, passenger_id โค 106
- driver_id โ passenger_id for each ride
- ride_id values are unique
Visualization
Tap to expand
Understanding the Visualization
1
Identify All Drivers
Extract unique driver IDs from all rides - these are our people of interest
2
Match with Passenger Records
For each driver, find all rides where they appear as a passenger
3
Count and Preserve Zeros
Count passenger appearances, ensuring drivers with zero passenger rides still appear in results
Key Takeaway
๐ฏ Key Insight: Use LEFT JOIN to preserve all drivers in the result, ensuring those who were never passengers still appear with count = 0
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code