INNER JOIN and LEFT JOIN
Combine rows from related tables with INNER JOIN, keep unmatched rows with LEFT JOIN, find missing matches, and avoid the most common join mistakes.
Welcome back!
Step 1 of 15Learn
Matching rows across tables
Question: what is the name of the customer behind each order? Names live in customers; orders hold only customer_id. A JOIN combines the two tables by matching rows on a condition:
SELECT orders.order_id, customers.name, orders.total
FROM orders
INNER JOIN customers
ON orders.customer_id = customers.customer_id;For each order, the database finds the customer whose customer_id matches and glues the two rows together. Result:
| order_id | name | total |
|---|---|---|
| 101 | Priya Shah | 36.00 |
| 102 | Lena Ortiz | 42.00 |
| 103 | Priya Shah | 12.99 |
| 104 | Marcus Lee | 59.50 |
| 105 | Tom Becker | 24.00 |
| 106 | Priya Shah | 21.00 |
Notice Priya appears three times, once per order. That's how one-to-many joins work: the "one" side repeats for each matching row on the "many" side.
Key parts of the syntax:
- JOIN ... ON names the second table and the matching condition, almost always foreign key = primary key.
- Qualified column names like
orders.customer_idsay which table a column comes from. They're required when both tables have a column with the same name; otherwise the database reports an "ambiguous column" error. - Table aliases keep things short:
FROM orders AS o JOIN customers AS c ON o.customer_id = c.customer_id. Many people omit AS for tables:FROM orders o. - Writing just
JOINmeans INNER JOIN.
An INNER JOIN returns only rows that have a match in both tables. Ava, who has no orders, does not appear in the result.
Step 2 of 15Note
Step 3 of 15Practice · Work it out
How many rows does this query return? SELECT c.name, o.order_id FROM customers c INNER JOIN orders o ON c.customer_id = o.customer_id;
Step 4 of 15Practice · Diagram
Customers and orders as overlapping sets
The overlap holds customers who have at least one matching order. Tap a region.
Which region does a LEFT JOIN from customers add on top of an INNER JOIN?
Tap the shape that answers the question, then press Check.
- customers
- orders
- customers only
A customer with no orders. INNER JOIN drops her; LEFT JOIN keeps her with NULL order columns.
- Ava Novak
- customers + orders (overlap)
Matched rows: 6 customer–order pairs. Both INNER and LEFT JOIN return these.
- Priya (3 orders)
- Tom
- Lena
- Marcus
- orders only
Empty here, because the foreign key stops orders for customers who don't exist.
- none
Step 5 of 15Learn
LEFT JOIN: keep everyone
What if the question is "list every customer and their orders, including customers who haven't ordered"? An INNER JOIN drops Ava. A LEFT JOIN (or LEFT OUTER JOIN) keeps every row from the left table, the one named in FROM, and fills the right table's columns with NULL where there's no match:
SELECT c.name, o.order_id, o.total
FROM customers c
LEFT JOIN orders o
ON c.customer_id = o.customer_id;This returns 7 rows: the same 6 matched rows, plus one row for Ava with order_id and total both NULL.
This makes a powerful pattern for finding missing matches, such as customers who never ordered:
SELECT c.name
FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id
WHERE o.order_id IS NULL;Result: Ava Novak. The LEFT JOIN keeps her row with NULLs, and WHERE keeps only rows where no order matched. The same idea finds products never sold, students not enrolled, or employees without a manager.
Combining joins with GROUP BY answers classic business questions, like how many orders each customer has placed:
SELECT c.name, COUNT(o.order_id) AS num_orders
FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id
GROUP BY c.name;Priya 3, Lena 1, Marcus 1, Tom 1, Ava 0. Note the use of COUNT(o.order_id), not COUNT(*): Ava's row exists, so COUNT(*) would wrongly report 1 order for her, while COUNT on the order column ignores her NULL and gives 0.
RIGHT JOIN is the mirror image, keeping all rows from the right table; most people just swap the table order and use LEFT JOIN. FULL OUTER JOIN keeps unmatched rows from both sides, though not every database supports it.
Step 6 of 15Practice · Code challenge · SQL
Code challenge · SQL
Return the order_id, the customer name and the order total for every order placed by a customer who lives in Austin.
Customers and orders are in separate tables (open the database below to see both).
The database for this exercise (tables and rows)
CREATE TABLE customers (
customer_id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
city TEXT,
email TEXT
);
INSERT INTO customers VALUES
(1, 'Priya Shah', 'Austin', 'priya@example.com'),
(2, 'Tom Becker', 'Denver', NULL),
(3, 'Lena Ortiz', 'Austin', 'lena@example.com'),
(4, 'Marcus Lee', 'Boston', 'marcus@example.com'),
(5, 'Ava Novak', 'Denver', 'ava@example.com');
CREATE TABLE orders (
order_id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers (customer_id),
order_date TEXT,
total REAL
);
INSERT INTO orders VALUES
(101, 1, '2026-01-05', 36.00),
(102, 3, '2026-01-09', 42.00),
(103, 1, '2026-02-14', 12.99),
(104, 4, '2026-02-20', 59.50),
(105, 2, '2026-03-02', 24.00),
(106, 1, '2026-03-15', 21.00);- Result matches the expected table
Step 7 of 15Practice · Work it out
How many rows does this query return? SELECT c.name, o.order_id FROM customers c LEFT JOIN orders o ON c.customer_id = o.customer_id;
Step 8 of 15Compare
INNER JOIN vs. LEFT JOIN
| Aspect | INNER JOIN | LEFT JOIN |
|---|---|---|
| Rows returned | Only rows with a match in both tables | All rows from the left table, matched or not |
| Unmatched left rows | Dropped | Kept, with NULLs in the right table's columns |
| Typical question | Show orders with their customer names | Show every customer, including those with no orders |
| Finding missing matches | Not possible directly | LEFT JOIN plus WHERE right_table.key IS NULL |
| Counting matches per row | Rows with zero matches disappear | Use COUNT(right_column) to get 0 for no matches |
Step 9 of 15Tap to reveal
Other joins you may meet
Joining three or more tables
Joins chain naturally: FROM orders o JOIN customers c ON c.customer_id = o.customer_id JOIN order_items i ON i.order_id = o.order_id. Each JOIN adds one table and its matching condition.
Self join
A table joined to itself under two aliases. An employees table with a manager_id column can be joined to itself to show each employee next to their manager's name.
FULL OUTER JOIN
Keeps unmatched rows from both tables, filling NULLs on whichever side has no match. Useful for reconciling two lists, such as finding records present in one system but missing from another. Some databases, such as older MySQL versions, do not support it directly.
CROSS JOIN
Pairs every row with every row on purpose, for example every store with every month to build a complete reporting grid. Use it deliberately and only with small tables.
Step 10 of 15Learn
Common join mistakes
Joins are where most beginner SQL bugs live. Four to watch for:
1. Joining on the wrong columns. ON o.order_id = c.customer_id runs without error, because both are integers, and silently produces nonsense, matching order 1 to customer 1 even though they're unrelated. Always join a foreign key to the primary key it references.
2. Forgetting the join condition. Listing two tables without a condition, or writing CROSS JOIN, produces a Cartesian product: every row of one table paired with every row of the other. 5 customers × 6 orders = 30 rows. On real tables with millions of rows, this can grind a database to a halt.
3. Turning a LEFT JOIN back into an INNER JOIN. This query looks like it lists all customers with their 2026 March orders:
SELECT c.name, o.order_id
FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id
WHERE o.order_date >= '2026-03-01';But for customers without matching orders, o.order_date is NULL, and the WHERE condition removes them. The LEFT JOIN's purpose is defeated. Move the condition into the ON clause to filter only the joined orders:
LEFT JOIN orders o
ON c.customer_id = o.customer_id
AND o.order_date >= '2026-03-01'Now all 5 customers appear, with March orders where they exist.
4. Double counting after a join. Joining customers to orders repeats each customer once per order. Summing a customer-level column, such as a loyalty credit, after the join counts it multiple times. Aggregate at the right level, or aggregate before joining.
Sanity-check every join: estimate how many rows you expect, then compare with what you get.
Step 11 of 15Practice · Code challenge · SQL
Code challenge · SQL
List every customer with the number of orders they placed, including customers with no orders (who must show 0).
The starter runs, but one customer gets the wrong count. Find out why and fix it.
The database for this exercise (tables and rows)
CREATE TABLE customers (
customer_id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
city TEXT,
email TEXT
);
INSERT INTO customers VALUES
(1, 'Priya Shah', 'Austin', 'priya@example.com'),
(2, 'Tom Becker', 'Denver', NULL),
(3, 'Lena Ortiz', 'Austin', 'lena@example.com'),
(4, 'Marcus Lee', 'Boston', 'marcus@example.com'),
(5, 'Ava Novak', 'Denver', 'ava@example.com');
CREATE TABLE orders (
order_id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers (customer_id),
order_date TEXT,
total REAL
);
INSERT INTO orders VALUES
(101, 1, '2026-01-05', 36.00),
(102, 3, '2026-01-09', 42.00),
(103, 1, '2026-02-14', 12.99),
(104, 4, '2026-02-20', 59.50),
(105, 2, '2026-03-02', 24.00),
(106, 1, '2026-03-15', 21.00);- Result matches the expected table
Step 12 of 15Practice · Spot the mistake
This query should list all customers, including those with no orders, along with how many orders each has placed. Which lines contain mistakes? Click them.
Step 13 of 15Practice · Work it out
If you join the 5-row customers table and the 6-row orders table with no join condition at all, how many rows does the result contain?
Step 14 of 15Practice · Chart
Rows returned by each join of customers (5) and orders (6)
Which of these is the warning sign of a forgotten or wrong ON condition?
Tap the bar that answers the question, then press Check.
Step 15 of 15Practice · What would you do?
What would you do?
A marketing manager asks for a list of customers who have never placed an order, so she can send them a welcome discount. Which query do you write?
Lesson complete!