The original interview report identified a late-order SQL exercise but did not preserve its exact schema. The following is a self-contained practice reconstruction of that topic.
You have a PostgreSQL table:
orders
------
order_id BIGINT PRIMARY KEY
created_at TIMESTAMPTZ NOT NULL
promised_delivery_at TIMESTAMPTZ NOT NULL
delivered_at TIMESTAMPTZ NULL
status TEXT NOT NULL -- 'completed', 'cancelled', or 'failed'
delivery_zone TEXT NOT NULL -- non-empty delivery market zone
Write one PostgreSQL query that produces one row for every UTC calendar date and delivery zone that has at least one eligible order. An eligible order has status = 'completed' and a non-null delivered_at. Exclude completed rows whose delivered_at is null; exclude all cancelled and failed rows. The three grouping/comparison fields created_at, promised_delivery_at, and delivery_zone are guaranteed non-null, and delivery_zone is guaranteed non-empty, so no additional null or unknown-zone bucket is needed.
Return these columns:
-
order_date
: the UTC calendar date obtained from the instant in
created_at
, regardless of the timezone offset originally used to write that value
-
delivery_zone
-
completed_orders
: number of eligible orders in the group
-
late_orders
: number of eligible orders where
delivered_at > promised_delivery_at
-
late_order_rate
:
late_orders / completed_orders
as a decimal between 0 and 1
-
previous_date_late_order_rate
: the late-order rate for the preceding available
order_date
in the same zone, or
NULL
for the zone's first date
-
late_order_rate_change
: current rate minus
previous_date_late_order_rate
, or
NULL
when there is no previous rate
An order delivered exactly at its promised time is on time. Preserve full numeric precision; do not format the rates as strings or percentages. Order the final result by order_date ascending and delivery_zone ascending.