Cross join in SQL
Learn how to use a cross join in SQL to generate every combination of two tables, check row counts, filter results, and avoid accidental duplicate combinations.

A cross join in SQL combines every row from one input with every row from another input, producing every possible combination. Use CROSS JOIN between the inputs without an ON condition; if the inputs contain two rows and three rows, the result contains six rows.
You'll use PostgreSQL to generate product and size combinations, check the result, and then exclude one size. The example uses temporary query definitions rather than permanent tables, so you can run it without creating or changing stored data.
The purpose of a cross join
A cross join builds a complete set of combinations when the inputs don't need a matching relationship. The result is called a Cartesian product: each row on the left appears once with each row on the right.
Suppose you're preparing a product setup sheet. You have a list of products and a separate list of sizes. Before checking inventory or deciding which combinations to sell, you want a starting grid containing every product in every size.
- Use a cross join for every product and size combination.
- Use a cross join for every location and reporting period combination.
- Use a cross join for every combination of two sets of scenario assumptions.
A cross join doesn't prove that a combination exists in the real world. A generated product and size combination might not be stocked or even manufactured. Treat the output as a candidate grid, not as evidence of availability.
A complete PostgreSQL example
The following query generates every combination of two fictional products and three sizes. Copy the complete block into a PostgreSQL query editor and run it as one statement.
WITH defines common table expressions, or CTEs: named query results available within the statement. VALUES supplies the starting rows directly. The names p and s are aliases, which are short names used to refer to the two inputs.
-- Fictional exercise: PostgreSQL
-- Target: generate every product and size combination.
-- Starting products: Cap, Shirt.
-- Starting sizes: S, M, L.
-- Changes needed: none; run the complete statement.
-- Expected count: 2 products * 3 sizes = 6 rows.
WITH products(product) AS (
VALUES ('Cap'), ('Shirt')
),
sizes(size) AS (
VALUES ('S'), ('M'), ('L')
)
SELECT p.product, s.size
FROM products AS p
CROSS JOIN sizes AS s
ORDER BY p.product, s.size;
-- Expected output:
-- product | size
-- Cap | L
-- Cap | M
-- Cap | S
-- Shirt | L
-- Shirt | M
-- Shirt | S
PostgreSQL takes each product row and combines it with all three size rows. SELECT chooses the columns to display. ORDER BY sorts the final output by product and then by size; the size labels appear alphabetically, not in clothing-size order.
There's no ON condition because the query isn't looking for a shared identifier. Adding a condition that connects matching identifiers would answer a different question: which rows relate to each other, rather than which combinations are possible.
Try writing the combination query yourself in CROSS JOIN, then compare your column choices and expected row count with the example.
A row-count check
Check a cross join by multiplying the number of rows in its inputs before inspecting individual combinations. Counting rows catches an unexpected input size even when the displayed results look reasonable.
For a count-only check, replace the previous statement with the complete statement below. The starting values stay the same, but SELECT now uses COUNT(*) to count output rows instead of displaying their columns.
-- PostgreSQL verification of the same fictional inputs.
-- Target: verify that 2 * 3 = 6 output rows.
-- Change: replace the previous statement with this one.
WITH products(product) AS (
VALUES ('Cap'), ('Shirt')
),
sizes(size) AS (
VALUES ('S'), ('M'), ('L')
)
SELECT COUNT(*) AS combination_count
FROM products AS p
CROSS JOIN sizes AS s;
-- Expected output:
-- combination_count
-- 6
A CTE exists only within its statement. You can't run a separate query against products after the first statement finishes. The verification query repeats both CTEs so you can run the check independently.
A filtered variation
A WHERE condition can restrict the combinations returned by a cross join. Keep the same products and sizes, but now generate combinations that exclude size L.
Replace the earlier statement with the complete query below. The exact change to the combination query is the WHERE line immediately before ORDER BY. Each product now has two eligible sizes, so the expected result contains four rows.
-- Fictional variation: PostgreSQL
-- Target: generate combinations excluding size L.
-- Starting products: Cap, Shirt.
-- Starting sizes: S, M, L.
-- Change: add WHERE s.size <> 'L' before ORDER BY.
-- Eligible sizes: S, M.
-- Expected count: 2 products * 2 eligible sizes = 4 rows.
WITH products(product) AS (
VALUES ('Cap'), ('Shirt')
),
sizes(size) AS (
VALUES ('S'), ('M'), ('L')
)
SELECT p.product, s.size
FROM products AS p
CROSS JOIN sizes AS s
WHERE s.size <> 'L'
ORDER BY p.product, s.size;
-- Expected output:
-- product | size
-- Cap | M
-- Cap | S
-- Shirt | M
-- Shirt | S
The operator <> means “not equal to.” Here, the condition checks only the size input, so it removes size L for every product. A condition involving both inputs could instead exclude particular product and size combinations.
Think of the cross join as defining all combinations and WHERE as defining which combinations qualify. PostgreSQL may optimize how it executes the statement, so that explanation describes the query's meaning rather than a guaranteed physical execution order.
The right join for the task
Choose CROSS JOIN only when every combination is intentional. If you're connecting records through a shared key, such as a product identifier, a matching join usually expresses the task more clearly.
- CROSS JOIN: combine each row from one input with every row from the other input.
- INNER JOIN: return row combinations that satisfy a matching condition.
- LEFT JOIN: keep every left-side row, adding matching right-side values where available and NULL where no match exists.
For example, generating candidate product and size combinations is different from attaching product names to existing order lines. The order-line task needs a relationship between identifiers, not every order line combined with every product.
If your task involves matching identifiers, practice INNER JOIN: combine matching rows before adapting the combination example.
Common mistakes and limits
The main risk is generating more combinations than your task needs. Output size multiplies across inputs, so adding another input multiplies the result again rather than simply adding rows.
- Unexpected repeated combinations: inspect the source rows and their identifiers. Separate records can share the same displayed label.
- Unexpected row order: use ORDER BY. SQL doesn't guarantee display order without an explicit sort.
- An empty result: check whether either input is empty. Every combination requires a row from both inputs.
- Unexpected missing combinations: inspect WHERE conditions, especially conditions that refer to both inputs.
- A large result: restrict the input sets to the relevant records and calculate the expected count before returning all columns.
Don't add DISTINCT automatically to make repeated labels disappear. DISTINCT removes duplicate output rows, but it doesn't explain whether the source records are duplicates or whether your SELECT omitted an important identifier.
The method's limitation is that it generates possibilities, not business rules. Real availability, valid scheduling combinations, and permitted scenarios need additional data or conditions. Large combinations can also be expensive to compute, sort, and transfer.
Independent practice
Practice by rebuilding the original six-row result without copying, then adding the size exclusion and checking all four remaining combinations. Explain why CROSS JOIN needs no matching condition and why the filter changes the expected count.
Common questions
Does CROSS JOIN need an ON clause? No. In PostgreSQL, CROSS JOIN combines every row from both inputs without an ON clause. Use an appropriate matching join when you need a condition connecting the inputs.
Does CROSS JOIN remove duplicates? No, a cross join preserves combinations involving repeated input rows. Inspect the inputs and selected identifiers before deciding whether DISTINCT is appropriate.
What happens when one input is empty? The cross join returns no rows because there are no combinations containing a row from each input. If you need to preserve rows without a match, consider whether a LEFT JOIN expresses your task.
Can I cross join more than two inputs? Yes, you can add another CROSS JOIN to combine the existing combinations with every row of another input. Multiply the row counts of all inputs to predict the unfiltered result size.
Practice what this post teaches
Once you can predict the output before running the query, continue with Practice: more joins. Choose each join by the relationship your task requires.