✏️ Explanatory Question
Level: Intermediate — Tests your understanding of Cartesian products and join conditions.
A CROSS JOIN returns the Cartesian product of two tables — it pairs every row of the first table with every row of the second table. There is no join condition.
If table A has m rows and table B has n rows, a CROSS JOIN produces m × n rows.
$$ total\_rows = m \times n $$
| Feature | CROSS JOIN | INNER JOIN |
|---|---|---|
| Join condition (ON) | None | Required |
| Result | Cartesian product (m × n) | Only matching rows |
| Row count | Very large | Depends on matches |
| Typical use | Generate combinations | Combine related data |
-- Sample: sizes(3 rows) and colors(4 rows)
-- CROSS JOIN produces 3 x 4 = 12 combinations
SELECT s.size, c.color
FROM sizes s
CROSS JOIN colors c;
-- Practical: every product variant (size + color)
SELECT p.product_name, s.size, c.color
FROM products p
CROSS JOIN sizes s
CROSS JOIN colors c;
-- Compare: INNER JOIN needs a condition
SELECT e.name, d.dept_name
FROM employees e
INNER JOIN departments d ON e.dept_id = d.dept_id; -- filtered by match