Dev Basics · 8 min read
SQL Joins Explained
Relational databases split data into tables so each fact is stored once: customers in one table, orders in another, products in a third. Joins put those pieces back together at query time. They are the heart of SQL, and most confusing query results — missing rows, duplicated rows, totals that are too large — come down to choosing the wrong join or joining on the wrong condition.
This guide explains inner, left, right, full and cross joins with a running example, then covers self joins, the classic pitfalls, and how indexes keep joins fast.
Try it yourself with the related tool.
Format a SQL query →Advertisement
The example tables
Imagine a customers table with id and name, and an orders table with id, customer_id and total. Ada has two orders, Grace has one, and Linus has none. There is also one order whose customer_id points at a customer who was deleted. Each join type answers a different question about these rows.
INNER JOIN: only matching rows
SELECT c.name, o.total FROM customers c JOIN orders o ON o.customer_id = c.id returns a row for each pair that matches the condition. Ada appears twice, once per order; Grace once. Linus does not appear, because he has no orders, and the orphaned order does not appear, because it has no customer. JOIN on its own means INNER JOIN. Use it when you only care about records that exist on both sides.
LEFT JOIN: keep everything on the left
A LEFT JOIN keeps every row from the left table and fills the right-hand columns with NULL where there is no match. With customers on the left, Linus now appears with a NULL total. This is the join for questions such as "all customers and their orders, including those with none".
It also finds missing relationships: LEFT JOIN orders o ON o.customer_id = c.id WHERE o.id IS NULL lists customers who have never ordered. Be careful where you put conditions, though. A filter on the right table in the WHERE clause, such as WHERE o.total > 100, removes the NULL rows and quietly turns the left join back into an inner join. Put such filters in the ON clause if you want to keep unmatched rows.
RIGHT and FULL OUTER JOIN
A RIGHT JOIN is a left join with the tables swapped; most people rewrite it as a left join for readability. A FULL OUTER JOIN keeps unmatched rows from both sides: Linus with no order, and the orphaned order with no customer. It is useful for reconciliation — comparing two lists and seeing what is only in one, only in the other, or in both. MySQL does not support it directly; combine a left join and a right join with UNION.
CROSS JOIN and self joins
A CROSS JOIN pairs every row with every row: three customers and four orders give twelve rows. It is occasionally useful, for example to build every combination of size and colour, but an accidental one — a join with a missing or always-true condition — is a common cause of a query that suddenly returns millions of rows.
A self join joins a table to itself under two aliases. An employees table with a manager_id column can list each employee next to their manager's name: FROM employees e LEFT JOIN employees m ON m.id = e.manager_id.
Duplicated rows and inflated totals
Joins multiply rows. If a customer has three orders and two addresses, joining customers to both tables produces six rows for that customer, and SUM(o.total) counts each order twice. When totals look too large, check whether you joined two one-to-many relationships at once. The fix is to aggregate each child table in a subquery or CTE first, then join the results. Reaching for DISTINCT to hide duplicates usually masks the real problem.
Keeping joins fast
The database needs an efficient way to find matching rows. Primary keys are indexed automatically, but foreign key columns such as orders.customer_id often are not — PostgreSQL, for example, does not index them for you. Add an index on columns used in join conditions, and check the plan with EXPLAIN when a query is slow. Join on columns of the same type; comparing an integer with a string can stop an index from being used.

