MySQL CROSS JOIN
MySQL CROSS JOIN: Generating Cartesian Combinations
The MySQL CROSS JOIN pairs every row from the first table with every row from the second table, creating a Cartesian product. If Table A has 5 rows and Table B has 4 rows, CROSS JOIN produces 5 x 4 = 20 output rows.
Basic Syntax
Because a CROSS JOIN matches every combination, it does not use an ON condition:
SELECT a.column1, b.column2
FROM table_a AS a
CROSS JOIN table_b AS b;
Practical SQL Example
A classic real-world use case is generating every size and color SKU variant—or building a full reporting grid of departments and quarters before joining metrics:
-- Build all product-tier and region pricing combinations
SELECT
p.product_name,
r.region_code
FROM demo_products AS p
CROSS JOIN (
SELECT 'US' AS region_code
UNION ALL SELECT 'EU'
UNION ALL SELECT 'APAC'
) AS r
WHERE p.is_active = 1
ORDER BY p.product_name, r.region_code;
| product_name | region_code |
|---|---|
| Mechanical Keyboard | APAC |
| Mechanical Keyboard | EU |
| Mechanical Keyboard | US |
Common Mistakes and Best Practices
- Beware Row Explosions on Large Tables: Cross-joining a 10,000-row table with another 10,000-row table generates 100 million rows and can exhaust temporary disk space. Filter both inputs as aggressively as possible first.
- Avoid Accidental Cross Joins: In MySQL, writing
FROM demo_orders, demo_customerswithout aWHEREjoin condition or writingINNER JOINwithoutONperforms aCROSS JOIN. Always specifyCROSS JOINexplicitly when a Cartesian product is intentional.
Frequently Asked Questions
Does CROSS JOIN accept an ON clause in MySQL?
No, adding ON turns it into a filtered join; use WHERE if you want to filter combinations after crossing, or INNER JOIN if matching keys.
Why is CROSS JOIN useful in analytics reports?
Cross-joining a calendar date series with a list of categories lets you LEFT JOIN orders afterward so days with zero sales still appear as 0 in charts.
Can I cross-join more than two tables?
Yes, chaining CROSS JOIN multiplies the row counts of all participating tables together.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.