A join combines rows from two tables by matching a column they share — customers with their orders, products with their prices, employees with their departments. If you have ever used VLOOKUP or XLOOKUP in a spreadsheet, you have done a join by hand. This guide explains every kind of SQL join with the same two small tables, shows exactly which rows each one returns, and covers the mistakes that quietly give wrong totals.
The two tables
We will use a customers table and an orders table. Notice two deliberate gaps: Meera has no orders, and order 104 belongs to customer 4, who is not in the customers table.
customers orders
id | name | city order_id | customer_id | amount
1 | Asha | Pune 101 | 1 | 500
2 | Ravi | Mumbai 102 | 1 | 250
3 | Meera | Delhi 103 | 2 | 900
104 | 4 | 300
To follow along, save each as a CSV file (customers.csv and orders.csv), load both into SQL Query on CSV and run the queries below. Each file becomes a table named after it.
INNER JOIN: only rows that match
SELECT c.name, o.order_id, o.amount
FROM customers c
JOIN orders o ON o.customer_id = c.id;
name | order_id | amount
Asha | 101 | 500
Asha | 102 | 250
Ravi | 103 | 900
JOIN on its own means INNER JOIN. It keeps only pairs where the ON condition is true. Meera disappears because she has no orders, and order 104 disappears because it has no customer. Asha appears twice because she has two orders: a join produces one row per matching pair, not one row per customer.
The letters c and o are table aliases — short names that save typing and make it clear which table each column comes from.
LEFT JOIN: keep everything on the left
SELECT c.name, o.order_id, o.amount
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id;
name | order_id | amount
Asha | 101 | 500
Asha | 102 | 250
Ravi | 103 | 900
Meera | NULL | NULL
A left join keeps every row of the first table, whether or not it matches. Where there is no match, the columns from the second table are NULL. This is the join you will use most for reporting, because “all customers, with their orders if they have any” is usually the question.
RIGHT JOIN and FULL JOIN
A right join is the mirror image: it keeps every row of the second table. Here it returns the three matched orders plus order 104 with a NULL name. Most people rewrite right joins as left joins by swapping the table order, which reads more naturally.
SELECT c.name, o.order_id, o.amount
FROM customers c
FULL JOIN orders o ON o.customer_id = c.id;
name | order_id | amount
Asha | 101 | 500
Asha | 102 | 250
Ravi | 103 | 900
Meera | NULL | NULL
NULL | 104 | 300
A full (outer) join keeps unmatched rows from both sides. It is the best way to reconcile two lists — a bank statement against your ledger, last month’s export against this month’s — because every row that exists on only one side shows up with NULL on the other.
Finding rows with no match (anti-joins)
Some of the most useful questions are about what is missing: customers who never ordered, orders whose customer does not exist. Use a left join and keep only the rows where the right-hand side is empty:
-- Customers with no orders
SELECT c.name
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.order_id IS NULL; -- Meera
-- Orders with an unknown customer
SELECT o.order_id
FROM orders o
LEFT JOIN customers c ON c.id = o.customer_id
WHERE c.id IS NULL; -- 104
The second query is a data-quality check worth running on any pair of exports. NOT IN and NOT EXISTS with a sub-query answer the same questions, but beware NOT IN: if the sub-query returns even one NULL, it returns no rows at all.
CROSS JOIN: every combination
A cross join pairs every row of one table with every row of the other — 3 customers × 4 orders gives 12 rows. It is occasionally what you want, for example to build a grid of every product for every month so that months with no sales still appear. Usually it is an accident: forgetting the ON condition, or listing two tables separated by a comma with no WHERE, produces one.
Joins and totals: count per customer
Joins are often followed by GROUP BY. To see how many orders each customer placed and how much they spent, including customers with none:
SELECT c.name,
COUNT(o.order_id) AS orders,
COALESCE(SUM(o.amount), 0) AS spent
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
GROUP BY c.name
ORDER BY spent DESC;
name | orders | spent
Ravi | 1 | 900
Asha | 2 | 750
Meera | 0 | 0
Two details matter. COUNT(o.order_id) counts only non-null values, so Meera gets 0; COUNT(*) would count her one unmatched row and wrongly give 1. And COALESCE turns her NULL total into 0 so it sorts and reads sensibly.
Five mistakes that give wrong answers
- Filtering the right table in WHERE.
LEFT JOIN orders o ON … WHERE o.amount > 400removes every row whereo.amountisNULL— so Meera vanishes and the left join has silently become an inner join. To keep all customers and match only large orders, move the condition into the join:LEFT JOIN orders o ON o.customer_id = c.id AND o.amount > 400. - Duplicate keys multiply rows. If a lookup table has the same ID twice — two price rows for one product, say — every matching row is doubled, and every
SUMafter it is too large. Before joining, check that the key is unique:SELECT id, COUNT(*) FROM products GROUP BY id HAVING COUNT(*) > 1should return nothing. - Joining two “many” tables. Joining customers to both orders and support tickets in one query pairs every order with every ticket. Summarise each table to one row per customer first (in a
WITHclause), then join the summaries. - Keys that look equal but are not. “00123” and “123”, “Pune” and “Pune ”, or “ASHA” and “asha” do not match. Clean keys before joining —
TRIM,LOWERandCASThelp — and see How to Clean Messy CSV Data. - Joining on NULL.
NULL = NULLis not true in SQL, so rows with an empty key never match anything. That is usually what you want, but it explains rows that unexpectedly fail to join.
A quick reference
| Join | Keeps | Typical use |
|---|---|---|
| INNER JOIN | Only matching pairs | Orders with their customer details |
| LEFT JOIN | All left rows, matches where they exist | All customers with their orders, if any |
| RIGHT JOIN | All right rows, matches where they exist | Same as LEFT with the tables swapped |
| FULL JOIN | All rows from both sides | Reconciling two lists |
| LEFT JOIN … WHERE right IS NULL | Left rows with no match | Customers who never ordered |
| CROSS JOIN | Every combination | Building a complete grid of dates × products |
Joins are one part of a bigger toolkit: our guide to querying CSV files with SQL covers filtering, grouping, dates and window functions, and How to Make a Pivot Table Without Excel shows how to turn a joined result into a summary table.