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
Before setting a target, the team wants to see the deliveries split into four equal groups from fastest to slowest, and to know what range of times each group covers. That framing — "the slowest quarter of deliveries" — is how the conversation is actually held.
Return one row per quartile with the number of deliveries in it and its fastest and slowest time. Order by quartile.
Result columns · in this order
quartile | 1 for the fastest quarter through 4 for the slowest. |
deliveries | How many deliveries landed in this bucket. |
fastest | Shortest delivery time in the bucket, in minutes. |
slowest | Longest delivery time in the bucket, in minutes. |
How to approach it
Compute the bucket in a CTE, then group by it in the outer query.
Sample input
| delivery_id | city | courier | minutes | delivered_on |
|---|---|---|---|---|
| 1 | Bengaluru | Asha | 18 | 2026-03-01 |
| 2 | Bengaluru | Asha | 22 | 2026-03-01 |
| 3 | Bengaluru | Rohit | 26 | 2026-03-02 |
| 4 | Bengaluru | Asha | 28 | 2026-03-02 |
| 5 | Bengaluru | Rohit | 30 | 2026-03-03 |
| 6 | Bengaluru | Meera | 30 | 2026-03-03 |
| 7 | Bengaluru | Meera | 34 | 2026-03-04 |
| 8 | Bengaluru | Rohit | 41 | 2026-03-04 |
| 9 | Bengaluru | Asha | 150 | 2026-03-05 |
| 10 | Pune | Kabir | 20 | 2026-03-01 |
| 11 | Pune | Kabir | 24 | 2026-03-01 |
| 12 | Pune | Nita | 26 | 2026-03-02 |
| 13 | Pune | Nita | 30 | 2026-03-02 |
| 14 | Pune | Kabir | 33 | 2026-03-03 |
| 15 | Pune | Nita | 36 | 2026-03-03 |
| 16 | Pune | Kabir | 45 | 2026-03-04 |
| 17 | Pune | Nita | 62 | 2026-03-05 |
| 18 | Pune | Kabir | 240 | 2026-03-05 |
| 19 | Chennai | Vikram | 19 | 2026-03-01 |
| 20 | Chennai | Priya | 23 | 2026-03-01 |
| 21 | Chennai | Vikram | 27 | 2026-03-02 |
| 22 | Chennai | Priya | 30 | 2026-03-03 |
| 23 | Chennai | Vikram | 38 | 2026-03-03 |
| 24 | Chennai | Priya | 52 | 2026-03-04 |
| 25 | Chennai | Vikram | 71 | 2026-03-04 |
| 26 | Chennai | Priya | 96 | 2026-03-05 |
26 rows — scroll inside the table to see them all.
Expected output
| quartile | deliveries | fastest | slowest |
|---|---|---|---|
| 1 | 7 | 18 | 26 |
| 2 | 7 | 26 | 30 |
| 3 | 6 | 33 | 45 |
| 4 | 6 | 52 | 240 |
4 rows — all rows shown.
Constraints
NTILE(4) over all deliveries ordered by minutes, ascending — quartile 1 is the fastest quarter.NTILE is a window function, so it is computed after GROUP BY. You cannot group by it in the same SELECT that produces it; it has to come from a subquery or CTE.fastest and slowest per bucket so the reader can see the ranges are not equal in width.quartile.Worked example
Twenty-six deliveries into four buckets does not divide evenly. NTILE gives the earlier buckets the extra rows: 7, 7, 6, 6.
Now look at what that costs. Deliveries 3 and 12 both took exactly 26 minutes, and they sit at ranks 7 and 8 — either side of the boundary. One lands in quartile 1 and the other in quartile 2, on nothing but their row number.
NTILE balances by row count, never by value. If equal values must stay together, NTILE is the wrong tool and a CASE over explicit thresholds is the right one.
What this tests
That you know NTILE splits by row count rather than by value, and that you can see where a window function is allowed to appear in a query.
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.