Cross and Self Joins

Lesson 11 of 20

Meet the wildest joins: CROSS JOIN pairs every row with every row, while a self-join makes one table play two roles.

The big picture

Joins that break the pattern

Most joins match rows by a shared column. These two do something different: one multiplies, the other folds a table against itself.

CROSS JOIN

Pairs every row of table A with every row of table B. Three rows times three rows make nine — no ON clause at all.

Cartesian product

That every-row-times-every-row output is called a cartesian product. Row counts explode fast, so reach for it on purpose.

Self join

A table can join itself: employees holds workers AND managers. Aliases give the two copies different names.

Same table, two roles

FROM employees e JOIN employees m ON e.manager_id = m.id reads one table twice, once per alias.