Find pairs of overlapping trips for the same driver in SQL
Company: Uber
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
The `trips` table logs every trip a driver takes, with its start and end time. Write a query that finds every pair of trips by the same driver whose time slots overlap, and report when each overlap starts and ends.
### Input Table(s)
Table: `trips`
| Column | Type | Meaning |
|---|---|---|
| trip_id | INT | Unique trip identifier |
| driver_id | INT | Driver who took the trip, never NULL |
| start_time | TIMESTAMP | When the trip started, never NULL |
| end_time | TIMESTAMP | When the trip ended, never NULL |
Primary key: `trip_id`. Every trip has `start_time < end_time`.
### Output Contract
| Column | Meaning |
|---|---|
| driver_id | The driver both trips belong to |
| trip_id_1 | The smaller trip id of the pair |
| trip_id_2 | The larger trip id of the pair |
| overlap_start | The later of the two start times |
| overlap_end | The earlier of the two end times |
- One row per overlapping pair, with `trip_id_1 < trip_id_2`.
- Order by `driver_id`, `trip_id_1`, `trip_id_2`, all ascending. The pair `(trip_id_1, trip_id_2)` is unique, so this order is fully determined.
- Timestamps are returned as stored, with no rounding.
### Example
Input `trips`:
| trip_id | driver_id | start_time | end_time |
|---|---|---|---|
| 1 | 10 | 2024-03-01 08:00:00 | 2024-03-01 09:00:00 |
| 2 | 10 | 2024-03-01 08:30:00 | 2024-03-01 09:30:00 |
| 3 | 10 | 2024-03-01 09:30:00 | 2024-03-01 10:00:00 |
| 4 | 10 | 2024-03-01 08:45:00 | 2024-03-01 08:50:00 |
| 5 | 20 | 2024-03-01 12:00:00 | 2024-03-01 13:00:00 |
| 6 | 30 | 2024-03-01 12:10:00 | 2024-03-01 12:40:00 |
| 7 | 20 | 2024-03-01 11:30:00 | 2024-03-01 12:15:00 |
Expected output:
| driver_id | trip_id_1 | trip_id_2 | overlap_start | overlap_end |
|---|---|---|---|---|
| 10 | 1 | 2 | 2024-03-01 08:30:00 | 2024-03-01 09:00:00 |
| 10 | 1 | 4 | 2024-03-01 08:45:00 | 2024-03-01 08:50:00 |
| 10 | 2 | 4 | 2024-03-01 08:45:00 | 2024-03-01 08:50:00 |
| 20 | 5 | 7 | 2024-03-01 12:00:00 | 2024-03-01 12:15:00 |
Trips 1 and 2 share 08:30 to 09:00. Trip 4 sits inside both trip 1 and trip 2, so each of its overlaps is trip 4's own window. Trip 3 starts at 09:30, exactly when trip 2 ends, so that pair is not reported, and it does not reach trip 1 or trip 4 at all. Trip 7 has the larger id but starts first; its overlap with trip 5 runs from 12:00 to 12:15. Trip 6 overlaps trips 5 and 7 in time but belongs to driver 30, so it pairs with nothing.
### Constraints and Clarifications
- Two trips overlap when each starts strictly before the other ends. A trip that starts exactly when another ends does not overlap it.
- Only trips of the same driver are compared. A driver with one trip, or with no overlapping trips, contributes no rows.
- A trip contained inside another counts as overlapping, and its overlap window is the inner trip itself.
- Two trips with identical start and end times overlap over their whole duration and are reported.
- Trip ids say nothing about time order. The trip with the smaller id may start later.
- No column is NULL and `trip_id` is unique, so there are no duplicate trips.
- `trips` has at most 10,000 rows.
```hint Pitfall to check
Make sure each pair appears once, and test your overlap condition on a pair where the trip with the larger id starts first.
```
Overview: Write a SQL query that finds every pair of trips by the same driver whose time slots overlap and reports when each overlap starts and ends. It tests self-joins, the interval overlap condition with strict boundaries, and returning each pair exactly once.