202 — Joining Tables

Intermediate

Connect related tables to build complete pictures of your data. Joins are the most powerful tool in SQL — they let you answer questions that span multiple tables, like 'what did each customer order and how much did they spend?'

Learning Objectives

1
Understand why relational databases split data across tables
2
Write INNER JOIN queries with proper ON conditions
3
Use LEFT JOIN to include rows with no matches
4
Join three or more tables in a single query
5
Combine JOINs with GROUP BY for aggregated reports
6
Recognize and avoid accidental Cartesian products
7
Notice when one-to-many joins multiply rows before aggregation
Step 1

Why joins exist

Examine how the e-commerce database splits data across tables and why you need joins to put it back together.

Commands to Run

SELECT * FROM orders LIMIT 5;
SELECT * FROM customers LIMIT 5;

What This Does

Notice that the orders table stores customer_id, not the customer's name or email. This is called normalization — storing each fact in one place to avoid duplication and inconsistency. If a customer changes their email, you update one row in customers rather than thousands of rows in orders. The trade-off is that you need JOINs to reassemble the full picture.

Expected Outcome

The orders table shows customer_id as a number, not a name. To see who placed each order, you need to look up that ID in the customers table.

Pro Tips

  • 1
    Foreign keys (like customer_id in orders) are the 'links' between tables
  • 2
    Think of each table as a spreadsheet tab — JOINs let you combine tabs based on matching values
Was this step helpful?

All Steps (0 / 14 completed)