Parcels That Missed Their SLA
A courier company promises delivery within 24 hours of pickup for express parcels and within 72 hours for standard parcels. The promise is measured in exact elapsed time between picked_up_at and delivered_at (to the second), not in calendar days.
List the delivered parcels that took strictly longer than their limit; a parcel delivered exactly on the limit is on time. Parcels not yet delivered are ignored.
Return parcel_id, service and hours_taken (elapsed hours rounded to 1 decimal), ordered by parcel_id. If no parcel is late, return no rows.
Tables
parcels — One row per parcel collected by a courier.
| Column | Type | Description |
|---|---|---|
parcel_id (key) | INTEGER | Unique id |
service | TEXT | 'express' (24-hour promise) or 'standard' (72-hour promise) |
picked_up_at | TEXT | Pickup time, 'YYYY-MM-DD HH:MM:SS' |
delivered_at | TEXT | Delivery time, 'YYYY-MM-DD HH:MM:SS'; NULL while still in transit |
Example
parcels
| parcel_id | service | picked_up_at | delivered_at |
|---|---|---|---|
| 1 | express | 2026-05-04 09:00:00 | 2026-05-05 08:30:00 |
| 2 | express | 2026-05-04 09:00:00 | 2026-05-05 09:00:00 |
| 3 | express | 2026-05-04 09:00:00 | 2026-05-05 11:15:00 |
| 4 | standard | 2026-05-04 23:30:00 | 2026-05-07 10:00:00 |
| 5 | standard | 2026-05-04 08:00:00 | 2026-05-07 20:00:00 |
| 6 | express | 2026-05-06 14:00:00 | NULL |
| 7 | express | 2026-05-06 08:00:00 | 2026-05-07 22:00:00 |
Expected output
| parcel_id | service | hours_taken |
|---|---|---|
| 3 | express | 26.3 |
| 5 | standard | 84 |
| 7 | express | 38 |
Return the rows in exactly the order described. Your query also runs against hidden datasets with tricky edge cases.
SQL Path · Dates & Time · problem 46 of 70 · SQLite
The idea
julianday() converts a timestamp into a day number with a fractional part for the time of day, so julianday(delivered_at) - julianday(picked_up_at) is the exact elapsed time in days. Multiply by 24 for hours.
The limit depends on the service, so a CASE picks 24 or 72 per row, and the strict > keeps a delivery that lands exactly on the limit on time. Fractional day numbers are floating point, so the solution compares in whole seconds (ROUND(... * 86400)) to make the boundary exact. The displayed hours_taken is rounded only for output.
Undelivered parcels have delivered_at IS NULL; julianday(NULL) is NULL and the comparison would drop them anyway, but saying so explicitly makes the intent clear.
The alternative uses unixepoch(), which returns integer seconds, so the difference is exact with no rounding needed.
Solution
SELECT parcel_id,
service,
ROUND((julianday(delivered_at) - julianday(picked_up_at)) * 24, 1) AS hours_taken
FROM parcels
WHERE delivered_at IS NOT NULL
AND ROUND((julianday(delivered_at) - julianday(picked_up_at)) * 86400)
> CASE service WHEN 'express' THEN 24 ELSE 72 END * 3600
ORDER BY parcel_id;
Another way
SELECT parcel_id,
service,
ROUND((unixepoch(delivered_at) - unixepoch(picked_up_at)) / 3600.0, 1) AS hours_taken
FROM parcels
WHERE unixepoch(delivered_at) - unixepoch(picked_up_at)
> iif(service = 'express', 24, 72) * 3600
ORDER BY parcel_id;
Why common answers fail
Measures calendar days between the two dates instead of elapsed hours, so a 47-hour express delivery on the next calendar day looks on time.
SELECT parcel_id, service,
ROUND((julianday(delivered_at) - julianday(picked_up_at)) * 24, 1) AS hours_taken
FROM parcels
WHERE julianday(date(delivered_at)) - julianday(date(picked_up_at))
> CASE service WHEN 'express' THEN 1 ELSE 3 END
ORDER BY parcel_id;
Uses >= so a parcel delivered exactly on the limit is wrongly reported as late.
SELECT parcel_id, service,
ROUND((julianday(delivered_at) - julianday(picked_up_at)) * 24, 1) AS hours_taken
FROM parcels
WHERE ROUND((julianday(delivered_at) - julianday(picked_up_at)) * 86400)
>= CASE service WHEN 'express' THEN 24 ELSE 72 END * 3600
ORDER BY parcel_id;
Rounds elapsed time to whole hours before comparing, so a parcel a few minutes late rounds back down to the limit and is missed.
SELECT parcel_id, service,
ROUND((julianday(delivered_at) - julianday(picked_up_at)) * 24, 1) AS hours_taken
FROM parcels
WHERE ROUND((julianday(delivered_at) - julianday(picked_up_at)) * 24) > CASE service WHEN 'express' THEN 24 ELSE 72 END
ORDER BY parcel_id;
What the interviewer is checking
Checks that you can do timestamp arithmetic without DATEDIFF, respect a precise SLA definition, and get boundary conditions right.
Mistakes to avoid
- Comparing calendar dates instead of elapsed time.
- Using >= when "within 24 hours" makes exactly 24 hours on time.
- Rounding before comparing, which hides small overruns.
- Comparing floating-point day fractions directly at an exact boundary.
Follow-ups to expect
- Compute the on-time percentage per service (AVG of a CASE that is 1 when on time, times 100.0).
- Flag parcels still in transit that are already past their limit (use julianday('now') instead of delivered_at).
- Exclude weekend hours from the clock (generate hours with a recursive CTE, or subtract whole weekend days).
In real work
Logistics, support-ticket and incident-response teams track SLA breaches with exactly this kind of elapsed-time check, and boundary disputes are common.