Sub topic 9
Generating Every Combination of Two Lists
This chapter assumes you know SELECT, table aliases, INNER JOIN and LEFT JOIN, primary keys, and basic row counting. You will use CROSS JOIN to pair every row in one table with every row in another table.
The Cartesian product
A Cartesian product is the set of every possible pair between two sets of rows. In SQL, CROSS JOIN creates this result.
If the first table has m rows and the second table has n rows, the result has:
m × n rows
For example, three sizes and two colors produce six combinations:
| Sizes | Colors | Result rows |
|---|---|---|
| 3 | 2 | 6 |
A CROSS JOIN has no ON condition because it does not match related rows. It combines every row with every other row.
CREATE TABLE sizes (
size VARCHAR(2) PRIMARY KEY
);
CREATE TABLE colors (
color VARCHAR(10) PRIMARY KEY
);
INSERT INTO sizes (size) VALUES
('S'),
('M'),
('L');
INSERT INTO colors (color) VALUES
('red'),
('blue');
SELECT s.size, c.color
FROM sizes AS s
CROSS JOIN colors AS c
ORDER BY s.size, c.color;The query returns these six rows:
| size | color |
|---|---|
| L | blue |
| L | red |
| M | blue |
| M | red |
| S | blue |
| S | red |
The aliases s and c make the selected columns clear. They also help when both tables contain a column with the same name.
Understanding the combination process
You can think of the query as a nested loop:
- Take the first size.
- Pair it with every color.
- Take the next size.
- Pair it with every color again.
- Continue until every size has been processed.
The order in which the database displays rows is not guaranteed unless you use ORDER BY. The combinations are the same regardless of display order.
Generating useful option lists
Cross joins are useful when combinations do not already exist in a table. For example, a store might need a row for every product and sales region before calculating sales or inventory.
CREATE TABLE products (
product_id INT PRIMARY KEY,
product_name VARCHAR(50) NOT NULL
);
CREATE TABLE regions (
region_id INT PRIMARY KEY,
region_name VARCHAR(50) NOT NULL
);
INSERT INTO products (product_id, product_name) VALUES
(1, 'Notebook'),
(2, 'Pen');
INSERT INTO regions (region_id, region_name) VALUES
(10, 'North'),
(20, 'South'),
(30, 'West');
SELECT
p.product_name,
r.region_name
FROM products AS p
CROSS JOIN regions AS r
ORDER BY p.product_id, r.region_id;This query returns six rows: two products multiplied by three regions. Each row represents a valid product-region combination that another process could populate with targets, prices, or stock levels.
Avoiding accidental combinations
A missing join condition can create a Cartesian product by mistake. In older SQL syntax, this query lists two tables separated by a comma:
SELECT p.product_name, r.region_name
FROM products AS p, regions AS r;That syntax also produces every product-region pair. It is valid, but it hides the fact that no matching condition exists. Prefer explicit CROSS JOIN when you want all combinations. Use an ordinary JOIN ... ON when the tables should be matched by related columns.
Choosing a cross join
Use CROSS JOIN for tasks such as:
- Building a product-size or product-color option matrix.
- Creating every date and category pair for a report.
- Testing all combinations of configuration values.
- Preparing rows for later updates or measurements.
Do not use it to connect large tables unless the resulting size is intentional. A table with 10,000 rows crossed with another table containing 10,000 rows produces 100 million rows before filtering.
Exercise
The sizes table has 4 rows and the colors table has 5 rows. How many rows will sizes CROSS JOIN colors return?
Show answer
It will return 20 rows, because 4 × 5 = 20. Every size is paired with every color.