MySQL joins combine rows from two tables by a matching column: SELECT ... FROM orders o JOIN customers c ON c.customer_id = o.customer_id; INNER JOIN keeps only matches, LEFT and RIGHT JOIN keep one side whole, and CROSS JOIN pairs every row.
The catch most people hit: MySQL has no FULL OUTER JOIN. Worse, one way of writing it doesn’t fail at all. It quietly runs as an inner join and drops rows.
Tested on: MySQL 8.4.9 Community on Windows 11, mysql command-line client. Below: INNER, LEFT, RIGHT, self and CROSS joins, three tables, USING vs ON, the FULL OUTER JOIN workaround, unmatched rows and inflated totals.
Here’s what we’re building: a sales report for a small online kitchen store. It lists every customer, how many orders they placed and what they spent, including the customer who never ordered.

Olivia Carter shows 0 orders and $0.00 instead of vanishing. That row is why it’s a LEFT JOIN.
Before You Start: The Sample Tables for Every MySQL Join
You need a MySQL server you can log in to (here’s how to install MySQL on Windows) and a test database. I made one called dfq_mysqljoins. Run this script once; every example below reuses it:
DROP TABLE IF EXISTS payments, order_items, products,
orders, customers, employees, ship_methods;
CREATE TABLE customers (
customer_id INT PRIMARY KEY,
name VARCHAR(20) NOT NULL,
state CHAR(2) NOT NULL -- home state
);
CREATE TABLE orders (
order_id INT PRIMARY KEY,
customer_id INT NULL, -- NULL = walk-in sale
order_date DATE NOT NULL,
state CHAR(2) NOT NULL, -- ship-to state
total DECIMAL(8,2) NOT NULL
);
CREATE TABLE products (product_id INT PRIMARY KEY,
product VARCHAR(15), price DECIMAL(8,2));
CREATE TABLE order_items (order_id INT, product_id INT, qty INT,
PRIMARY KEY (order_id, product_id));
CREATE TABLE payments (payment_id INT PRIMARY KEY,
order_id INT, amount DECIMAL(8,2));
CREATE TABLE employees (employee_id INT PRIMARY KEY,
name VARCHAR(20), manager_id INT NULL);
CREATE TABLE ship_methods (method VARCHAR(10), fee DECIMAL(5,2));
INSERT INTO customers VALUES
(1,'Hannah Brooks','OH'), (2,'Tyler Reed','TX'), (3,'Grace Miller','OH'),
(4,'Marcus Bennett','FL'), (5,'Olivia Carter','WA');
INSERT INTO orders VALUES
(101,1,'2026-01-05','OH',120.00), (102,2,'2026-01-09','TX',75.50),
(103,1,'2026-02-02','KY',210.00), (104,3,'2026-02-14','OH',46.50),
(105,4,'2026-03-01','FL',105.00), (106,NULL,'2026-03-03','OH',31.00);
INSERT INTO products VALUES
(1,'Coffee Maker',60.00), (2,'Mug Set',15.50), (3,'Grinder',45.00);
INSERT INTO order_items VALUES (101,1,2), (102,1,1), (102,2,1),
(103,1,2), (103,3,2), (104,2,3), (105,1,1), (105,3,1), (106,2,2);
INSERT INTO payments VALUES (1,101,120.00), (2,102,75.50),
(3,103,105.00), (4,103,105.00), (5,104,46.50), (6,105,105.00), (7,106,31.00);
INSERT INTO employees VALUES (1,'Rachel Foster',NULL),
(2,'Daniel Hughes',1), (3,'Emily Sanders',1), (4,'Kevin Price',2);
INSERT INTO ship_methods VALUES ('Ground',5.00), ('Express',15.00);
SELECT COUNT(*) AS customers FROM customers;
Check your work:
+-----------+
| customers |
+-----------+
| 5 |
+-----------+
Three details in this data drive the examples:
| Row | Why it’s there |
|---|---|
| Olivia Carter (customer 5) | Has no orders, so INNER JOIN drops her |
| Order 106 | customer_id is NULL (a walk-in sale), so it has no customer |
| Order 103 | Two payments of $105.00, which doubles it in a careless SUM |
Both customers and orders also have a state column. That’s deliberate, and it matters for NATURAL JOIN later. Column types are explained in MySQL data types.
Step 1: Match Rows with MySQL INNER JOIN
Goal: list each order with the customer’s name. INNER JOIN returns a row only when the ON condition finds a match on both sides.
SELECT o.order_id, c.name, o.order_date, o.total
FROM orders AS o
INNER JOIN customers AS c ON c.customer_id = o.customer_id
ORDER BY o.order_id;
-- old comma style: same match
SELECT COUNT(*) AS comma_join_rows
FROM orders o, customers c
WHERE c.customer_id = o.customer_id;
Result:
+----------+----------------+------------+--------+
| order_id | name | order_date | total |
+----------+----------------+------------+--------+
| 101 | Hannah Brooks | 2026-01-05 | 120.00 |
| 102 | Tyler Reed | 2026-01-09 | 75.50 |
| 103 | Hannah Brooks | 2026-02-02 | 210.00 |
| 104 | Grace Miller | 2026-02-14 | 46.50 |
| 105 | Marcus Bennett | 2026-03-01 | 105.00 |
+----------+----------------+------------+--------+
+-----------------+
| comma_join_rows |
+-----------------+
| 5 |
+-----------------+

That returns 5 rows. Order 106 is gone because its NULL customer_id matches nothing, and Olivia is gone because she has no order. JOIN on its own means INNER JOIN.
The second query is the old comma style. A comma plus WHERE matches the same 5 rows, so it’s an inner join too.
I’d still write JOIN ... ON. Forget the WHERE on a comma join and you get a cross join of every order with every customer.
Checkpoint: 5 rows, and comma_join_rows is 5.
Step 2: Keep Every Customer with MySQL LEFT JOIN and Find Unmatched Rows
Goal: every customer, with their orders when they have any. LEFT JOIN keeps all rows from the left table and fills the right side with NULL when nothing matches.
SELECT c.name, o.order_id, o.total
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.customer_id
ORDER BY c.customer_id, o.order_id;
-- customers who never ordered
SELECT c.name, c.state
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.customer_id
WHERE o.order_id IS NULL;
What comes back:
+----------------+----------+--------+
| name | order_id | total |
+----------------+----------+--------+
| Hannah Brooks | 101 | 120.00 |
| Hannah Brooks | 103 | 210.00 |
| Tyler Reed | 102 | 75.50 |
| Grace Miller | 104 | 46.50 |
| Marcus Bennett | 105 | 105.00 |
| Olivia Carter | NULL | NULL |
+----------------+----------+--------+
+---------------+-------+
| name | state |
+---------------+-------+
| Olivia Carter | WA |
+---------------+-------+

You should see 6 rows, with Olivia once at the bottom showing NULL. The second query adds WHERE o.order_id IS NULL, which keeps only the customers with no match. It returns Olivia Carter, WA.
Common mistake: test a column that can’t be NULL in a real match, like the primary key o.order_id. Testing a nullable column such as o.customer_id can give false hits.
Checkpoint: one unmatched customer. The same pattern in SQL Server is covered in INNER JOIN vs LEFT JOIN.
Step 3: Keep Every Order with MySQL RIGHT JOIN
Goal: every order, with a name when there is one. RIGHT JOIN keeps the right table whole:
SELECT o.order_id, o.total, c.name
FROM customers AS c
RIGHT JOIN orders AS o ON o.customer_id = c.customer_id
ORDER BY o.order_id;
Output:
+----------+--------+----------------+
| order_id | total | name |
+----------+--------+----------------+
| 101 | 120.00 | Hannah Brooks |
| 102 | 75.50 | Tyler Reed |
| 103 | 210.00 | Hannah Brooks |
| 104 | 46.50 | Grace Miller |
| 105 | 105.00 | Marcus Bennett |
| 106 | 31.00 | NULL |
+----------+--------+----------------+
Order 106 now shows up with $31.00 and a NULL name. Swap the tables and a LEFT JOIN gives the same rows. MySQL’s manual recommends LEFT JOIN for portability, and the optimizer rewrites right joins as left joins anyway.
You’ll see the same advice for RIGHT JOIN in SQL Server.
Step 4: Join Multiple Tables in MySQL (Three or More)
Goal: the products in each order placed on or after 02/01/2026. Add one JOIN ... ON per table; MySQL reads them left to right:
SELECT o.order_id, c.name, p.product, i.qty,
i.qty * p.price AS line_usd
FROM orders AS o
JOIN customers AS c ON c.customer_id = o.customer_id
JOIN order_items AS i ON i.order_id = o.order_id
JOIN products AS p ON p.product_id = i.product_id
WHERE o.order_date >= '2026-02-01'
ORDER BY o.order_id, p.product;
Result:
+----------+----------------+--------------+------+----------+
| order_id | name | product | qty | line_usd |
+----------+----------------+--------------+------+----------+
| 103 | Hannah Brooks | Coffee Maker | 2 | 120.00 |
| 103 | Hannah Brooks | Grinder | 2 | 90.00 |
| 104 | Grace Miller | Mug Set | 3 | 46.50 |
| 105 | Marcus Bennett | Coffee Maker | 1 | 60.00 |
| 105 | Marcus Bennett | Grinder | 1 | 45.00 |
+----------+----------------+--------------+------+----------+
That returns 5 line items from orders 103, 104 and 105. Order 103’s lines add up to $210.00, its order total. Each ON only links the new table to one already in the query.
Mixing in a LEFT JOIN? Make the later joins LEFT too, or they drop the NULL rows again. See the PostgreSQL multiple joins guide.
Step 5: Join a Table to Itself (MySQL Self Join)
Goal: each employee next to their manager. A self join uses the same table twice with two aliases:
SELECT e.name AS employee, m.name AS manager
FROM employees AS e
LEFT JOIN employees AS m ON m.employee_id = e.manager_id
ORDER BY e.employee_id;
Rows returned:
+---------------+---------------+
| employee | manager |
+---------------+---------------+
| Rachel Foster | NULL |
| Daniel Hughes | Rachel Foster |
| Emily Sanders | Rachel Foster |
| Kevin Price | Daniel Hughes |
+---------------+---------------+
Kevin Price reports to Daniel Hughes, and Rachel Foster has NULL because she’s the top manager. I used LEFT JOIN so she stays; an inner self join returns 3 rows.
The SQL Server self join guide covers the same idea in T-SQL.
Step 6: Build Every Combination with MySQL CROSS JOIN
Goal: a delivered price for every product and shipping method. CROSS JOIN has no ON clause; it pairs each row with every row:
SELECT p.product, s.method, p.price + s.fee AS delivered_usd
FROM products AS p
CROSS JOIN ship_methods AS s
ORDER BY p.product, s.fee;
Output:
+--------------+---------+---------------+
| product | method | delivered_usd |
+--------------+---------+---------------+
| Coffee Maker | Ground | 65.00 |
| Coffee Maker | Express | 75.00 |
| Grinder | Ground | 50.00 |
| Grinder | Express | 60.00 |
| Mug Set | Ground | 20.50 |
| Mug Set | Express | 30.50 |
+--------------+---------+---------------+
3 products times 2 methods gives 6 rows, and the Mug Set ships Ground for $20.50.
Keep both tables small. Two 1,000-row tables give a million rows. SQL Server’s CROSS JOIN works the same way.
USING vs ON in MySQL, and Why NATURAL JOIN Bites
When both columns share a name, USING (order_id) is shorter than ON:
SELECT * FROM orders
JOIN order_items USING (order_id)
WHERE order_id = 104;
-- NATURAL JOIN matches EVERY same-named column
SELECT COUNT(*) AS natural_rows FROM customers NATURAL JOIN orders;
Result:
+----------+-------------+------------+-------+-------+------------+------+
| order_id | customer_id | order_date | state | total | product_id | qty |
+----------+-------------+------------+-------+-------+------------+------+
| 104 | 3 | 2026-02-14 | OH | 46.50 | 2 | 3 |
+----------+-------------+------------+-------+-------+------------+------+
+--------------+
| natural_rows |
+--------------+
| 4 |
+--------------+
With USING, SELECT * shows order_id once. An ON join would list it twice, once per table.
NATURAL JOIN goes further and matches every column the two tables share. Here that’s customer_id and state, so it returns 4 rows instead of 5.
Order 103 shipped to KY, not Hannah’s home state of OH, so it dropped out. Add one same-named column later and every NATURAL JOIN on that table changes meaning. I keep it out of saved code.
Does MySQL Support FULL OUTER JOIN?
No. MySQL 8.4 has no FULL JOIN. I ran both spellings with --force so the client kept going after the error:
SELECT c.name, o.order_id
FROM customers c
FULL OUTER JOIN orders o ON o.customer_id = c.customer_id;
-- no alias: FULL quietly becomes the alias of customers
SELECT name, order_id
FROM customers FULL JOIN orders USING (customer_id);
What MySQL said:
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FULL OUTER JOIN orders o ON o.customer_id = c.customer_id' at line 3
+----------------+----------+
| name | order_id |
+----------------+----------+
| Hannah Brooks | 101 |
| Tyler Reed | 102 |
| Hannah Brooks | 103 |
| Grace Miller | 104 |
| Marcus Bennett | 105 |
+----------------+----------+

The first query fails with ERROR 1064 near ‘FULL OUTER JOIN’. The second is the trap: with no table alias, MySQL takes FULL as the alias of customers and runs a plain inner join.
It returned 5 rows, with Olivia and order 106 missing, and no warning. MariaDB has the same gap; I have a separate guide on the MariaDB FULL OUTER JOIN workaround.
Emulate FULL OUTER JOIN with UNION
Take the LEFT JOIN, then add only the right-side rows that had no match:
SELECT c.name, o.order_id, o.total
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
UNION ALL
SELECT c.name, o.order_id, o.total
FROM customers c
RIGHT JOIN orders o ON o.customer_id = c.customer_id
WHERE c.customer_id IS NULL
ORDER BY order_id;
Output:
+----------------+----------+--------+
| name | order_id | total |
+----------------+----------+--------+
| Olivia Carter | NULL | NULL |
| Hannah Brooks | 101 | 120.00 |
| Tyler Reed | 102 | 75.50 |
| Hannah Brooks | 103 | 210.00 |
| Grace Miller | 104 | 46.50 |
| Marcus Bennett | 105 | 105.00 |
| NULL | 106 | 31.00 |
+----------------+----------+--------+
That returns 7 rows: Olivia with no order and order 106 with no customer, plus the 5 matches. UNION ALL with the IS NULL filter keeps real duplicate rows; plain UNION would merge them. More on that in UNION in MariaDB.
Test the Report: MySQL Join with GROUP BY and SUM
The opening report is a LEFT JOIN plus GROUP BY. COUNT(o.order_id) counts only matched rows, so Olivia gets 0. COALESCE turns her NULL total into 0.00.
SELECT c.name, c.state,
COUNT(o.order_id) AS orders,
COALESCE(SUM(o.total), 0) AS total_usd
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.customer_id
GROUP BY c.customer_id, c.name, c.state
ORDER BY total_usd DESC;
What comes back:
+----------------+-------+--------+-----------+
| name | state | orders | total_usd |
+----------------+-------+--------+-----------+
| Hannah Brooks | OH | 2 | 330.00 |
| Marcus Bennett | FL | 1 | 105.00 |
| Tyler Reed | TX | 1 | 75.50 |
| Grace Miller | OH | 1 | 46.50 |
| Olivia Carter | WA | 0 | 0.00 |
+----------------+-------+--------+-----------+
Now add payments. Order 103 has two payment rows, so the join repeats it, and SUM counts it twice:
SELECT c.name, SUM(o.total) AS order_total, SUM(p.amount) AS paid
FROM customers c
JOIN orders o ON o.customer_id = c.customer_id
JOIN payments p ON p.order_id = o.order_id
WHERE c.customer_id = 1 GROUP BY c.name;
-- fix: total the payments per order first
SELECT c.name, SUM(o.total) AS order_total, SUM(pp.paid) AS paid
FROM customers c
JOIN orders o ON o.customer_id = c.customer_id
JOIN (SELECT order_id, SUM(amount) AS paid
FROM payments GROUP BY order_id) pp
ON pp.order_id = o.order_id
WHERE c.customer_id = 1 GROUP BY c.name;
Result:
+---------------+-------------+--------+
| name | order_total | paid |
+---------------+-------------+--------+
| Hannah Brooks | 540.00 | 330.00 |
+---------------+-------------+--------+
+---------------+-------------+--------+
| name | order_total | paid |
+---------------+-------------+--------+
| Hannah Brooks | 330.00 | 330.00 |
+---------------+-------------+--------+

The first query says Hannah ordered $540.00; the real figure is $330.00. Totaling payments per order in a derived table first keeps one row per order. GROUP BY details are in GROUP BY in MariaDB.
Troubleshooting MySQL Joins: WHERE Filters and Errors 1052 and 1054
Important: a WHERE on the right table turns your LEFT JOIN into an inner join. Same date filter, two places:
-- date filter in WHERE
SELECT c.name, o.order_id
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
WHERE o.order_date >= '2026-02-01';
-- same filter in ON
SELECT c.name, o.order_id
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
AND o.order_date >= '2026-02-01';
Output:
+----------------+----------+
| name | order_id |
+----------------+----------+
| Hannah Brooks | 103 |
| Grace Miller | 104 |
| Marcus Bennett | 105 |
+----------------+----------+
+----------------+----------+
| name | order_id |
+----------------+----------+
| Hannah Brooks | 103 |
| Tyler Reed | NULL |
| Grace Miller | 104 |
| Marcus Bennett | 105 |
| Olivia Carter | NULL |
+----------------+----------+
In WHERE, Tyler and Olivia vanish (3 rows) because their NULL order_date fails the test. In ON, every customer stays (5 rows).
So put right-table filters in ON. The LEFT OUTER JOIN with a WHERE clause post shows it in SQL Server.
Two real errors I hit while building this:
SELECT order_id, qty
FROM orders
JOIN order_items ON order_items.order_id = orders.order_id;
SELECT c.name, i.qty
FROM customers c, orders o
JOIN order_items i ON i.order_id = o.order_id
AND o.customer_id = c.customer_id;
What MySQL said:
ERROR 1052 (23000) at line 1: Column 'order_id' in field list is ambiguous
ERROR 1054 (42S22) at line 5: Unknown column 'c.customer_id' in 'on clause'
ERROR 1052 means a column exists in both tables, so qualify it as orders.order_id. ERROR 1054 comes from mixing a comma join with JOIN: JOIN binds tighter, so the ON clause can’t see c. Use JOIN everywhere.
Where to Take It Next with MySQL Joins
Next step: build more of the schema behind these queries with CREATE TABLE in MySQL, then keep going in the MySQL tutorial.
- Same joins on another engine: PostgreSQL joins
- MariaDB vs MySQL
- If you also run MariaDB: joins in MariaDB
- Syntax differences: MySQL vs SQL Server
Frequently Asked Questions
Is JOIN the same as INNER JOIN in MySQL?
Yes. In MySQL, JOIN, INNER JOIN and CROSS JOIN are syntactic equivalents, so CROSS JOIN with an ON clause runs as an inner join. Standard SQL keeps them separate. See MySQL’s JOIN clause reference.
Does MySQL use hash joins?
Yes, since MySQL 8.0.18. When an equi-join has no usable index, the optimizer builds an in-memory hash table instead of a nested loop. EXPLAIN FORMAT=TREE shows it as “Inner hash join”, and join_buffer_size caps its memory.
What is STRAIGHT_JOIN in MySQL?
STRAIGHT_JOIN works like JOIN but forces MySQL to read the left table first. It’s for the rare query where the optimizer picks a slow join order.

Kumar Saurabh is a freelance software engineer who builds web and mobile apps with Next.js, React, Flutter and Python. From 2021 to 2024, Saurabh worked as an IT analyst and developer at TSInfo Technologies, writing technical articles on SQL and PostgreSQL. Saurabh writes the PostgreSQL, MySQL, MariaDB and MongoDB tutorials on DatabaseFAQs.com and holds a Master of Computer Applications (MCA) from Chandigarh University.