Katkat

Reason About Composite Join Keys and Predicate Placement

AmazonBusiness Intelligence Engineer · Technical Screen

Last updated

This question tests your understanding of composite join keys, the critical semantic difference between filtering in the `ON` versus `WHERE` clause during joins, and techniques to prevent row fan-out.

Scenario

You are analyzing merchant order fulfillment across international marketplaces. You are working with two tables:

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

Tasks

1. Conceptual Analysis

Compare the behavior and row counts of the following two queries:

-- Query A
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;

-- Query B
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;

Explain how Query B fundamentally alters the LEFT JOIN semantics, and describe what happens to orders with no matching expedited shipments.

2. Write a Safe Query

Write a SQL query that fulfills the following requirements:

  • Return all rows from orders with order_date >= '2023-01-01'.
  • Join with shipments on the composite key (order_id, marketplace_id).
  • Include expedited shipment details (carrier, shipped_at). If an order has no expedited shipment, still return the order with NULL for shipment fields.
  • Prevent row multiplication: An order can have multiple expedited split shipments. For each order, return at most one matching shipment: the one with the latest shipped_at timestamp.

Input Example

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

Output Example

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

Edge Cases to Consider

  • Orders that have only non-expedited shipments.
  • Orders that have zero shipments.
  • Multiple shipments with the exact same shipped_at timestamp (handle deterministically via tie-breaker such as shipment_id).

Hints

Hint 1

A WHERE clause predicate on a right-side table filters rows after the join is performed, which eliminates rows where the right side produced NULLs.

Hint 2

To preserve all rows from the left table, any right-side predicate must either reside in the ON clause or be applied to the right table in a subquery/CTE before joining.

Hint 3

Use a window function such as `ROW_NUMBER() OVER (PARTITION BY ... ORDER BY ...)` within a CTE to isolate the latest shipment per composite key prior to the join.

Follow-up questions

  • How does SQL evaluate the join if one of the composite key columns contains a `NULL` value in both tables?
  • If the `shipments` table contains billions of rows, how does filtering in a pre-aggregated CTE compare in performance against filtering in the `ON` clause?
  • What assertion query would you write in a data pipeline to verify that your query preserved the exact row count of the `orders` table?