Before continuing: This entry is part of the JOIN guide. If you are new to the topic, start with JOIN.
CROSS JOIN example
| id | name |
|---|---|
| 1 | Amy Wilson |
| 2 | Liam Martin |
| id | name |
|---|---|
| 1 | Mouse |
| 2 | Keyboard |
We want to prepare a proposal that combines each of those customers with each product:
SELECT c.name AS customer,
p.name AS product
FROM customers AS c
CROSS JOIN products AS p
WHERE c.id IN (1, 2)
AND p.id IN (1, 2)
ORDER BY c.id, p.id;| customer | product |
|---|---|
| Amy Wilson | Mouse |
| Amy Wilson | Keyboard |
| Liam Martin | Mouse |
| Liam Martin | Keyboard |
Check the result with small data
There are two months and two channels to plan a report for.
WITH months(month) AS (VALUES ('January'), ('February')),
channels(channel) AS (VALUES ('Web'), ('Store'))
SELECT month, channel FROM months CROSS JOIN channels
ORDER BY month, channel;| month | channel |
|---|---|
| February | Store |
| February | Web |
| January | Store |
| January | Web |
The query generates every possible pair and needs no ON condition. Check the row counts before crossing large tables.
Check your understanding: How many rows would there be if you added a third channel?
Show answer
6 rows: 2 months × 3 channels. CROSS JOIN multiplies the rows of one source by those of the other.
How CROSS JOIN is used
CROSS JOIN needs no ONcondition: asking for every combination is the whole point. You can filter afterwards with WHERE, although with large tables it is better to reduce the data first.
Common mix-ups with CROSS JOIN
Creating a Cartesian product by accident
SELECT c.name, p.id
FROM customers AS c, orders AS p;A comma between tables with no condition can pair every customer with every order. If you meant to relate them through customer_id, use a JOIN ... ON. Save CROSS JOIN for cases where every combination really is the goal.
Building a complete set of combinations and predicting its size
A deliberate use is generating every combination of shops and days for a report. Three shops and seven days produce twenty-one pairs before any sales are added. You can then bring in the transactions with an outer join so that days with no activity also appear. The Cartesian product defines the expected set; the sales alone should not decide which combinations appear in the report.
Work out the size before running it. A thousand products by a hundred thousand customers produce a hundred million pairs, even if none of them reflects a real relationship. If you only need relationships that are stored, you are missing a condition or a source that describes those pairs. Applying a LIMIT to end up with a few rows does not prove the query has the right meaning or cost.
Include a case where one side is empty: the Cartesian product returns zero rows because no combination is possible. Also check for repeated keys in the dimension lists, since every occurrence takes part. For results you can compare, add a final sort on the chosen keys. Use names and aliases that make it obvious you are building combinations, rather than hiding a forgotten join condition.
Technical questions: PostgreSQL: Table Expressions and Joins (opens in a new tab).