Cross Join in SQL
? CROSS JOIN in SQL
A CROSS JOIN produces the Cartesian product of two tables, meaning it returns all possible combinations of rows from both tables.
? Syntax
SELECT *FROM table1CROSS JOIN table2;? Example
Suppose you have two tables:
Colors
| color |
|---|
| Red |
| Blue |
Shapes
| shape |
|---|
| Circle |
| Square |
Query:
SELECT color, shapeFROM ColorsCROSS JOIN Shapes;Result:
| color | shape |
|---|---|
| Red | Circle |
| Red | Square |
| Blue | Circle |
| Blue | Square |
? Notes
CROSS JOIN returns every combination of rows from both tables.
Number of rows in result = (rows in table1) × (rows in table2).
Often used when you want to combine every row from one table with every row from another.
If you want examples with other join types or filtering CROSS JOIN results, just ask!