Sub topic 1
Starting point: what an inner join drops
Prerequisites
This chapter assumes that you can write basic SELECT, FROM, and WHERE queries. You should also know that a primary key uniquely identifies a row, a foreign key refers to a key in another table, and NULL means that a value is missing or unknown. You should have used an INNER JOIN with an ON condition.
A review of inner joins
An inner join returns only rows that have a match in both tables. The join condition tells SQL how to match those rows.
Consider these tables:
CREATE TABLE customers (
customer_id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL
);
CREATE TABLE orders (
order_id INT PRIMARY KEY,
customer_id INT,
total DECIMAL(10, 2) NOT NULL
);
INSERT INTO customers (customer_id, name) VALUES
(1, 'Ada'),
(2, 'Ben'),
(3, 'Cleo');
INSERT INTO orders (order_id, customer_id, total) VALUES
(101, 1, 50.00),
(102, 1, 20.00),
(103, 2, 35.00),
(104, NULL, 15.00);The customers table contains three customers. Ada has two orders, Ben has one, and Cleo has none. Order 104 has no customer ID.
This query joins customers to their orders:
SELECT c.name, o.order_id, o.total
FROM customers AS c
INNER JOIN orders AS o
ON o.customer_id = c.customer_id;The result is:
| name | order_id | total |
|---|---|---|
| Ada | 101 | 50.00 |
| Ada | 102 | 20.00 |
| Ben | 103 | 35.00 |
SQL drops Cleo because no order has customer_id = 3. It also drops order 104 because its customer_id is NULL, so it does not match any customer ID.
What the inner join drops
An inner join does not mean “return all customers and show their orders when available.” It means “return rows where the condition succeeds on both sides.”
For every customer row, ask whether at least one order satisfies this condition:
o.customer_id = c.customer_idThe results are:
| Customer or order | Match? | Result |
|---|---|---|
| Ada and order 101 | Yes | Returned |
| Ada and order 102 | Yes | Returned |
| Ben and order 103 | Yes | Returned |
| Cleo and any order | No | Dropped |
| Order 104 and any customer | No | Dropped |
When a customer has several matching orders, the customer row appears several times. This is expected. The join produces one result row for each matching pair, not one row for each customer.
Why this matters for reporting
Suppose you ask for a list of every customer and their order totals. An inner join cannot show Cleo, because Cleo has no matching order. A report based on this query might incorrectly suggest that Cleo does not exist.
The same issue appears in other questions:
- Which products have never been sold?
- Which employees have no assigned projects?
- Which accounts have no payments?
- Which departments have no employees?
These questions require unmatched rows to remain in the result. An inner join removes those rows before you can inspect them. An outer join changes that behavior by preserving rows from one or both sides.
The first design question is therefore: which table contains the rows that must not disappear? If every customer must appear, customers are the preserved side. If every order must appear, orders are the preserved side. The next chapter uses this choice to introduce outer joins.
Exercise
What rows does this query return? SELECT c.name, o.orderid FROM customers AS c INNER JOIN orders AS o ON o.customerid = c.customer_id WHERE o.total > 30;
Show answer
It returns Ada with order 101 and Ben with order 103. Cleo is already removed because she has no matching order, and order 102 is removed because its total is 20.00.