Classify each order by the hour of `placed_at`: hours 12-14 -> 'Lunch', hours 19-22 -> 'Dinner', anything else -> 'Off-peak'. An order is on time if it was delivered (`delivered_at` not NULL) and the minutes from placed_at to delivered_at are <= `promised_min`. Undelivered orders are late. Return `band`, `total_orders`, `on_time_orders`, `on_time_pct` (ROUND 1), `avg_delay_min` = average of (actual minutes - promised_min) over LATE orders that WERE delivered (ROUND 1; NULL if there are none). Order by `on_time_pct` DESC, then `band`.
| Column | Type |
|---|---|
| order_id | INTEGER |
| restaurant_id | INTEGER |
| placed_at | TIMESTAMP |
| delivered_at | TIMESTAMP |
| promised_min | INTEGER |
| order_id | restaurant_id | placed_at | delivered_at | promised_min |
|---|---|---|---|---|
| 1 | 1 | 2026-08-01 12:10:00 | 2026-08-01 12:40:00 | 30 |
| 2 | 1 | 2026-08-01 13:00:00 | 2026-08-01 13:50:00 | 35 |
| 3 | 2 | 2026-08-01 14:59:00 | 2026-08-01 15:20:00 | 30 |
| 4 | 2 | 2026-08-01 19:00:00 | 2026-08-01 19:45:00 | 40 |
| 5 | 3 | 2026-08-01 20:00:00 | NULL | 30 |
| 6 | 3 | 2026-08-01 22:30:00 | 2026-08-01 22:55:00 | 30 |
| 7 | 1 | 2026-08-01 16:00:00 | 2026-08-01 16:20:00 | 25 |
| 8 | 2 | 2026-08-01 11:59:00 | 2026-08-01 12:30:00 | 30 |
| band | total_orders | on_time_orders | on_time_pct | avg_delay_min |
|---|---|---|---|---|
| Lunch | 3 | 2 | 66.7 | 15 |
| Off-peak | 2 | 1 | 50 | 1 |
| Dinner | 3 | 1 | 33.3 | 5 |
Solve it in the live editor on this page — run on sample data, then submit against hidden tests.
‹ Ordered Checkout Funnel by City · De-duplicate Burst Reviews ›
{{ 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.