Joins
Combine rows from related tables — inner joins, left joins, and how to read them.
- Combine two tables with an INNER JOIN on a key
- Keep unmatched rows with a LEFT JOIN
- Join more than two tables
The relational model splits data across tables linked by keys (the foreign keys from the first lesson). A join stitches them back together so you can answer questions that span tables — like "which customer placed each order?"
INNER JOIN
An inner join matches rows where the join condition holds, dropping rows with no
match. Match orders.customer_id to customers.id:
Each result row pairs an order with its customer. Table aliases keep it tidy —
FROM orders o JOIN customers c ON c.id = o.customer_id, then write c.name.
LEFT JOIN keeps unmatched rows
An inner join drops customers who have no orders. A LEFT JOIN keeps every
row from the left table, filling unmatched right-side columns with NULL:
Customers with no orders (Alan, Edsger) still appear, with NULL for order_id.
That's how you find "customers who never ordered": ... WHERE o.id IS NULL.
Joining several tables
Chain joins to traverse the schema. "What products are on each order?" needs
orders → order_items → products:
Where to go next
Joins combine tables side by side. Next: subqueries — using one query's result inside another.