KatInterview

Keeping LEFT JOIN Intact While Picking One Shipment per Order

SQL & DataModel answer
EasyModel answerSQL & Datasqljoinswindow-functions

Last updated

Explain why moving a shipment predicate from ON to WHERE turns a LEFT JOIN into an INNER JOIN, then write a query that keeps every qualifying order and attaches only its latest expedited shipment.

Setting

You support a fulfillment analytics pipeline that joins marketplace order data to shipment events.

Two tables are available:

  • orders: order_id, marketplace_id, seller_id, order_date
  • shipments: shipment_id, marketplace_id, order_id, carrier, is_expedited (BOOLEAN), shipped_at

Order identifiers are only unique within a marketplace, so every join between these tables must use the composite key (order_id, marketplace_id). A single order may have several shipments, and several of them may be expedited.

Part 1 — Explain the difference

Two reports are in circulation:

-- Report 1
SELECT o.order_id, o.marketplace_id, s.carrier
FROM orders o
LEFT JOIN shipments s
  ON o.order_id = s.order_id
 AND o.marketplace_id = s.marketplace_id
 AND s.is_expedited = TRUE;

-- Report 2
SELECT o.order_id, o.marketplace_id, s.carrier
FROM orders o
LEFT JOIN shipments s
  ON o.order_id = s.order_id
 AND o.marketplace_id = s.marketplace_id
WHERE s.is_expedited = TRUE;

Describe, in words, how each report behaves:

  • Which orders appear in each result?
  • How many rows does each report return on the sample data below, and why?
  • What happens to an order that has shipments but none of them expedited?
  • What happens to an order that has no shipments at all?
  • Is there still a risk of duplicated orders even in the report that behaves "correctly"?

Part 2 — Produce the report you actually need

Write a SQL query that satisfies all of the following:

  1. Every order with order_date >= '2023-01-01' appears in the output.
  2. The join to shipments uses (order_id, marketplace_id).
  3. Only expedited shipments are eligible.
  4. If an order has no expedited shipment, the order still appears with NULL in the shipment columns.
  5. If an order has several expedited shipments, exactly one row is returned for that order — the one with the greatest shipped_at.
  6. When two expedited shipments share the exact same shipped_at, the choice must be deterministic (use shipment_id as the tie-breaker).

Sample Data

orders:

order_idmarketplace_idseller_idorder_date
101USS12023-02-01
102USS22023-02-05
103UKS12023-02-10

shipments:

shipment_idmarketplace_idorder_idcarrieris_expeditedshipped_at
1US101UPSTRUE2023-02-02 10:00:00
2US101FedExTRUE2023-02-02 14:00:00
3US102USPSFALSE2023-02-06 09:00:00

Expected Output

order_idmarketplace_idseller_idcarriershipped_at
101USS1FedEx2023-02-02 14:00:00
102USS2NULLNULL
103UKS1NULLNULL

Edge Cases

  • Orders whose only shipments are non-expedited.
  • Orders with no shipment rows whatsoever.
  • Two expedited shipments with identical shipped_at.
  • is_expedited stored as NULL.
  • shipped_at stored as NULL.
  • NULL inside a join-key column.

What to Submit

Prose explaining the semantics of each report and a single production-ready SQL query for Part 2, plus a brief note on the behavior of the chosen query for each edge case above.

Hints

Hint 1

Ask yourself when each clause is evaluated: ON builds the join, WHERE filters the already-built result set.

Hint 2

A LEFT JOIN manufactures NULLs on the right side for unmatched left rows; a WHERE comparison against NULL yields UNKNOWN, so those rows are discarded.

Hint 3

To guarantee one row per order, first reduce the shipment side to a single winner per composite key (e.g. ROW_NUMBER() partitioned by order_id, marketplace_id), then LEFT JOIN that reduced set.