ExpertWindow Functions50 points

Gaps and islands: every driver's longest working streak

Zoomi pays a loyalty bonus to drivers who work many days in a row. A driver worked on a calendar day if they completed at least one trip that day (status 'Completed', day taken from requested_at). A streak is a run of consecutive calendar days with no missing day in between. A driver may have several streaks, and the bonus is based on the longest one.

Task: for every driver who completed any trip, return driver (name), streak_days, streak_start and streak_end (dates as YYYY-MM-DD) of their single longest streak. If a driver has two streaks of the same length, use the earlier one. Sort by streak_days from high to low, then driver A to Z.

The data you can query

Each table is shown with its column types and the first rows, so you can see exactly what the data looks like.

cities4 rows
id INTEGERname TEXTcountry TEXT
1DubaiUAE
2LahorePakistan
3SingaporeSingapore
4NairobiKenya
drivers8 rows
id INTEGERname TEXTcity_id INTEGERjoined_on TEXTvehicle_type TEXT
1Rashid Al Mansoori12025-09-18Economy
2Tariq Mehmood22026-02-05Economy
3Wei Jie Tan32025-09-08Comfort
4Samuel Kiptoo42025-11-12Economy
5Hassan Qureshi12025-12-20Economy
6Priya Raman22025-06-24Economy

Showing the first 6 of 8 rows.

riders32 rows
id INTEGERname TEXThome_city_id INTEGERsignup_date TEXT
1Ayesha Khan12026-03-09
2Bilal Okafor22026-02-26
3Chen Silva32026-03-12
4Diego Tanaka42026-03-30
5Elena Weber12026-06-04
6Farid Nguyen22026-05-23

Showing the first 6 of 32 rows.

trips548 rows
id INTEGERrider_id INTEGERdriver_id INTEGERcity_id INTEGERrequested_at TEXTstatus TEXTdistance_km REALfare_usd REALsurge REALrating INTEGER
11512026-04-06 08:23:01Completed46.915
24842026-04-06 10:42:25Completed13.19.441.255
33332026-04-06 13:24:49Completed3.55.5315
42222026-04-06 17:17:07Completed15.89.51.54
51112026-04-06 18:25:11Completed11.819.351.254
64842026-04-06 21:01:43Completed16.69.314

Showing the first 6 of 548 rows.

Your query runs on SQLite. Write a single SELECT; use LIMIT instead of TOP.

Need a hint?

Subtract the row number (in days) from each date. Days in the same streak land on the same value.