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
A delivery counts as on time when it took 30 minutes or less. Operations want the on-time percentage per city on a weekly dashboard.
The first attempt is in the editor and it reports 0 for every city, which is obviously wrong — Bengaluru alone had six on-time deliveries out of nine. Return city, the total deliveries, the on_time count and on_time_pct, ordered by city.
Result columns · in this order
city | The city. |
deliveries | Total deliveries made in that city. |
on_time | How many took 30 minutes or less. |
on_time_pct | On-time deliveries as a percentage of the total, to one decimal place. |
How to approach it
Multiply by 100.0 before dividing, so the arithmetic is decimal from the start.
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
| city | deliveries | on_time | on_time_pct |
|---|---|---|---|
| Bengaluru | 9 | 6 | 66.7 |
| Chennai | 8 | 4 | 50 |
| Pune | 9 | 4 | 44.4 |
3 rows — all rows shown.
Constraints
minutes <= 30. The boundary is inclusive, and four deliveries sit exactly on it, so an accidental < changes three of the four answers.on_time_pct is a percentage between 0 and 100, not a fraction between 0 and 1.city.Worked example
Bengaluru: 9 deliveries, 6 of them at 30 minutes or less. 6 / 9 in integer arithmetic is 0 — the fractional part is discarded, and no error is raised.
100.0 * 6 / 9 is 66.7, because the first operand is a decimal literal, which promotes the whole expression to decimal arithmetic before any division happens.
Order matters. 100 * (6 / 9) still truncates first and gives 0; ROUND(6 / 9, 1) * 100 does too. The decimal has to enter the expression before the division, not after it.
What this tests
The integer-division trap, which silently floors a rate to zero, plus conditional aggregation for the numerator.
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.