CROSS JOIN
The CROSS JOIN keyword returns the cartesian product ā every row from a paired with every row from b.
š” Returns the Cartesian product ā every row from A paired with every row from B
Visual Diagram
Table A JOIN Table B
āāāāāāāāāāā āāāāāāāāāāā
ā Rows ā āāāāāāāāā ā Rows ā
ā from ā matched ā from ā
ā A ā by key ā B ā
āāāāāāāāāāā āāāāāāāāāāā
Basic Syntax
SELECT columns
FROM table1
CROSS JOIN table2
ON table1.column = table2.column;
Example
SELECT
c.first_name,
c.last_name,
o.total AS order_total
FROM customers c
CROSS JOIN orders o ON c.id = o.customer_id;
ā ļø Always specify the join condition in the ON clause. Omitting it creates a CROSS JOIN (Cartesian product)!
ā Key Takeaways
- CROSS JOIN combines rows from two tables based on a related column
- The ON clause specifies how the tables are related
- Use table aliases to make queries more readable
- Different join types determine which rows are included
- Practice joins with sample data to understand the differences