Longest On-Crew Relay Streak
Problem
A courier network moves parcels by relay: each `relay_hops` row records one courier handing a parcel to another at time `hop_at` (format `HH:MM`). Every courier belongs to a crew. A hop is **on-crew** when the receiving courier is on the same crew as the sending courier; otherwise the parcel left the crew and the streak resets. For each crew, ordering that crew's hops by time, find the length of the longest unbroken run of consecutive on-crew hops. Return `crew_name` and `longest_streak` for every crew that has at least one on-crew hop, ordered by `crew_name`.
Tables
Example rows — the live problem includes the full dataset.
| courier_id | crew_name |
|---|---|
| 1 | Falcon |
| 2 | Falcon |
| 3 | Falcon |
| hop_from | hop_at | hop_to |
|---|---|---|
| 1 | 00:05 | 2 |
| 2 | 00:07 | 3 |
| 3 | 00:08 | 4 |
Expected output
Your answer should return 2 rows with the columns crew_name, longest_streak.
Starter code (SQL)
SELECT *
FROM crews;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