MySQL JOINs explained with simple tables

Intermediate  Nov 3, 2026

Combine rows from two tables with INNER JOIN and LEFT JOIN.

MySQL JOINs explained with simple tables
The steps of this tutorial at a glance.
The steps of this tutorial at a glance.

Two tables

CREATE TABLE customers (id INT PRIMARY KEY, name VARCHAR(50));
CREATE TABLE orders (id INT PRIMARY KEY, customer_id INT, total DECIMAL(8,2));
INSERT INTO customers VALUES (1,'Ali'),(2,'Sara'),(3,'Omar');
INSERT INTO orders VALUES (10,1,50.00),(11,1,20.00),(12,2,75.00);

Ali has two orders, Sara has one, Omar has none.

The table described by the SQL above.
The table described by the SQL above.
How the code in this section fits into the page.
How the code in this section fits into the page.

INNER JOIN: only matching rows

SELECT c.name, o.total
FROM customers c
INNER JOIN orders o ON o.customer_id = c.id;

Omar is missing, because he has no order.

LEFT JOIN: keep every row from the left table

SELECT c.name, o.total
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id;

Omar appears with NULL for the total.

LEFT JOIN: keep every row from the left table
LEFT JOIN: keep every row from the left table

Find rows with no match

SELECT c.name
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.id IS NULL;

Tips

  • Always join on the key columns, and write the ON condition. Forgetting it makes every row match every row.
  • Give tables short aliases (c, o) and prefix every column with them.
  • Index the joined columns (orders.customer_id) so joins stay fast.
The main points of this section.
The main points of this section.

More tutorials