Compatible Carpool Pairs
Problem
A commuter app records the routes each rider takes each day, and which riders are already grouped into the same carpool.
Table: trips
| Column Name | Type |
|-------------|------|
| rider_id | int |
| route_id | int |
| trip_date | date |
Each row means the rider travelled that route on that date.
Table: carpool
| Column Name | Type |
|-------------|------|
| rider1_id | int |
| rider2_id | int |
(rider1_id, rider2_id) is the primary key for this table. Each row means the two riders share a carpool. Note that rider1_id < rider2_id.
A carpool pair (rider1_id, rider2_id) is highly compatible if:
- They already share a carpool.
- They travelled the same three or more distinct routes on the same day.
Return the highly compatible pairs in any order, with rider1_id < rider2_id and no duplicate rows.
Tables
Example rows — the live problem includes the full dataset.
| rider_id | route_id | trip_date |
|---|---|---|
| 1 | 10 | 2021-03-15 |
| 1 | 11 | 2021-03-15 |
| 1 | 12 | 2021-03-15 |
| rider1_id | rider2_id |
|---|---|
| 1 | 2 |
| 2 | 7 |
Expected output
Your answer should return 2 rows with the columns rider1_id, rider2_id.
Starter code (SQL)
SELECT *
FROM trips;Solve this SQL question free
Write SQL and run it instantly in your browser — even on your phone. No signup needed to try.
Solution & explanation
Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.
Sign up free to unlock