CROSS JOIN
Generate every possible combination of rows from two tables
Cartesian Product
CROSS JOIN produces every possible combination of rows from two tables. No ON condition — it generates the pure Cartesian product. Here's what to know:
How CROSS JOIN Works
Generate every combination
- Produces cartesian product: every left row × every right row
- No ON clause needed — all combinations are included
- 10 rows × 10 rows = 100 rows in the result
- Rarely used by accident — but devastating when it happens
Cross Join Dangers
Row explosion warning
- 1,000 × 1,000 = 1,000,000 rows — grows quadratically
- Missing ON clause in INNER JOIN = accidental cross join
- Most "cross join" results are bugs, not features
- Always verify row counts after joins
Legitimate Uses
When cross join is actually correct
- Generate all date × product combinations (fill gaps)
- Create lookup grids: sizes × colors
- Test data generation
- Scaffold: all users × all permission types
Safety Rules
Keep cross joins under control
- Only cross join small reference tables
- Always add WHERE or LIMIT as a safety net
- Document intent: comment why cross join is deliberate
- Verify expected row count before running
CROSS JOINNo ON clause — all combinationsWarningOutput = rows_A × rows_B — use with small tables| id | name | category |
|---|---|---|
| P101 | Wireless Mouse | Electronics |
| P102 | Office Desk | Furniture |
| P103 | LED Desk Lamp | Furniture |
| P104 | USB-C Cable | Electronics |
| P105 | Bluetooth Speaker | Electronics |
| P106 | Office Chair | Furniture |
| P107 | Coffee Maker | Appliances |
| P108 | Notebook Set | Stationery |
| P109 | Wireless Headphones | Electronics |
| P110 | Standing Desk | Furniture |
| id | name | city | country | |
|---|---|---|---|---|
| C1001 | Aarav Sharma | [email protected] | Mumbai | India |
| C1002 | Sara Chen | [email protected] | Singapore | Singapore |
| C1003 | James Wilson | [email protected] | London | UK |
| C1004 | Maria Garcia | [email protected] | Madrid | Spain |
| C1005 | Yuki Tanaka | [email protected] | Tokyo | Japan |
| C1006 | Priya Patel | [email protected] | Delhi | India |
| C1007 | Alex Johnson | [email protected] | New York | USA |
| C1008 | Chen Wei | [email protected] | Shanghai | China |
| C1009 | Emma Brown | [email protected] | Sydney | Australia |
| C1010 | Omar Hassan | [email protected] | Dubai | UAE |
| C1011 | Lena Muller | [email protected] | Berlin | Germany |
| C1012 | Ravi Kumar | [email protected] | Bangalore | India |
Worked Example
Generate every unique pair of product categories.
| cat_1 | cat_2 |
|---|---|
| Appliances | Electronics |
| Appliances | Furniture |
| Appliances | Stationery |
Key Concepts
Pro Tip
Rarely used in basic queries, but essential for generating complete grids and scaffolds — especially reports that need a row for every combination even when data is missing.
When to Use
Report scaffolds (every month × every product), test data generation, date dimensions, all-pairs comparisons.
How many total row combinations would a CROSS JOIN between customers and products produce? Use COUNT(*).