INNER JOIN: combine two tables

Bring columns from related tables together using the keys that link them.

Module 4: Joining tables · Lesson 13 of 26 · 16 min read · Free

orders says order 1 was at branch_id 2. That's useless in a report — people want Lekki. The branch name lives in branches. A JOIN matches rows from two tables using the key that links them:

SELECT orders.order_id, orders.order_date, branches.name AS branch
FROM orders
JOIN branches ON orders.branch_id = branches.branch_id
LIMIT 10;

Read it as: for each order, find the branch whose branch_id equals the order's branch_id, and put them side by side.

  • JOIN (also written INNER JOIN) — the second table.
  • ON — the matching rule, almost always foreign key = primary key.
  • table.column — needed when both tables have a column with the same name (branch_id, or name, or unit_price).

Table aliases

Writing orders. and branches. everywhere gets tiring. Give each table a short alias:

SELECT o.order_id, o.order_date, b.name AS branch
FROM orders AS o
JOIN branches AS b ON o.branch_id = b.branch_id
LIMIT 10;

Professionals almost always use short aliases. (AS is optional: FROM orders o works too.)

Order lines with product names

SELECT oi.order_id, p.name, oi.quantity, oi.unit_price,
       oi.quantity * oi.unit_price AS line_total
FROM order_items AS oi
JOIN products AS p ON oi.product_id = p.product_id
LIMIT 10;

Order 1 had Dangote Sugar (3 × ₦1,100), Hypo Bleach, Eva Water and Lipton — now readable, not just numbers.

What INNER means

An inner join keeps only rows that match on both sides. Thirty walk-in orders have no customer_id, so

SELECT COUNT(*)
FROM orders o
JOIN customers c ON o.customer_id = c.customer_id;

gives 2,970, not 3,000. Those 30 orders vanish silently. That's sometimes what you want — and sometimes a costly mistake. Lesson 4.3 shows how to keep them.

Check yourself

Answer in your head first, then open each question to see the answer and why.

  1. What does ON o.branch_id = b.branch_id do?

    • A Sorts the rows
    • B Says which rows of the two tables belong together
    • C Filters to one branch
    • D Creates a new table
    Show the answer

    B) Says which rows of the two tables belong together — ON is the matching rule for the join.

  2. An INNER JOIN between orders and customers returns 2,970 rows, but orders has 3,000. Why?

    • A 30 orders have no matching customer, so inner join drops them
    • B The query is wrong
    • C Joins always lose 1%
    • D There are 30 duplicate customers
    Show the answer

    A) 30 orders have no matching customer, so inner join drops them — Inner joins keep only rows that match on both sides.

  3. Why write p.name instead of just name?

    • A To say which table's column you mean when names clash
    • B It runs faster
    • C SQL requires a dot everywhere
    • D To create an alias
    Show the answer

    A) To say which table's column you mean when names clash — Several tables have a name column; the alias makes it clear.

More free lessons: What SQL is, and why employers want it · Install DB Browser and open the practice database · WHERE: only the rows you need · COUNT, SUM, AVG, MIN and MAX · CASE: if-then logic inside a query · INSERT, UPDATE and DELETE — safely · From a business question to an answer