Compute the average trip duration in minutes for each commuter and city
Company: Waymo
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Write a SQL query to calculate the average commute time in minutes (`end_time` minus `start_time`) for every unique `commuter_id` and `city` combination, rounded to 2 decimal places as `avg_commute_minutes`. Order by `commuter_id` ascending, then `city` ascending.
```hint Keep the seconds
Subtracting two timestamps gives an interval. Turn the whole interval into minutes before you average, and check that a trip of 40 minutes and 30 seconds counts as 40.5.
```
### Input Table(s)
`trips`
| Column | Type | Meaning |
|---|---|---|
| trip_id | INT | Id of the trip. Primary key. |
| commuter_id | INT | The commuter who took the trip. NOT NULL. |
| start_time | TIMESTAMP | Date and time the trip started, no time zone. NOT NULL. |
| end_time | TIMESTAMP | Date and time the trip ended, no time zone. NOT NULL. |
| city | VARCHAR(100) | City where the trip took place. NOT NULL. |
Primary key: `trip_id`. A commuter can have many trips, in one city or several.
### Output Contract
- Columns, in this order: `commuter_id`, `city`, `avg_commute_minutes`.
- One row per distinct (`commuter_id`, `city`) pair that appears in `trips`.
- `avg_commute_minutes` is the mean of the trip durations in that group, in minutes, rounded to 2 decimal places.
- Order by `commuter_id` ascending, then `city` ascending. Each pair appears once, so this ordering is unique.
### Example
Input `trips`:
| trip_id | commuter_id | start_time | end_time | city |
|---|---|---|---|---|
| 1 | 1 | 2024-03-01 08:00:00 | 2024-03-01 08:30:00 | Seattle |
| 2 | 1 | 2024-03-02 08:10:00 | 2024-03-02 08:55:00 | Seattle |
| 3 | 1 | 2024-03-05 17:00:00 | 2024-03-05 17:20:00 | Austin |
| 4 | 2 | 2024-03-01 07:30:00 | 2024-03-01 08:10:30 | Seattle |
| 5 | 2 | 2024-03-02 07:45:00 | 2024-03-02 08:20:00 | Seattle |
| 6 | 2 | 2024-03-03 07:40:00 | 2024-03-03 08:20:15 | Seattle |
Expected result:
| commuter_id | city | avg_commute_minutes |
|---|---|---|
| 1 | Austin | 20.00 |
| 1 | Seattle | 37.50 |
| 2 | Seattle | 38.58 |
Commuter 1 has one Austin trip of 20 minutes and two Seattle trips of 30 and 45 minutes, so the Seattle average is 37.50. Commuter 2's Seattle trips last 40.5, 35 and 40.25 minutes, which average to 115.75 / 3 = 38.5833..., rounded to 38.58. Austin sorts before Seattle for commuter 1.
### Constraints and Clarifications
- `trips` has between 1 and 10,000 rows, and `commuter_id` is between 1 and 10,000.
- `end_time` is always later than `start_time`. No column contains NULL.
- A trip's duration is the exact elapsed time between the two timestamps, in minutes, with seconds counted as fractions of a minute. Do not truncate each trip to whole minutes.
- Durations use the full date and time, so a trip that crosses midnight still gets its true length.
- `city` contains only lowercase and uppercase English letters. Group and sort cities by their exact stored value, so `Austin` and `austin` would be separate groups.
- Every trip counts once, even when two trips share the same commuter, times and city.
Overview: Given a table of commuter trips with start and end timestamps, compute each commuter's average trip duration in minutes for every city they traveled in. It tests timestamp arithmetic, converting intervals to fractional minutes, grouping on two keys, rounding and deterministic ordering.