SQL ยท Chapter 16 of 42

INNER JOIN

INNER JOIN returns rows where the join condition matches in BOTH tables. Non-matching rows are excluded.

Implicit vs explicit

Old style: `FROM a, b WHERE a.id = b.a_id`. Modern: `FROM a JOIN b ON a.id = b.a_id`. Always use the modern form.

Multiple joins

Chain multiple JOINs left-to-right; each ON refers to already-joined tables.

Example 1 (sql)
SELECT u.name, o.total
FROM users u
INNER JOIN orders o ON o.user_id = u.id
WHERE o.total > 500;
Output
users with big orders

Join + filter.

Example 2 (sql)
SELECT o.id, u.name, p.name
FROM orders o
JOIN users u ON u.id = o.user_id
JOIN products p ON p.id = o.product_id;
Output
orders enriched with user and product

Three-way join.

Key points

  • Returns only matched rows.
  • Use ON for the match condition.
  • You can chain many JOINs.
  • Prefer explicit JOIN syntax.
๐Ÿ’ก Note: `JOIN` without a prefix defaults to `INNER JOIN` in every mainstream database.

๐Ÿ“ Quick Quiz

1. INNER JOIN returns rows that:

2. What does JOIN without INNER/LEFT default to?

3. Modern JOIN syntax uses: