SQL CROSS JOIN
A CROSS JOIN returns every possible combination of rows from two tables.
Itโs called a Cartesian product โ no ON clause is required.
๐น Basic Syntax
SELECT *
FROM table1
CROSS JOIN table2;๐น Example: All Product and Region Combinations
SELECT
p.product_name,
r.region_name
FROM products p
CROSS JOIN regions r;โ If there are 5 products and 3 regions, you get 5 ร 3 = 15 rows โ every combination.
๐น Use Cases
- Generate all combinations (e.g., size ร color)
- Testing and simulations
- Matrix-style reports
โ ๏ธ Caution
CROSS JOIN can return very large results if the tables are big. Use it carefully.
๐ง Quick Recap
| Key Point | Explanation |
|---|---|
CROSS JOIN | Returns all combinations of rows from both tables |
| Output size | Multiplies row counts (rows_A ร rows_B) |
No ON clause | Doesnโt need a join condition |
| Use cases | All pairings, simulations, exhaustive comparisons |
๐ก Use CROSS JOIN when you need every combination of two sets