Learn SQL
-- joining.self_cross_join

SELF JOIN & CROSS JOIN in SQL

Join a table to itself; know the cartesian explosion

--concept

SELF JOIN joins a table to itself with aliases — hierarchies (employee/manager), pairs, consecutive rows. CROSS JOIN produces the full cartesian product (every combination) — usually a forgotten-ON bug, deliberately for grids and test matrices. Non-equi joins match ranges instead of equality (salary bands, effective dates).

--how it works
1

SELF needs two aliases

Same table left and right (e vs m). Alias discipline is readability.

2

Hierarchies via SELF

employee.manager_id → manager.id resolves names without a second table.

3

CROSS multiplies

No ON — every left × every right. 6 × 3 = 18 rows here.

4

Missing ON is a CROSS

Forgotten join condition silently becomes cartesian. Row-count check catches it.

--visualization
employees e LEFT JOIN employees m ON e.manager_id = m.id
e.namem.name (manager)
AliceNULL
BobNULL
CynthiaNULL
Stage 1 / 1
One table, two aliases — hierarchies without a second table.
--example
query.sql
1-- manager names via self join:
2SELECT e.name, m.name AS manager FROM employees e LEFT JOIN employees m ON e.manager_id = m.id LIMIT 2;
Result
namemanager
Alicenull
Bobnull

Each employee row looks itself up as a manager row. With no manager_id populated the manager side is NULL — the LEFT preserves the employee. CROSS JOIN with departments would instead multiply every employee by every department.

--variants

Manager names via SELF

One table acts as two roles.

query.sql
1SELECT e.name, m.name AS manager FROM employees e LEFT JOIN employees m ON e.manager_id = m.id LIMIT 2;
Result
namemanager
Alicenull
Bobnull
1e scans employees as workers.
2Each row probes the same table as managers by id.
3No manager_ids populated → NULL managers, LEFT preserves workers.

Deliberate CROSS grid

Every combination — test matrices, size/color grids.

query.sql
1SELECT e.name, d.dept_id FROM employees e CROSS JOIN departments d LIMIT 3;
Result
namedept_id
AliceEngineering
AliceSales
AliceMarketing
1No matching — pure product.
2Alice pairs with all three departments.
3Full result would be 18 rows — verify intent before running wide.
--key facts
SELFone table, two aliases — hierarchies and pairs
CROSSno ON, full product — usually a bug, sometimes a grid
Row-count checkunexpected multiplication means a missing ON
--use cases
Org charts, pairs, consecutive-row diffs via SELF
Grid generation: variants, calendars, test matrices via CROSS
--under the hood
SELF joins benefit from an index on the self-FK (manager_id); CROSS joins are the planner’s cheapest shape — no key, just nested loops.
Accidental CROSS on big tables is the pager incident — lint for JOIN without ON.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.