Sub topic 7
Cross Joins and the Cartesian Product
Assume you know SELECT, INNER JOIN, LEFT JOIN, primary keys, and NULL handling. This chapter focuses on joins where rows are combined by position in two sets rather than matched by a related key.
What a cross join does
A cross join returns every possible pair of rows from two tables. This result is called a Cartesian product.
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 matching condition. SQL pairs each row on the left with each row on the right.
CROSS JOIN syntax
Use the explicit CROSS JOIN syntax when you intend to generate all combinations.
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 pairs:
| size | color |
|---|---|
| L | blue |
| L | red |
| M | blue |
| M | red |
| S | blue |
| S | red |
The table aliases s and c make the column sources clear. They are especially useful when both tables contain columns with the same name.
A comma between tables can also create a Cartesian product:
SELECT s.size, c.color
FROM sizes AS s, colors AS c;This older syntax is valid in many SQL systems, but it is easier to misuse. If you add a WHERE condition, the condition acts like a join filter. A reader may miss that relationship because the tables and the condition appear far apart. Prefer CROSS JOIN when you want every combination.
Generating useful combinations
Cross joins are useful when the combinations do not yet exist as rows in a table. Common examples include:
- Every store and every product
- Every date and every account
- Every language and every supported region
- Every product size and every available color
- Every test input and every configuration
For example, an application might need a list of all size-color choices before checking which choices are actually in stock.
SELECT s.size, c.color
FROM sizes AS s
CROSS JOIN colors AS c
ORDER BY s.size, c.color;The result is generated from the two input lists. No matching key is required.
Avoiding accidental Cartesian products
A Cartesian product is often a bug when you expected related rows. Consider these tables:
CREATE TABLE customers (
id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL
);
CREATE TABLE orders (
id INT PRIMARY KEY,
customer_id INT NOT NULL,
total DECIMAL(10, 2) NOT NULL
);This query pairs every customer with every order:
SELECT c.name, o.id, o.total
FROM customers AS c
CROSS JOIN orders AS o;If there are 1,000 customers and 20,000 orders, the query produces 20 million rows. It does not determine which customer placed each order.
To match orders to their customers, use the relationship between the tables:
SELECT c.name, o.id, o.total
FROM customers AS c
INNER JOIN orders AS o
ON o.customer_id = c.id;Before running a cross join, ask why every pair is meaningful. Estimate the row count first. If you expected a smaller result, check whether an ON condition is missing or whether a filter belongs in the query.
Exercise
A table has 4 rows and another table has 7 rows. How many rows will their cross join return, assuming neither table is empty?
Show answer
It will return 4 × 7 = 28 rows.