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.

ColumnTypeDescription
shipment_id (key)INTEGERUnique shipment id
client_idINTEGERClient who booked the shipment
booked_atTEXTBooking timestamp 'YYYY-MM-DD HH:MM:SS'; a client never books two shipments at the same moment
requested_dateTEXTArrival date the client asked for, 'YYYY-MM-DD'
delivered_dateTEXTActual arrival date 'YYYY-MM-DD'; NULL while still in transit

Example

shipments

shipment_idclient_idbooked_atrequested_datedelivered_date
172026-01-05 09:00:002026-01-082026-01-08
272026-01-12 10:00:002026-01-152026-01-14
382026-01-06 11:30:002026-01-092026-01-07
482026-01-20 08:00:002026-01-222026-01-22
592026-01-18 16:45:002026-01-212026-01-21
692026-01-03 07:10:002026-01-062026-01-09
7102026-01-25 12:00:002026-01-28NULL
8112026-01-26 15:20:002026-01-272026-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

First isolate one row per client: the shipment with the earliest booked_at.
ROW_NUMBER() OVER (PARTITION BY client_id ORDER BY booked_at) = 1, or a join to MIN(booked_at) per client, does that.
Turn each first shipment into 1 or 0 with CASE (a NULL delivered_date must become 0), then divide by the number of clients using 100.0.

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.

Practice next

Next Post Previous Post
No Comment
Add Comment
comment url