Sub topic 3
Writing your first INNER JOIN
This chapter assumes you can write a basic SELECT with FROM, WHERE and ORDER BY. It also assumes you know what a primary key and a foreign key are. You will write an INNER JOIN, learn what it returns, and see which rows it leaves out. Later chapters cover aliases, join conditions, and multi-table joins in more detail.
The sample data
The examples use two related tables. orders.customer_id is a foreign key that points to customers.id. Two rows are set up on purpose to have no partner. Chloe has no orders, and order 104 has no customer.
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name VARCHAR(100) NOT NULL
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers (id),
order_date DATE NOT NULL,
total DECIMAL(10, 2) NOT NULL
);
INSERT INTO customers (id, name) VALUES (1, 'Ada'), (2, 'Ben'), (3, 'Chloe');
INSERT INTO orders (id, customer_id, order_date, total) VALUES
(101, 1, '2026-01-05', 50.00),
(102, 1, '2026-02-10', 75.00),
(103, 2, '2026-02-11', 20.00),
(104, NULL, '2026-03-01', 99.00);The basic form
An inner join combines each row of one table with the rows of another table that satisfy a condition. The condition goes in the ON clause.
SELECT c.name, o.id AS order_id, o.total
FROM customers AS c
INNER JOIN orders AS o
ON o.customer_id = c.id;The result has three rows:
| name | order_id | total |
|---|---|---|
| Ada | 101 | 50.00 |
| Ada | 102 | 75.00 |
| Ben | 103 | 20.00 |
Read the query in parts:
FROM customers AS cnames the first table.cis an alias, a short name you can use for the table in the rest of the query.INNER JOIN orders AS onames the second table and gives it the aliaso.ON o.customer_id = c.idis the join condition. A pair of rows is kept only when this condition is true.SELECTthen picks columns from the combined rows. Prefixing each column with its alias (c.name,o.id) tells the database which table it comes from.
The keyword INNER is optional. JOIN on its own means the same thing. This tutorial writes INNER JOIN so the intent is clear to the reader.
What the database does
Conceptually, the database considers pairs of rows, one from each table. It keeps a pair when the ON condition is true. Each kept pair becomes one row of the result, with columns from both tables side by side. A real database does not build every pair, because it uses indexes and smarter algorithms. The result is the same as if it had.
The diagram shows the two kinds of row that disappear. This behavior defines an inner join: a row that has no partner in the other table does not appear in the output.
Rows that are excluded
- Chloe has no rows in
orderswithcustomer_id = 3. No pair satisfies the condition for her, so she is missing. - Order 104 has
customer_idset toNULL. A comparison withNULLis never true, not evenNULL = NULL. No customer matches, so the order is missing.
If your question is "which customers have placed orders?", this is what you want. If your question is "list all customers and show their orders if any", an inner join gives the wrong answer. A later chapter covers outer joins for that case.
Order of tables
For an inner join, the order of the two tables does not change which rows come back. This query returns the same three rows as the first one:
SELECT c.name, o.id AS order_id, o.total
FROM orders AS o
INNER JOIN customers AS c
ON c.id = o.customer_id;The order of the rows in the output is not guaranteed unless you add ORDER BY. The order in which you write the tables matters for outer joins, and you will see why later.
Joining and then filtering
You can combine a join with the WHERE clause. Conceptually, the join pairs rows first, and WHERE then removes the pairs you do not want.
SELECT c.name, o.id AS order_id, o.order_date
FROM customers AS c
INNER JOIN orders AS o
ON o.customer_id = c.id
WHERE o.total > 30
ORDER BY o.order_date;The pairs are Ada with 101, Ada with 102, and Ben with 103. WHERE o.total > 30 drops Ben's order, which has a total of 20.00. The result is:
| name | order_id | order_date |
|---|---|---|
| Ada | 101 | 2026-01-05 |
| Ada | 102 | 2026-02-10 |
ORDER BY runs last, so it can sort by any column from either table.
Common errors
These are the mistakes you are most likely to hit in your first joins.
- Missing or wrong
ONcondition. Some databases reject a join withoutON. Others, such as MySQL, acceptJOINwithout it and return every combination of rows. If a result has far more rows than expected, check the condition first. - Ambiguous column name. Both tables have a column called
id. WritingSELECT idfails with an "ambiguous column" error. Qualify the column asc.idoro.id. - Joining on the wrong columns.
ON o.id = c.idis valid SQL and runs without an error. It pairs order 101 with a customer that has id 101, which does not exist, so the result looks plausible but is wrong. Join a foreign key to the key it references. - Expecting unmatched rows. If Chloe is missing from your output, the join is behaving as designed.
- Mixing up output names. Both tables have an
idcolumn. If you select both without renaming, the result has two columns namedid. UseASto give them distinct names, aso.id AS order_iddoes above.
Summary of the behavior
| Situation | Appears in the result? |
|---|---|
| Row has a match under the `ON` condition | Yes, once per match |
| Row has no match | No |
| Row has `NULL` in the join column | No |
| Row matches several rows in the other table | Yes, once for each match |
The last row of the table matters. Ada has two orders, so Ada appears twice. A later chapter on one-to-many matches covers how this affects row counts and totals.
Exercise
Using the sample data, what does this query return, and why is Chloe absent? SELECT c.name, COUNT(*) AS n FROM customers AS c INNER JOIN orders AS o ON o.customer_id = c.id GROUP BY c.name;
Show answer
It returns Ada with n = 2 and Ben with n = 1. The inner join first produces the three matching pairs (Ada/101, Ada/102, Ben/103), and GROUP BY then counts them per customer. Chloe has no order with customerid = 3, so she never appears in the joined rows and no group is created for her. Order 104 is also absent because its customerid is NULL, and NULL never equals any id.
Exercise
You run SELECT c.name, o.id FROM customers c INNER JOIN orders o ON o.id = c.id and get one row for Ada joined to order 101... but wait, the intent was to list each customer's orders. What is wrong, and what is the fix?
Show answer
The condition compares the primary key of orders (o.id) with the primary key of customers (c.id), so it matches unrelated rows whose ids happen to be equal. The join should compare the foreign key with the key it references: ON o.customer_id = c.id.