Reason About Composite Join Keys and Predicate Placement
Quick Overview
Reason about a two-column SQL join key and a right-side predicate without assuming a schema or join type. Compare inner- and left-join semantics, cardinality checks, and safe query shapes.
Reason About Composite Join Keys and Predicate Placement
Company: Amazon
Role: Business Intelligence Engineer
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Technical Screen
A SQL review prompt says that two relations must be matched on a composite key with columns `(key_a, key_b)`. The query under review joins on only `key_a` and places a predicate that references only the right relation in `WHERE` rather than `ON`. The source does not specify table names, the predicate itself, the intended join type, or whether unmatched left rows should survive.
Explain the defect in the incomplete join key. Then analyze predicate placement separately for an `INNER JOIN` and a `LEFT JOIN`, stating the conditions under which moving the right-side predicate between `ON` and `WHERE` preserves results. Give generic query shapes for both “discard unmatched rows” and “retain unmatched rows,” and explain how you would detect unintended row multiplication.
### Constraints & Assumptions
- Do not assume a particular business schema, flag name, or cardinality guarantee.
- Use `L` and `R` for the left and right relations and `right_condition` for the right-only predicate.
- State any uniqueness requirement instead of treating it as given.
### Clarifying Questions to Ask
- Is the intended operation an inner join or an outer join?
- Should a left row with no qualifying right match remain in the output?
- Is `(key_a, key_b)` unique on the right at the required grain?
- Can `right_condition` evaluate to `NULL`, and should that case qualify?
```hint Follow the unmatched row
For a left join, determine the right-column values created for an unmatched left row and then apply the `WHERE` predicate to that row.
```
### What a Strong Answer Covers
- Why omitting either composite-key column can create false matches and duplicates.
- Match eligibility in `ON` versus post-join filtering in `WHERE`.
- Equivalence for a right-only filter under ordinary inner-join semantics.
- Non-equivalence for a left join when unmatched left rows must remain.
- Key uniqueness checks, matches-per-left-row diagnostics, and aggregate reconciliation.
### Follow-up Questions
- How do null-rejecting and null-accepting predicates differ after a left join?
- How would effective-dated matching change the key and predicate logic?
- When is a many-to-many join intentional?
Quick Answer: Reason about a two-column SQL join key and a right-side predicate without assuming a schema or join type. Compare inner- and left-join semantics, cardinality checks, and safe query shapes.
A SQL review prompt says that two relations must be matched on a composite key with columns (key_a, key_b). The query under review joins on only key_a and places a predicate that references only the right relation in WHERE rather than ON. The source does not specify table names, the predicate itself, the intended join type, or whether unmatched left rows should survive.
Explain the defect in the incomplete join key. Then analyze predicate placement separately for an INNER JOIN and a LEFT JOIN, stating the conditions under which moving the right-side predicate between ON and WHERE preserves results. Give generic query shapes for both “discard unmatched rows” and “retain unmatched rows,” and explain how you would detect unintended row multiplication.
Constraints & Assumptions
Do not assume a particular business schema, flag name, or cardinality guarantee.
Use
L
and
R
for the left and right relations and
right_condition
for the right-only predicate.
State any uniqueness requirement instead of treating it as given.
Clarifying Questions to Ask Guidance
Is the intended operation an inner join or an outer join?
Should a left row with no qualifying right match remain in the output?
Is
(key_a, key_b)
unique on the right at the required grain?
Can
right_condition
evaluate to
NULL
, and should that case qualify?
What a Strong Answer Covers Guidance
Why omitting either composite-key column can create false matches and duplicates.
Match eligibility in
ON
versus post-join filtering in
WHERE
.
Equivalence for a right-only filter under ordinary inner-join semantics.
Non-equivalence for a left join when unmatched left rows must remain.
Key uniqueness checks, matches-per-left-row diagnostics, and aggregate reconciliation.
Follow-up Questions Guidance
How do null-rejecting and null-accepting predicates differ after a left join?
How would effective-dated matching change the key and predicate logic?