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:
Knowledge Canvas
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 in INNER JOIN: a silent cross join in SQLite and MySQL, a syntax error in PostgreSQL, SQL Server and Oracle
- 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
Syntax
CROSS JOINNo ON clause — all combinationsWarningOutput = rows_A × rows_B — use with small tablesSample Data
| 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 | aarav@email.com | Mumbai | India |
| C1002 | Sara Chen | sara@email.com | Singapore | Singapore |
| C1003 | James Wilson | james@email.com | London | UK |
| C1004 | Maria Garcia | maria@email.com | Madrid | Spain |
| C1005 | Yuki Tanaka | yuki@email.com | Tokyo | Japan |
| C1006 | Priya Patel | priya@email.com | Delhi | India |
| C1007 | Alex Johnson | alex@email.com | New York | USA |
| C1008 | Chen Wei | wei@email.com | Shanghai | China |
| C1009 | Emma Brown | emma@email.com | Sydney | Australia |
| C1010 | Omar Hassan | omar@email.com | Dubai | UAE |
| C1011 | Lena Muller | lena@email.com | Berlin | Germany |
| C1012 | Ravi Kumar | ravi@email.com | Bangalore | India |
Worked Example
Generate every unique pair of product categories.
| cat_1 | cat_2 |
|---|---|
| Appliances | Electronics |
| Appliances | Furniture |
| Appliances | Stationery |
| Electronics | Furniture |
| Electronics | Stationery |
| Furniture | 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(*).