Given a Rides table containing ride information with drivers and passengers, write a SQL solution to report the ID of each driver and the number of times they were a passenger in other rides.
Each row contains a ride_id, driver_id, and passenger_id. Note that driver_id != passenger_id for any ride.
Return the result table in any order showing each driver's ID and their passenger count.
Table Schema
| Column Name | Type | Description |
|---|---|---|
ride_id
PK
|
int | Unique identifier for each ride |
driver_id
|
int | ID of the driver for this ride |
passenger_id
|
int | ID of the passenger for this ride |
Input & Output
| ride_id | driver_id | passenger_id |
|---|---|---|
| 1 | 1 | 2 |
| 2 | 2 | 1 |
| 3 | 3 | 1 |
| 4 | 3 | 2 |
| driver_id | cnt |
|---|---|
| 1 | 2 |
| 2 | 1 |
Driver 1 appears as a passenger in rides 2 and 3 (driven by drivers 2 and 3), so count = 2. Driver 2 appears as a passenger in ride 1 (driven by driver 1), so count = 1. Driver 3 never appears as a passenger, so they are not included in the result.
| ride_id | driver_id | passenger_id |
|---|---|---|
| 1 | 1 | 2 |
| 2 | 1 | 3 |
| driver_id | cnt |
|---|
Driver 1 never appears as a passenger in any ride, so the result is empty. Only drivers who have been passengers at least once appear in the output.
| ride_id | driver_id | passenger_id |
|---|---|---|
| 1 | 1 | 2 |
| 2 | 2 | 1 |
| driver_id | cnt |
|---|---|
| 1 | 1 |
| 2 | 1 |
Driver 1 appears as passenger in ride 2, and driver 2 appears as passenger in ride 1. Each has been a passenger exactly once.
Constraints
-
1 ≤ ride_id ≤ 100 -
1 ≤ driver_id, passenger_id ≤ 100 -
driver_id != passenger_id