Inner join keeps matching rows only
An inner join returns only rows that match in both tables. An outer join also returns rows without a match, with NULL in the columns that came from the table that had no match. In MySQL, LEFT JOIN and RIGHT JOIN are the outer joins you can write directly. MySQL does not support FULL OUTER JOIN.
The examples use two tables. Customers have a customer_id and a name, and orders have an order_id and a customer_id. Chen has no orders.
CREATE TABLE customers (
customer_id INT PRIMARY KEY,
name VARCHAR(100) NOT NULL
);
CREATE TABLE orders (
order_id INT PRIMARY KEY,
customer_id INT NOT NULL,
order_total DECIMAL(10, 2) NOT NULL,
FOREIGN KEY (customer_id) REFERENCES customers (customer_id)
);
INSERT INTO customers (customer_id, name) VALUES
(1, 'Ana'),
(2, 'Ben'),
(3, 'Chen');
INSERT INTO orders (order_id, customer_id, order_total) VALUES
(101, 1, 25.00),
(102, 1, 40.00),
(103, 2, 15.50);
An inner join keeps only the customers who have orders:
SELECT c.name, o.order_id, o.order_total
FROM customers AS c
INNER JOIN orders AS o
ON o.customer_id = c.customer_id
ORDER BY c.name, o.order_id;
| name | order_id | order_total |
|---|---|---|
| Ana | 101 | 25.00 |
| Ana | 102 | 40.00 |
| Ben | 103 | 15.50 |
Chen is missing, because no order matches the row for Chen.
Left outer join keeps every customer
A LEFT JOIN keeps every row from the table on the left, which here is customers. Where there is no match, the columns from orders are NULL.
SELECT c.name, o.order_id, o.order_total
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.customer_id
ORDER BY c.name, o.order_id;
| name | order_id | order_total |
|---|---|---|
| Ana | 101 | 25.00 |
| Ana | 102 | 40.00 |
| Ben | 103 | 15.50 |
| Chen | NULL | NULL |
The OUTER keyword is optional, so LEFT JOIN and LEFT OUTER JOIN do the same thing. NULL means no value, which isn’t the same as 0 or an empty string. For the next step, replacing those NULLs with a default, look at “How to get 0 instead of NULL in SQL?”
Right outer join
A RIGHT JOIN does the same job from the other side. It keeps every row from the table on the right and fills in NULL on the left where nothing matches. Swapping the table order and using LEFT JOIN gives the same result. The MySQL manual recommends LEFT JOIN over RIGHT JOIN so that code stays portable across databases.
FULL OUTER JOIN in MySQL
A FULL OUTER JOIN keeps unmatched rows from both tables. MySQL doesn’t have that keyword. A common workaround combines a LEFT JOIN and a RIGHT JOIN with UNION:
SELECT *
FROM t1
LEFT JOIN t2
ON t2.id = t1.id
UNION
SELECT *
FROM t1
RIGHT JOIN t2
ON t2.id = t1.id;
Rows that match appear in both halves, and UNION removes the duplicates. UNION also removes exact duplicate rows, so check that your data doesn’t need them.
Choosing between them
Use an inner join when you only want rows that match in both tables. Use a left join when every row from the first table should appear, even without a match. “When to use left, right, or inner join?” looks at that choice in more detail.
Speed is a separate question. People often ask “Which join is faster in SQL?” and the honest answer is that it depends on the data, the indexes and the query. EXPLAIN shows the plan MySQL uses for a given query, which is the best way to compare them on your own tables.