For each driver, look at each trip and the driver's next trip (by `start_ts`). The idle gap is the minutes between the trip's `end_ts` and the next trip's `start_ts`. Only count gaps where the next trip starts on the same calendar day as the previous trip ended AND the gap is at most 120 minutes (longer gaps are shift breaks). Return `driver_id`, `idle_gaps` (number of qualifying gaps), `avg_idle_min` (ROUND 1). Drivers with no qualifying gap are excluded. Order by `avg_idle_min`, then `driver_id`.
| Column | Type |
|---|---|
| trip_id | INTEGER |
| driver_id | INTEGER |
| start_ts | TIMESTAMP |
| end_ts | TIMESTAMP |
| trip_id | driver_id | start_ts | end_ts |
|---|---|---|---|
| 1 | 10 | 2026-04-01 08:00:00 | 2026-04-01 08:20:00 |
| 2 | 10 | 2026-04-01 08:35:00 | 2026-04-01 09:00:00 |
| 3 | 10 | 2026-04-01 09:10:00 | 2026-04-01 09:40:00 |
| 4 | 10 | 2026-04-01 13:00:00 | 2026-04-01 13:30:00 |
| 5 | 10 | 2026-04-02 07:00:00 | 2026-04-02 07:30:00 |
| 6 | 11 | 2026-04-01 22:00:00 | 2026-04-01 23:50:00 |
| 7 | 11 | 2026-04-02 00:10:00 | 2026-04-02 00:30:00 |
| 8 | 11 | 2026-04-02 00:40:00 | 2026-04-02 01:00:00 |
| driver_id | idle_gaps | avg_idle_min |
|---|---|---|
| 11 | 1 | 10 |
| 10 | 2 | 12.5 |
Solve it in the live editor on this page — run on sample data, then submit against hidden tests.
‹ Driver Longest Active-Day Streak · Watch Sessions with 30-Minute Timeout ›
{{ totalCount }} questions in the style asked at Amazon, Microsoft, Google, Meta, Flipkart and more — {{ hardPct }}% Hard. Write SQL in a real database engine in your browser, run it on sample data, then evaluate against hidden tests.
Company tags show where this type of question is commonly reported in interviews. Questions, data and solutions are written by TechTown.
{{ cur.statement }}
No submissions yet. Press Evaluate to submit your query.
{{ solutionSql }}
{{ out.error }}
You have solved your 3 free questions — nice work! Log in (free) to unlock all 150 questions, keep your streak and appear on the leaderboard.