Cross Join
What you'll learn: You'll learn how a CROSS JOIN combines every row from one table with every row from another, producing a complete Cartesian product.
What is a Cross Join?
A CROSS JOIN creates every possible pairing between rows in two tables. Unlike the joins you've seen before (inner, left, right, full), a cross join doesn't look for matching conditions—it simply matches everything with everything.
If Table A has 3 rows and Table B has 4 rows, a cross join produces 3 × 4 = 12 rows. Each row from A is paired with each row from B.
When Would You Use This?
Think of it like creating all possible combinations:
- Every shirt color with every pant size
- Every student paired with every possible exam time slot
- Every product combined with every available warehouse
Basic Syntax
SELECT *
FROM TableA
CROSS JOIN TableB;
Or the implicit syntax (without the keyword):
SELECT *
FROM TableA, TableB;
Both produce identical results: a full Cartesian product.
Example in Action
If you have a colors table (Red, Blue) and a sizes table (Small, Medium, Large), a cross join gives you all six combinations:
- Red-Small, Red-Medium, Red-Large
- Blue-Small, Blue-Medium, Blue-Large
Watch Out
Cross joins can explode quickly! A table with 1,000 rows crossed with another 1,000-row table produces 1 million rows. Use them carefully and only when you genuinely need every combination.
Key Takeaway: A CROSS JOIN generates a Cartesian product by pairing every row from one table with every row from another, useful for generating all possible combinations but potentially very large.