Sign in to run and submit your work
Reading is open to everyone. Running code and saving drafts need an account so your work is yours and comes back on your next visit.
or
CODE WORKSPACE
The city operations team is judged on the slow tail of delivery times, not the middle. They want median and 90th-percentile delivery minutes per city beside the average, and this engine has no percentile function.
Return one row per city, ordered by p90 descending.
Result columns · in this order
city | The city. |
deliveries | Completed deliveries there. |
avg_minutes | Mean delivery time. |
median_minutes | The middle delivery time. |
p90_minutes | The delivery time 90% of orders beat. |
How to approach it
Sort each city's latencies and count positions. A percentile is a position, not a formula.
Sample input
| delivery_id | city | ordered_at | delivered_at |
|---|---|---|---|
| 1 | Bengaluru | 2026-03-02 12:00:00 | 2026-03-02 12:22:00 |
| 2 | Bengaluru | 2026-03-02 12:05:00 | 2026-03-02 12:31:00 |
| 3 | Bengaluru | 2026-03-02 12:10:00 | 2026-03-02 12:38:00 |
| 4 | Bengaluru | 2026-03-02 12:15:00 | 2026-03-02 12:44:00 |
| 5 | Bengaluru | 2026-03-02 12:20:00 | 2026-03-02 12:52:00 |
| 6 | Bengaluru | 2026-03-02 12:25:00 | 2026-03-02 13:00:00 |
| 7 | Bengaluru | 2026-03-02 12:30:00 | 2026-03-02 13:11:00 |
| 8 | Bengaluru | 2026-03-02 12:35:00 | 2026-03-02 13:30:00 |
| 9 | Bengaluru | 2026-03-02 12:40:00 | 2026-03-02 14:15:00 |
| 10 | Delhi | 2026-03-02 19:00:00 | 2026-03-02 19:18:00 |
| 11 | Delhi | 2026-03-02 19:05:00 | 2026-03-02 19:26:00 |
| 12 | Delhi | 2026-03-02 19:10:00 | 2026-03-02 19:34:00 |
| 13 | Delhi | 2026-03-02 19:15:00 | 2026-03-02 19:43:00 |
| 14 | Delhi | 2026-03-02 19:20:00 | 2026-03-02 19:52:00 |
| 15 | Delhi | 2026-03-02 19:25:00 | 2026-03-02 20:04:00 |
| 16 | Delhi | 2026-03-02 19:30:00 | 2026-03-02 20:20:00 |
| 17 | Delhi | 2026-03-02 19:35:00 | 2026-03-02 21:05:00 |
| 18 | Mumbai | 2026-03-02 13:00:00 | 2026-03-02 13:24:00 |
| 19 | Mumbai | 2026-03-02 13:05:00 | 2026-03-02 13:31:00 |
| 20 | Mumbai | 2026-03-02 13:10:00 | 2026-03-02 13:37:00 |
| 21 | Mumbai | 2026-03-02 13:15:00 | 2026-03-02 13:42:00 |
| 22 | Mumbai | 2026-03-02 13:20:00 | 2026-03-02 13:49:00 |
| 23 | Mumbai | 2026-03-02 13:25:00 | 2026-03-02 13:57:00 |
| 24 | Mumbai | 2026-03-02 13:30:00 | 2026-03-02 14:38:00 |
24 rows — scroll inside the table to see them all.
Expected output
| city | deliveries | avg_minutes | median_minutes | p90_minutes |
|---|---|---|---|---|
| Bengaluru | 9 | 40.3 | 32 | 95 |
| Delhi | 8 | 37.8 | 28 | 90 |
| Mumbai | 7 | 33.3 | 27 | 68 |
3 rows — all rows shown.
Constraints
ordered_at and delivered_at.CEIL(0.9 * n) for p90.(n + 1) / 2 under integer division — with 9 deliveries that is position 5.avg_minutes is rounded to 1 decimal place; the percentiles are actual observed values, so they stay whole numbers.p90_minutes descending, then city.Worked example
Bengaluru's nine latencies sorted are 22, 26, 28, 29, 32, 35, 41, 55 and 95 minutes. The median is position 5, so 32. p90 is position CEIL(0.9 * 9) = 9, so 95.
The average is 40.3, which describes no delivery that happened: it is 8 minutes above the median and 55 below the worst. A single 95-minute delivery drags it up far enough to look like a mild problem, while the customer who waited an hour and a half is invisible in it.
What this tests
Building a percentile from positions when the engine provides none, and reading the difference between a mean and a tail — the reason latency is reported at p90 or p99 rather than on average.
Submit for review to find out what your query gets right, what it gets wrong, and how it compares with the best working query for this exercise.
This scenario runs a full workspace — editor, canvas and results side by side. It needs a laptop or desktop to be usable. Open this page on a bigger screen to start building.