A shop sells each tee in every size and every colour. You need every pair:
sql
SELECT s.size, k.colour
FROM sizes s
CROSS JOIN colours k;sizes s
| size |
|---|
| S |
| M |
| L |
colours k
| colour |
|---|
| black |
| white |
sizes s CROSS JOIN colours k
| size | colour |
|---|---|
| S | black |
| S | white |
| M | black |
| M | white |
| L | black |
| L | white |
CROSS JOIN has no ON. It pairs every row of the first table with every row of the second. The result is the Cartesian product of the two tables: rows of A × rows of B rows.
Older SQL writes a cross join with a comma:
sql
SELECT s.size, k.colour FROM sizes s, colours k;A join is a product plus a filter
An inner join returns the pairs of the cross join where the condition is true. These two queries return the same rows:
text
SELECT … FROM orders o JOIN customers c ON o.customer_id = c.id;
SELECT … FROM orders o, customers c WHERE o.customer_id = c.id;The database does not build the full product to run a join. The result is the same as if it did.