Joins answer one question: how do I combine rows from two tables? The four core types differ only in what they do with rows that fail to match. An INNER JOIN keeps matches only; a LEFT JOIN keeps every row of the left table anyway; a RIGHT JOIN keeps every row of the right table; a FULL OUTER JOIN keeps every row from both. Everything else — NULLs appearing, row counts multiplying, results "missing" rows — follows from that one rule.
You can learn all four with the two invented tables below. Every expected result is shown, so you can check yourself without a database.
The dataset
Two tables. Note two deliberate imperfections: customer 1 has two orders, and order 105 belongs to customer 9 — who does not exist in customers.
| customer_id | name | city |
|---|---|---|
| 1 | Asha | Delhi |
| 2 | Ben | Pune |
| 3 | Chitra | Kolkata |
| 4 | Dev | Delhi |
| 5 | Emma | Bengaluru |
| order_id | customer_id | item | amount |
|---|---|---|---|
| 101 | 1 | Keyboard | 1500 |
| 102 | 1 | Mouse | 700 |
| 103 | 2 | Monitor | 9000 |
| 104 | 3 | Keyboard | 1500 |
| 105 | 9 | Laptop | 55000 |
Expected matches: customer 1 matches orders 101 and 102 (two result rows), customer 2 matches 103, customer 3 matches 104. Customers 4 and 5 have no orders. Order 105 has no customer.
INNER JOIN — matches only
Task: List every order with the customer's name and city.SELECT c.name, c.city, o.item, o.amount
FROM customers AS c
INNER JOIN orders AS o
ON c.customer_id = o.customer_id;
ON is where the match happens: a row survives only when the join key is equal on both sides.
Expected result (4 rows):
| name | city | item | amount |
|---|---|---|---|
| Asha | Delhi | Keyboard | 1500 |
| Asha | Delhi | Mouse | 700 |
| Ben | Pune | Monitor | 9000 |
| Chitra | Kolkata | Keyboard | 1500 |
Dev and Emma disappear (no orders). Order 105 disappears (no customer). Note that Asha appears twice — a join multiplies rows by the number of matches, not by the number of customers.
LEFT JOIN — keep every left row
Task: List all customers and their orders. Customers with no orders must still appear.SELECT c.name, c.city, o.item, o.amount
FROM customers AS c
LEFT JOIN orders AS o
ON c.customer_id = o.customer_id;
The left table (customers) is the protected list: every one of its rows appears at least once. Where no order matches, the order columns come back NULL — a NULL-padded row on the right side.
| name | city | item | amount |
|---|---|---|---|
| Asha | Delhi | Keyboard | 1500 |
| Asha | Delhi | Mouse | 700 |
| Ben | Pune | Monitor | 9000 |
| Chitra | Kolkata | Keyboard | 1500 |
| Dev | Delhi | NULL | NULL |
| Emma | Bengaluru | NULL | NULL |
WHERE o.amount > 1000 to this query, Dev and Emma vanish — because their o.amount is NULL, and NULL > 1000 is not true. Put the condition inside the join (ON c.customer_id = o.customer_id AND o.amount > 1000) when you want to keep customers whose orders fail the condition, or use it in WHERE when you mean to filter them out.
RIGHT JOIN — keep every right row
Task: List every order with customer details, even orders whose customer is missing.SELECT c.name, c.city, o.item, o.amount
FROM customers AS c
RIGHT JOIN orders AS o
ON c.customer_id = o.customer_id;
Now orders is the protected table: all 5 orders appear. Order 105 has no customer, so its customer columns are NULL.
| name | city | item | amount |
|---|---|---|---|
| Asha | Delhi | Keyboard | 1500 |
| Asha | Delhi | Mouse | 700 |
| Ben | Pune | Monitor | 9000 |
| Chitra | Kolkata | Keyboard | 1500 |
| NULL | NULL | Laptop | 55000 |
Dev and Emma are gone — they are left-table rows with no match, and a RIGHT JOIN does not protect the left side. In practice most teams rewrite this as FROM orders LEFT JOIN customers and get the identical result with one convention to remember.
FULL OUTER JOIN — keep everything
Task: One complete report: all customers, all orders, matches or not.SELECT c.name, c.city, o.item, o.amount
FROM customers AS c
FULL OUTER JOIN orders AS o
ON c.customer_id = o.customer_id;
Expected result (7 rows):
| name | city | item | amount |
|---|---|---|---|
| Asha | Delhi | Keyboard | 1500 |
| Asha | Delhi | Mouse | 700 |
| Ben | Pune | Monitor | 9000 |
| Chitra | Kolkata | Keyboard | 1500 |
| Dev | Delhi | NULL | NULL |
| Emma | Bengaluru | NULL | NULL |
| NULL | NULL | Laptop | 55000 |
This is the union of the LEFT and RIGHT results: 4 matched rows + 2 customers without orders + 1 order without a customer. (MySQL lacks FULL OUTER JOIN before version 8.0.12 workarounds; PostgreSQL and SQLite ≥ 3.39 support it directly — see the docs below.)
Anti-join: rows with no match
Two of the most common interview questions are just filters on top of what you have seen:
Customers who never ordered:SELECT c.name
FROM customers AS c
LEFT JOIN orders AS o
ON c.customer_id = o.customer_id
WHERE o.order_id IS NULL;
Expected result (2 rows): Dev, Emma.
Orders with no valid customer:
SELECT o.order_id, o.item
FROM orders AS o
LEFT JOIN customers AS c
ON c.customer_id = o.customer_id
WHERE c.customer_id IS NULL;
Expected result (1 row): 105, Laptop.
The pattern is always the same: join first, then test a right-side column for NULL. Testing WHERE o.customer_id = 9 is not the same thing — it hardcodes the answer instead of detecting it.
Quick reference
| Join type | Keeps | Unmatched left rows | Unmatched right rows | Row count on our data |
|---|---|---|---|---|
| INNER | matches only | dropped | dropped | 4 |
| LEFT | all left + matches | kept (NULL-padded) | dropped | 6 |
| RIGHT | all right + matches | dropped | kept (NULL-padded) | 5 |
| FULL OUTER | all from both | kept | kept | 7 |
Two more joins you will meet: CROSS JOIN (every left row × every right row: 5 × 5 = 25 rows here, no ON) and SELF JOIN (a table joined to itself, e.g. employees and their managers).
Common mistakes to avoid
- Comma joins with forgotten conditions.
FROM customers, orderswith noWHEREis a CROSS JOIN — 25 rows of nonsense on our data. Always write explicitJOIN ... ON. - Ambiguous columns. Once two tables are joined,
SELECT customer_idfails or misleads; qualify withc.customer_idoro.customer_id. - Filtering the right table in WHERE after a LEFT JOIN. As shown above, it silently converts your LEFT JOIN into an INNER JOIN. Decide per condition:
ONto keep the left rows,WHEREto exclude them. - Assuming NULL matches NULL.
ON c.x = o.xnever matches two NULLs. UseIS NOT DISTINCT FROM(PostgreSQL) orCOALESCE-based keys when NULL is a real value in your data. - Forgetting multiplication. One customer with 2 orders produces 2 rows. If you then sum
o.amountper customer without grouping, customers with more orders dominate your totals — aggregate withGROUP BY c.customer_id(next topic).
Practice on PixoQuest
Reading join syntax once is not the same as recognising which join a question needs. The PixoQuest SQL Basic course drills joins and aggregation as short interactive quizzes — pick the right join type from a question prompt, predict row counts, and get instant feedback with no database install. Use it for pattern recognition; use a real engine (PostgreSQL, SQLite) to type the queries above yourself.
Bottom line
INNER keeps matches, LEFT keeps the left table, RIGHT keeps the right table, FULL keeps both — and every unmatched cell is NULL. On our 5-customer/5-order dataset that means 4, 6, 5, and 7 rows respectively. Master three skills around that core: predicting row counts before running a query, protecting left rows from WHERE-clause filters, and writing anti-joins. Those three decide most SQL join questions in interviews and semester papers.
Sources & further reading
- PostgreSQL documentation, Tutorial: Joins Between Tables: https://www.postgresql.org/docs/current/tutorial-join.html
- PostgreSQL documentation, SELECT — join syntax and FULL OUTER support: https://www.postgresql.org/docs/current/sql-select.html
- SQLite documentation, SELECT — join clauses (note the FULL OUTER support added in 3.39): https://www.sqlite.org/lang_select.html
The tables and expected results in this article are invented example data written for this exercise; no real customer or order data is used.
Frequently asked questions
What is the difference between INNER JOIN and LEFT JOIN?
INNER JOIN returns only rows that have a match on both sides. LEFT JOIN returns every row of the left table, and fills unmatched right-side columns with NULL. Use LEFT JOIN when the left table is the complete list you must not lose (all customers, all students, all products).
When should I use a RIGHT JOIN?
Rarely. Anything a RIGHT JOIN does, a LEFT JOIN does by swapping the table order, and most teams standardise on LEFT JOIN for readability. You still need to recognise RIGHT JOIN in other people's queries and in exam papers.
What is an anti-join?
The pattern that finds left rows with no match: LEFT JOIN then filter WHERE right.key IS NULL. It answers questions like 'customers with no orders' or 'students who never submitted an assignment'.
Why does my LEFT JOIN drop rows when I add a condition to WHERE?
Because WHERE runs after the join and filters out the NULL-padded rows the LEFT JOIN created. Put right-table conditions in the ON clause (inside the join) or guard them with OR right.col IS NULL when you want to keep non-matching left rows.
Can NULL keys join to each other?
No. NULL = NULL is not true in SQL, so rows with NULL in the join key never match anything, including each other. Match NULLs explicitly with IS NOT DISTINCT FROM if that is what you need.