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