Why Joins?
Data is split across tables to avoid repetition. Customers live in customers and their purchases in orders. A join puts them back together using a shared column.
INNER JOIN
Keeps only rows that match on both sides:
Result:
| name | order_id | order_date | status |
|---|---|---|---|
| Asha Rao | 1 | 2025-01-10 | shipped |
| Asha Rao | 2 | 2025-02-14 | shipped |
| Ben Carter | 3 | 2025-02-20 | shipped |
| Chen Wei | 4 | 2025-03-01 | pending |
| Ben Carter | 5 | 2025-03-15 | cancelled |
| Eli Brown | 6 | 2025-04-02 | shipped |
| Asha Rao | 7 | 2025-04-20 | pending |
| Chen Wei | 8 | 2025-05-05 | shipped |
| Fatima Khan | 9 | 2025-05-11 | shipped |
| Ben Carter | 10 | 2025-06-01 | shipped |
Dina has no orders, so she is missing from the result.
LEFT JOIN
Keeps every row of the left table. Unmatched right columns become NULL:
Result:
| name | order_id |
|---|---|
| Asha Rao | 1 |
| Asha Rao | 2 |
| Asha Rao | 7 |
| Ben Carter | 3 |
| Ben Carter | 5 |
| Ben Carter | 10 |
| Chen Wei | 4 |
| Chen Wei | 8 |
| Dina Cohen | NULL |
| Eli Brown | 6 |
| Fatima Khan | 9 |
Anti Join: Rows with No Match
Result:
| name |
|---|
| Dina Cohen |
The same idea with NOT EXISTS is often clearer and equally fast in MySQL 8:
Result:
| name |
|---|
| Dina Cohen |
WHERE turns a LEFT JOIN back into an INNER JOIN. Put it in ON to keep unmatched rows.
Result:
| name | shipped_orders |
|---|---|
| Asha Rao | 2 |
| Ben Carter | 2 |
| Chen Wei | 1 |
| Dina Cohen | 0 |
| Eli Brown | 1 |
| Fatima Khan | 1 |
RIGHT JOIN
The mirror image of a LEFT JOIN. Most people just swap the tables and use LEFT JOIN:
Result:
| name | order_id |
|---|---|
| Dina Cohen | NULL |
| Eli Brown | 6 |
Joining Three Tables
Result:
| order_id | customer | product | quantity | line_total |
|---|---|---|---|---|
| 1 | Asha Rao | Laptop | 1 | 899.00 |
| 1 | Asha Rao | Headphones | 2 | 158.00 |
| 2 | Asha Rao | Notebook | 10 | 35.00 |
| 2 | Asha Rao | Pen Pack | 4 | 20.00 |
JOIN ... USING
When both columns have the same name, USING is shorter and shows the column once:
Result:
| order_id | name | quantity |
|---|---|---|
| 1 | Laptop | 1 |
| 1 | Headphones | 2 |
CROSS JOIN
Every row with every row (a Cartesian product). Useful for generating combinations:
Result:
| size | color |
|---|---|
| L | blue |
| L | red |
| M | blue |
| M | red |
| S | blue |
| S | red |
JOIN without ON is allowed and acts like a CROSS JOIN. A forgotten condition multiplies your rows. Always check row counts.
Self Join
Result:
| employee | manager |
|---|---|
| Zara | (top) |
| Yusuf | Zara |
| Xin | Zara |
| Wale | Yusuf |
FULL OUTER JOIN Workaround
MySQL has no FULL OUTER JOIN. Combine a LEFT and a RIGHT join with UNION:
Result:
| ak | bk | av | bv |
|---|---|---|---|
| NULL | 3 | NULL | b3 |
| 1 | NULL | a1 | NULL |
| 2 | 2 | a2 | b2 |
Joins and Duplicates
A one-to-many join repeats the parent row. Counting after the join can double count. Count orders and items separately:
Result:
| name | orders | items |
|---|---|---|
| Asha Rao | 3 | 20 |
| Ben Carter | 3 | 14 |
| Chen Wei | 2 | 24 |
| Eli Brown | 1 | 3 |
| Fatima Khan | 1 | 2 |
Common Beginner Mistakes
| Mistake | What happens | Fix |
|---|---|---|
Condition on the right table in WHERE after LEFT JOIN | Unmatched rows vanish | Move to ON |
| Missing join condition | Rows multiply | Always write ON |
COUNT(*) after a one-to-many join | Inflated counts | COUNT(DISTINCT ...) |
Expecting FULL JOIN | Syntax error | LEFT + RIGHT with UNION |
| Joining on columns of different types | Slow, no index | Same type on both sides |
Practice
- List each product with the number of units sold (include products never sold).
- Find customers who never ordered Electronics.
- Show employee and manager names with a self join.
- Build a FULL OUTER JOIN of two small tables.
Conclusion
Use INNER JOIN for matches, LEFT JOIN to keep one side, anti joins to find gaps, and UNION of left and right joins to imitate FULL JOIN. Put right-table filters in ON.

