advanced
Cross Joins
Produce every combination of rows from two tables with CROSS JOIN.
5 min read
Explanation
A CROSS JOIN returns the Cartesian product: every row from the first table paired with every row from the second. If table A has 5 rows and table B has 4, the result has 20 rows.
It's the only join with no ON condition, because there's nothing to match
— you want all combinations. Use it deliberately; an accidental cross join (a
missing ON) is a classic performance disaster.
Cap the size
Cross joins explode in size fast. Filter or LIMIT the inputs, or you can
generate millions of rows from modest tables.
Syntax
SELECT a.col, b.col
FROM table_a a
CROSS JOIN table_b b;Interactive Example
Pair each product category with each customer city to imagine a category-by-city matrix. Both sides are small, so the result stays readable.
Loading database engine...
Loading database engine...
Common Mistakes
- Accidental cross join. Forgetting
ONin anINNER JOINsilently produces a Cartesian product — always verify row counts. - Unbounded size. Cross joining two large tables can create billions of rows; pre-aggregate or limit first.
- Confusing it with a bad join. If you meant to match on a key, use a real join, not CROSS JOIN.
Best Practices
- Reach for CROSS JOIN only when you genuinely need all combinations.
- Keep inputs small with
DISTINCT/filters before crossing. - After writing one, check the row count to confirm it's what you expect.
Practice Question
Using a CROSS JOIN, generate a matrix of every category from products paired
with every status from orders, and count how many combinations exist.
Summary
CROSS JOIN returns every combination of two tables (the Cartesian product) with
no ON clause. Use it intentionally for combination matrices, keep inputs small,
and never let one sneak in by forgetting a join condition.