Quick reference · SQL joins
Join rows by what they share
A join combines rows from two tables using a match rule. Start with the question: should unmatched rows appear?
SELECT c.name, o.id
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.id;
| Join | Rows kept | Unmatched side |
|---|---|---|
INNER JOIN | Only pairs that match | Dropped from both sides |
LEFT JOIN | All left rows, plus matches | Right columns become NULL |
RIGHT JOIN | All right rows, plus matches | Left columns become NULL |
FULL OUTER JOIN | All rows from both sides | Missing side becomes NULL |
CROSS JOIN | Every possible pair | No matching condition |
Three checks that prevent common mistakes
- Ask what one output row represents. A row can repeat when one row matches several rows.
- For a left join, put right-table match restrictions in
ONwhen unmatched left rows must remain. A right-table filter inWHEREremoves theNULLrows. - Select named columns and qualify them (
c.id) when names may overlap.
USING (id) is concise when both tables share that key name. Avoid NATURAL JOIN in routine queries: a later column-name overlap can silently change the match rule.
For the full walkthrough, see Lesson 1: SQL joins refresher.