Tutorials › SQL Tutorial › SQL INNER JOIN

SQL INNER JOIN

10 min read · Lesson 15 of 24

In a well-designed database, information is split across tables. The orders table knows which customer placed an order (customer_id), but the customer's name and city live in customers. A join combines rows from two tables into one result, matching them on a shared column.

Why we need joins

Look at the orders on their own:

Example SQL
SELECT order_id, customer_id, order_date
FROM orders;
Try it Yourself »

customer_id 2 is not very meaningful to a person. We want "Savannah Tech". To get it, we match each order to the customer whose customer_id is the same.

INNER JOIN syntax

SELECT columns
FROM table1
INNER JOIN table2
  ON table1.column = table2.column;

The ON clause gives the join condition: which rows belong together. Most of the time you join a foreign key (orders.customer_id) to the primary key it points to (customers.customer_id).

Your first join

Example SQL
SELECT orders.order_id,
       orders.order_date,
       customers.name,
       customers.city
FROM orders
INNER JOIN customers
  ON orders.customer_id = customers.customer_id;
Try it Yourself »

For each order, the database finds the customer with the matching ID and glues the two rows together. Each order appears once, now with its customer's name and city.

Because both tables have a customer_id column, you must say which table you mean by writing table.column. Table aliases (from Aliases) make this much shorter:

Example SQL
SELECT o.order_id, o.status, c.name AS customer, c.country
FROM orders AS o
INNER JOIN customers AS c ON o.customer_id = c.customer_id
ORDER BY c.name;
Try it Yourself »
Tip: The word INNER is optional: JOIN on its own means INNER JOIN. Many people write JOIN for short.

What "inner" means

An inner join returns only rows that have a match in both tables. Arusha Outfitters, Nyeri Stationers and Homa Bay Fresh have never placed an order, so none of them appear in the results above. If you need to include rows without a match, use a LEFT JOIN.

Joining order lines to products

The same idea works between order_items and products. Show every order line with the product name and price:

Example SQL
SELECT oi.order_id, p.name AS product, oi.quantity, p.price
FROM order_items AS oi
JOIN products AS p ON oi.product_id = p.product_id
ORDER BY oi.order_id;
Try it Yourself »

Calculating with joined columns

Once tables are joined you can use columns from both in calculations, filters and groups. Line totals (quantity × price) for order 112:

Example SQL
SELECT p.name, oi.quantity, p.price,
       oi.quantity * p.price AS line_total
FROM order_items AS oi
JOIN products AS p ON oi.product_id = p.product_id
WHERE oi.order_id = 112;
Try it Yourself »

And combining with GROUP BY: the number of orders for each customer, using names instead of IDs:

Example SQL
SELECT c.name, COUNT(*) AS orders
FROM customers AS c
JOIN orders AS o ON o.customer_id = c.customer_id
GROUP BY c.customer_id, c.name
ORDER BY orders DESC, c.name;
Try it Yourself »

Another way you may see joins written

Older code often lists tables with commas and puts the join condition in WHERE:

SELECT o.order_id, c.name
FROM orders o, customers c
WHERE o.customer_id = c.customer_id;

This gives the same result, but if you forget the condition, you get every order paired with every customer (12 × 12 = 144 rows), called a Cartesian product. The explicit JOIN ... ON style keeps the join condition next to the join and is much harder to get wrong.

Warning: A missing or wrong ON condition is the most common cause of joins returning far too many rows. If a result looks suspiciously large, check the ON clause first.

Other join types

JoinReturns
INNER JOINonly rows with a match in both tables
LEFT JOINall rows from the left table, plus matches from the right
RIGHT JOINall rows from the right table, plus matches from the left
FULL OUTER JOINall rows from both tables, matched where possible
CROSS JOINevery row of one table paired with every row of the other

The next lesson covers LEFT JOIN and joining three or more tables.

Exercise

Complete the join between orders and customers.

SELECT o.order_id, c.name FROM orders o  customers c  o.customer_id = c.customer_id;
Exercise

Show each order line with its product name.

SELECT oi.order_id, p.name FROM order_items oi JOIN products p ON oi.product_id = p.;
Sign in or create a free account to track progress, earn XP, badges and a certificate.