Reason About Composite Join Keys and Predicate Placement
AmazonBusiness Intelligence Engineer · Technical ScreenLast 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_dateshipments: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
orderswithorder_date >= '2023-01-01'. - Join with
shipmentson 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 withNULLfor 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_attimestamp.
Input Example
orders:
| order_id | marketplace_id | seller_id | order_date |
|---|---|---|---|
| 101 | US | S1 | 2023-02-01 |
| 102 | US | S2 | 2023-02-05 |
| 103 | UK | S1 | 2023-02-10 |
shipments:
| shipment_id | marketplace_id | order_id | carrier | is_expedited | shipped_at |
|---|---|---|---|---|---|
| 1 | US | 101 | UPS | TRUE | 2023-02-02 10:00:00 |
| 2 | US | 101 | FedEx | TRUE | 2023-02-02 14:00:00 |
| 3 | US | 102 | USPS | FALSE | 2023-02-06 09:00:00 |
Output Example
| order_id | marketplace_id | seller_id | carrier | shipped_at |
|---|---|---|---|---|
| 101 | US | S1 | FedEx | 2023-02-02 14:00:00 |
| 102 | US | S2 | NULL | NULL |
| 103 | UK | S1 | NULL | NULL |
Edge Cases to Consider
- Orders that have only non-expedited shipments.
- Orders that have zero shipments.
- Multiple shipments with the exact same
shipped_attimestamp (handle deterministically via tie-breaker such asshipment_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?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.