Code of the Day
IntermediateCombining data

Joins

Combine rows from related tables — inner joins, left joins, and how to read them.

SQLIntermediate11 min read
Recommended first
By the end of this lesson you will be able to:
  • Combine two tables with an INNER JOIN on a key
  • Keep unmatched rows with a LEFT JOIN
  • Join more than two tables

The relational model splits data across tables linked by keys (the from the first lesson). A stitches them back together so you can answer questions that span tables — like "which customer placed each order?"

INNER JOIN

An inner join matches rows where the join condition holds, dropping rows with no match. Match orders.customer_id to customers.id:

SQL — editable, runs in your browser

Each result row pairs an order with its customer. Table aliases keep it tidy — FROM orders o JOIN customers c ON c.id = o.customer_id, then write c.name.

LEFT JOIN keeps unmatched rows

An inner join drops customers who have no orders. A LEFT JOIN keeps every row from the left table, filling unmatched right-side columns with NULL:

SQL — editable, runs in your browser

Customers with no orders (Alan, Edsger) still appear, with NULL for order_id. That's how you find "customers who never ordered": ... WHERE o.id IS NULL.

Joining several tables

Chain joins to traverse the schema. "What products are on each order?" needs orders → order_items → products:

SQL — editable, runs in your browser

Where to go next

Joins combine tables side by side. Next: subqueries — using one query's result inside another.

Finished reading? Mark it complete to track your progress.

On this page