SQL JOINs Explained: INNER, LEFT, RIGHT, and FULL

Posted by Kyle Hankinson


Short answer

Join Returns Rows with no match
INNER JOIN Only rows that match in both tables Dropped from both sides
LEFT JOIN Every row from the left (first) table Right-side columns are NULL
RIGHT JOIN Every row from the right (second) table Left-side columns are NULL
FULL JOIN Every row from both tables NULL on whichever side has no match
CROSS JOIN Every combination of rows Not applicable: there is no join condition

LEFT JOIN vs RIGHT JOIN: they are the same operation with the tables swapped. A LEFT JOIN B returns the same rows as B RIGHT JOIN A. Most people write every outer join as a LEFT JOIN and put the table they want to keep first.

LEFT JOIN and LEFT OUTER JOIN are the same thing; OUTER is optional. The same goes for RIGHT and FULL.

Sample data

-- customers          -- orders
-- id | name          -- id | customer_id | total
-- 1  | Alice         -- 1  | 1           | 50.00
-- 2  | Bob           -- 2  | 1           | 75.00
-- 3  | Carol         -- 3  | 3           | 30.00
--                    -- 4  | 9           | 20.00

Bob has no orders. Order 4 belongs to customer 9, who does not exist (an orphan row).

INNER JOIN

SELECT c.name, o.id AS order_id, o.total
FROM customers c
INNER JOIN orders o ON o.customer_id = c.id;
name order_id total
Alice 1 50.00
Alice 2 75.00
Carol 3 30.00

Bob (no orders) and order 4 (no customer) are both left out. A plain JOIN means INNER JOIN.

LEFT JOIN

SELECT c.name, o.id AS order_id, o.total
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id;
name order_id total
Alice 1 50.00
Alice 2 75.00
Bob NULL NULL
Carol 3 30.00

Every customer appears. Bob gets NULLs. The orphan order 4 is not shown, because it is on the right side.

To find rows with no match (customers who never ordered), keep only the NULL rows:

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

RIGHT JOIN

SELECT c.name, o.id AS order_id, o.total
FROM customers c
RIGHT JOIN orders o ON o.customer_id = c.id;
name order_id total
Alice 1 50.00
Alice 2 75.00
Carol 3 30.00
NULL 4 20.00

Every order appears, including the orphan order 4 with a NULL name. Bob is gone. The same result as a LEFT JOIN, written the usual way:

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

FULL JOIN

SELECT c.name, o.id AS order_id, o.total
FROM customers c
FULL JOIN orders o ON o.customer_id = c.id;
name order_id total
Alice 1 50.00
Alice 2 75.00
Bob NULL NULL
Carol 3 30.00
NULL 4 20.00

Both Bob and the orphan order appear. This is the join for reconciling two lists: what is on one side, the other, or both.

FULL JOIN works in PostgreSQL, SQL Server, Oracle and SQLite (3.39 and later; RIGHT JOIN arrived in the same release).

FULL JOIN in MySQL and MariaDB

MySQL and MariaDB have no FULL JOIN. Combine a LEFT JOIN with the right-side rows that had no match:

SELECT c.name, o.id AS order_id, o.total
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
UNION ALL
SELECT c.name, o.id, o.total
FROM customers c
RIGHT JOIN orders o ON o.customer_id = c.id
WHERE c.id IS NULL;

Use UNION ALL with the WHERE c.id IS NULL filter, not plain UNION. Plain UNION also removes rows that are genuinely identical, so two real orders with the same customer and total would collapse into one. (More on that in UNION vs UNION ALL.)

CROSS JOIN and self-joins

CROSS JOIN returns every pairing: 3 customers times 10 products gives 30 rows. It is useful for building combinations (every store with every day of the month), and dangerous on large tables.

A self-join joins a table to itself, typically for hierarchies:

SELECT e.name AS employee, m.name AS manager
FROM employees e
LEFT JOIN employees m ON m.id = e.manager_id;

LEFT JOIN keeps the employee at the top who has no manager.

Mistakes that change the result

A WHERE condition on the right table turns a LEFT JOIN into an INNER JOIN.

-- Bob disappears: WHERE runs after the join, and NULL > 50 is not true
SELECT c.name, o.total
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.total > 50;

-- Bob stays, with NULL: the condition only decides which orders match
SELECT c.name, o.total
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id AND o.total > 50;

A missing or wrong ON condition multiplies rows. PostgreSQL and SQL Server reject INNER JOIN without ON, but MySQL accepts it and treats it as a CROSS JOIN. A condition that matches too broadly (joining on a non-unique column) has the same effect. If a join returns more rows than you expect, check that the right-hand key is unique.

Joining two one-to-many tables at once (customers to orders and customers to support tickets) multiplies each customer's orders by their tickets. Aggregate each child table in a subquery first, then join the totals.

Joins on large tables need an index on the join column of the inner table; see how database indexes work.


About the author - Kyle Hankinson is the founder and sole developer of SQLPro for MySQL and the Hankinsoft Development suite of database tools. He has been building native macOS and iOS applications since 2010, and now ships SQLPro on Windows and Android as well.

Try SQLPro for MySQL - A native MySQL and MariaDB client for macOS, iOS, and Android. No Java required.

Download Free Trial View Pricing Compare