First Shipment Delivered on Target
A freight company lets business clients book shipments and pick the exact date they want them to arrive. Receiving docks are scheduled for that day, so arriving early is as bad as arriving late.
Management wants to know how well first impressions go: what percentage of clients had their first shipment (the one with the earliest booked_at) delivered exactly on its requested_date? A shipment that has not been delivered yet (delivered_date is NULL) is not on target.
Return a single row with one column first_on_target_pct: the percentage rounded to 2 decimal places (for example 40.0 or 33.33). If there are no shipments, return that row with NULL.
Tables
shipments — One row per booked shipment. Ids are assigned by a batch import and do not follow booking order.
| Column | Type | Description |
|---|---|---|
shipment_id (key) | INTEGER | Unique shipment id |
client_id | INTEGER | Client who booked the shipment |
booked_at | TEXT | Booking timestamp 'YYYY-MM-DD HH:MM:SS'; a client never books two shipments at the same moment |
requested_date | TEXT | Arrival date the client asked for, 'YYYY-MM-DD' |
delivered_date | TEXT | Actual arrival date 'YYYY-MM-DD'; NULL while still in transit |
Example
shipments
| shipment_id | client_id | booked_at | requested_date | delivered_date |
|---|---|---|---|---|
| 1 | 7 | 2026-01-05 09:00:00 | 2026-01-08 | 2026-01-08 |
| 2 | 7 | 2026-01-12 10:00:00 | 2026-01-15 | 2026-01-14 |
| 3 | 8 | 2026-01-06 11:30:00 | 2026-01-09 | 2026-01-07 |
| 4 | 8 | 2026-01-20 08:00:00 | 2026-01-22 | 2026-01-22 |
| 5 | 9 | 2026-01-18 16:45:00 | 2026-01-21 | 2026-01-21 |
| 6 | 9 | 2026-01-03 07:10:00 | 2026-01-06 | 2026-01-09 |
| 7 | 10 | 2026-01-25 12:00:00 | 2026-01-28 | NULL |
| 8 | 11 | 2026-01-26 15:20:00 | 2026-01-27 | 2026-01-27 |
Expected output
| first_on_target_pct |
|---|
| 40 |
Rows may be returned in any order. Your query also runs against hidden datasets with tricky edge cases.
SQL Path · Aggregation & Grouping · problem 16 of 70 · SQLite
The idea
The question has two steps. First, reduce the table to one shipment per client: the earliest booking. ROW_NUMBER() partitioned by client_id and ordered by booked_at numbers each client’s shipments chronologically, and rn = 1 keeps the first. Because ids come from a batch import, ordering by shipment_id would pick the wrong row.
Second, compute a percentage over those rows. A CASE returns 1 only when delivered_date = requested_date. For an undelivered shipment the comparison is NULL, which is not true, so it falls to ELSE 0. Summing the flags and dividing by COUNT(*) (one row per client) with 100.0 avoids integer division.
An aggregate with no GROUP BY always returns exactly one row. On an empty table COUNT(*) is 0, and SQLite returns NULL for division by zero, which gives the required NULL. The alternative joins each client to its MIN(booked_at) and averages 100-or-0 flags.
Solution
WITH ranked AS (
SELECT
client_id,
requested_date,
delivered_date,
ROW_NUMBER() OVER (PARTITION BY client_id ORDER BY booked_at) AS rn
FROM shipments
)
SELECT
ROUND(100.0 * SUM(CASE WHEN delivered_date = requested_date THEN 1 ELSE 0 END) / COUNT(*), 2)
AS first_on_target_pct
FROM ranked
WHERE rn = 1;
Another way
SELECT
ROUND(AVG(CASE WHEN s.delivered_date = s.requested_date THEN 100.0 ELSE 0 END), 2) AS first_on_target_pct
FROM shipments AS s
JOIN (
SELECT client_id, MIN(booked_at) AS first_booked
FROM shipments
GROUP BY client_id
) AS f
ON f.client_id = s.client_id
AND f.first_booked = s.booked_at;
Why common answers fail
Measures every shipment instead of only each client’s first one.
SELECT ROUND(100.0 * SUM(CASE WHEN delivered_date = requested_date THEN 1 ELSE 0 END) / COUNT(*), 2) AS first_on_target_pct
FROM shipments;
Assumes the smallest id is the first booking, but ids do not follow booking order.
SELECT ROUND(100.0 * SUM(CASE WHEN s.delivered_date = s.requested_date THEN 1 ELSE 0 END) / COUNT(*), 2) AS first_on_target_pct
FROM shipments AS s
WHERE s.shipment_id IN (SELECT MIN(shipment_id) FROM shipments GROUP BY client_id);
Counts early deliveries as on target, but the requirement is the exact requested date.
WITH ranked AS (
SELECT requested_date, delivered_date,
ROW_NUMBER() OVER (PARTITION BY client_id ORDER BY booked_at) AS rn
FROM shipments
)
SELECT ROUND(100.0 * SUM(CASE WHEN delivered_date <= requested_date THEN 1 ELSE 0 END) / COUNT(*), 2) AS first_on_target_pct
FROM ranked
WHERE rn = 1;
What the interviewer is checking
Combines "first row per group" with a conditional ratio; the interviewer watches whether you pick the first event by time rather than by id and handle NULLs.
Mistakes to avoid
- Averaging over all shipments instead of first ones.
- Choosing the first shipment by smallest id.
- Treating early delivery as on target.
- Integer division giving 0.
Follow-ups to expect
- Compare first-shipment accuracy with all later shipments (split on rn = 1 vs rn > 1).
- Report the rate per booking month of the first shipment (GROUP BY strftime on booked_at).
- Allow one day of tolerance (abs(julianday(delivered) - julianday(requested)) <= 1).
In real work
Onboarding analytics: the quality of a customer’s first experience is a strong predictor of retention, so teams track first-order SLA separately.