Sub topic 2
From Every Combination to Matching Rows
Assume you know basic SELECT, FROM, WHERE, and ORDER BY syntax, and that a primary key identifies a row while a foreign key refers to a key in another table.
An inner join returns rows from two tables when a condition says that the rows match. A useful way to understand this is to start with a Cartesian product.
The Cartesian product
A Cartesian product pairs every row in one table with every row in another table. If the first table has 3 rows and the second has 4 rows, the product has 12 combinations.
Suppose the tables contain these rows:
| customers | orders |
|---|---|
| Ada, Ben, Chloe | 101, 102, 103, 104 |
The product would pair Ada with all four orders, Ben with all four orders, and Chloe with all four orders. Most of those pairs are meaningless.
A join condition filters the product. For customers and orders, the meaningful condition is:
orders.customer_id = customers.idConceptually, an inner join does this:
- Form possible row combinations.
- Test each combination against the join condition.
- Keep only combinations for which the condition is true.
Database systems usually use more efficient algorithms than literally building every combination. The product-and-filter model explains the result, not necessarily the execution method.
Filtering combinations with a condition
Here is a small schema and dataset:
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name VARCHAR(100) NOT NULL
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER,
total DECIMAL(10, 2) NOT NULL
);
INSERT INTO customers (id, name) VALUES
(1, 'Ada'),
(2, 'Ben'),
(3, 'Chloe');
INSERT INTO orders (id, customer_id, total) VALUES
(101, 1, 50.00),
(102, 1, 75.00),
(103, 2, 20.00),
(104, NULL, 99.00);
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 query returns three rows:
| name | order_id | total |
|---|---|---|
| Ada | 101 | 50.00 |
| Ada | 102 | 75.00 |
| Ben | 103 | 20.00 |
Chloe has no order with customer_id = 3, so she contributes no result row. Order 104 has a NULL customer ID, so it does not match any customer ID.
The ON clause contains the rule that decides which combinations survive. The table aliases c and o make the rule shorter and show which table owns each column.
Inner join as a filtered product
The older comma-style form expresses the same idea:
SELECT c.name, o.id AS order_id
FROM customers AS c, orders AS o
WHERE o.customer_id = c.id;However, forgetting the WHERE condition leaves the full Cartesian product. Explicit INNER JOIN ... ON syntax makes the relationship visible and reduces the chance of creating accidental combinations.
Exercise
If customers has 3 rows and orders has 4 rows, how many combinations does their Cartesian product contain, and how many rows can an inner join return at most?
Show answer
The Cartesian product contains 3 × 4 = 12 combinations. The inner join can return at most 12 rows, although the key condition normally filters out many of them.