Stop 06 · Joining TablesTopics · 2/10

INNER JOIN

Part of the SQL Roadmap

In short

An orders table only stores customer_id 1042, not 'Ananya Iyer from Chennai'. INNER JOIN is how you put the name next to the order, and it's the join you'll write most often.

INNER JOIN returns only rows that have a match in both tables, based on the join condition in ON.

The jaadu stands behind a wooden temple shoe rack, handing a pair of sandals to a devotee holding a matching brass token, while a few unclaimed sandals sit on a lower shelf.

Think of it like this

The temple shoe counter

At a temple's shoe counter, you hand over your chappals and get a numbered token. On the way out, each token is matched with its pair of chappals. A token with no chappals, or chappals with no token, simply aren't handed out. INNER JOIN returns only the pairs where the number matches on both sides.

Watch it happen

Matching orders to customers on customer_id

Order 1 → C1
0
Order 2 → C2
1
Order 3 → C9
2
Order 4 → C1
3

1/5Four orders. The customers table has C1 (Ananya) and C2 (Rohit), but no C9.

The big ideas

What you need to know

Only matches survive

A row appears only if the ON condition finds a partner in the other table. Unmatched rows on either side are dropped.

Join on keys

ON o.customer_id = c.customer_id links a foreign key to a primary key. Joining on names or cities invites wrong matches.

Alias every table

orders AS o, customers AS c. Then prefix every column, like c.name, so nothing is ambiguous.

JOIN means INNER JOIN

Writing just JOIN gives you an inner join. Writing INNER makes your intent clearer to readers.

See it in code

Walk through the code

sql
1SELECT2  o.order_id,3  o.order_date,4  c.name AS customer,5  c.city,6  o.amount7FROM orders AS o8INNER JOIN customers AS c9  ON c.customer_id = o.customer_id10WHERE o.order_date >= DATE '2026-11-01'11ORDER BY o.order_date, o.order_id;

1/3Columns come from both tables, each prefixed with its alias.

November orders with the customer's name and city next to each one. The syntax is identical in PostgreSQL, MySQL and SQL Server.

Save yourself the pain

Common mistakes

Oops

SELECT customer_id FROM orders JOIN customers ON ...

Do this instead

Both tables have customer_id, so the database says it's ambiguous. Write o.customer_id or c.customer_id.

Oops

Assuming the joined result has as many rows as the orders table.

Do this instead

Orders with a missing or invalid customer_id are dropped. Count rows before and after the join to check.

Oops

Joining on c.name = o.customer_name.

Do this instead

Two customers can share a name, and spellings vary. Join on the ID columns.

Your mission

Put names on orders

~15 min

Quick check

orders has 100 rows. 5 of them have a customer_id that isn't in customers. How many rows does orders INNER JOIN customers return?

Go deeper

  1. 1Join orders to customers and show the customer name next to each order.
  2. 2Always use table aliases and prefix columns, like o.customer_id.
  3. 3Compare row counts before and after the join.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge