Your First Join: INNER JOIN
The Matching Game
The sales team needed a report that combined order amounts with customer names. The data lived in two separate tables. It was time for my first real join.
The Quest: The Intersection
An `INNER JOIN` returns only rows that have a match in **both** tables. If an order has a `customer_id` that doesn't exist in the `customers` table, that order will be excluded from the results.
Think of it as the "Intersection" of a Venn Diagram.
The Implementation: The Syntax
SELECT
o.order_id,
o.order_amount,
c.name,
c.email
FROM orders o
INNER JOIN customers c ON o.customer_id = c.id;
Breaking It Down
1. **FROM orders o**: Start with the `orders` table, alias it as `o`.
2. **INNER JOIN customers c**: Bring in the `customers` table, alias it as `c`.
3. **ON o.customer_id = c.id**: This is the "Key" that links them. Match rows where these two columns are equal.
The "Oops" Moment
I once got back fewer rows than I expected. Some orders had a NULL `customer_id` (guest checkouts). Since NULL doesn't match anything, those rows were silently dropped.
**Pro Tip**: Always compare your row count before and after a join. If it drops, investigate why. Are there orphan keys or NULLs?
The Victory
The sales report now showed "John Doe: $500, Jane Smith: $1,200..." instead of just "Customer 101: $500." The data finally made sense to a human.
Your Task for Today
Write an `INNER JOIN` between two tables in your database. Compare the row count of the joined table to the original source table.
*Day 33: Preserving Data—LEFT JOIN.*