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 ordersJoin + 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 productThree-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.
