# Self & Cross Joins

> Join a table to itself to follow manager links, and use CROSS JOIN to build every combination of two lists.

Subject: SQL | Lesson 15 of 44 | 5 min read
Interactive lesson: https://codewithanimation.in/sql/self-and-cross-joins
Previous: [LEFT, RIGHT & FULL Joins](https://codewithanimation.in/sql/outer-joins.md) | Next: [Joining Many Tables](https://codewithanimation.in/sql/joining-many-tables.md)

## Two special joins

A **self join** joins a table to itself. It is not a new keyword: you list the same table twice with two different aliases, and treat each copy as its own table. It is used when a row points at another row of the same table, such as an employee who points at a manager.

A **cross join** pairs every row of one table with every row of another. There is no `ON`, so nothing is filtered. Two tables with 3 and 2 rows give 3 x 2 = 6 rows.

## Trees and combinations

Org charts, comment threads and category trees all store a link to a row in the same table. A self join turns that link into a name you can read. A cross join builds every size and color combination for a shop without typing them out.

## Show each employee next to their manager

```sql
SELECT e.name AS employee, m.name AS manager
FROM staff AS e
LEFT JOIN staff AS m
  ON e.manager_id = m.id;
```

`e` is the employee copy of `staff` and `m` is the manager copy. The `ON` test says: take the employee's `manager_id` and find the row with that `id`. Aliases are required here, because without them both copies would have the same name.

Asha has `NULL` as her manager, so the `LEFT JOIN` shows her with a NULL manager. An `INNER JOIN` would drop her.

A cross join is written without `ON`: `FROM sizes CROSS JOIN colors`. Sizes S, M, L and colors Red, Blue give 6 pairs. It is the same as an inner join whose test is always true.

## Common mistake: Cross joins grow fast

Row counts multiply. Two tables of 1,000 rows give 1,000,000 rows. A cross join is almost always a mistake when it appears by accident, for example when you list two tables with a comma and forget the join test.

In a self join, mixing up the two aliases is easy. Read `e.manager_id = m.id` aloud: the employee's manager id equals the manager's id.

## Tip: Use roles as alias names

Name the aliases after the role each copy plays, such as `e` and `m` or `employee` and `manager`, not `a` and `b`. Use `LEFT JOIN` when the top of the tree has no parent.

## Try it: Follow the manager link

Switch between INNER and LEFT in the self join and watch Asha appear and disappear. Then switch to the cross join and count the pairs.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/sql/self-and-cross-joins

## Check yourself

### Question 1

`sizes` has 3 rows and `colors` has 2 rows. How many rows does `sizes CROSS JOIN colors` return?

- A. 6
- B. 5
- C. 3

Correct answer: A

Every size is paired with every color: 3 x 2 = 6.

- Why not B: 5 is 3 plus 2. A cross join multiplies the counts.
- Why not C: Each size is paired with every color, not just one.

### Question 2

Why does a self join on `staff` need aliases?

- A. Both copies have the same table name, so the aliases tell them apart
- B. Aliases make the query faster
- C. A table cannot be joined to itself without a foreign key

Correct answer: A

Without aliases, a column such as name would be ambiguous. Aliases give each copy its own name.

- Why not B: Aliases only name things. They do not change speed.
- Why not C: A join only needs an ON test. A foreign key is not required.

### Question 3

Asha, the top manager, has a NULL `manager_id`. She is missing from `staff e JOIN staff m ON e.manager_id = m.id`. Which change keeps her?

- A. Use LEFT JOIN instead of INNER JOIN
- B. Use CROSS JOIN
- C. Change ON to e.manager_id = NULL

Correct answer: A

A NULL key never matches, so an INNER JOIN drops Asha. LEFT JOIN keeps every employee and shows NULL for the manager.

- Why not B: A cross join pairs everyone with everyone and has no ON test, which is a different result.
- Why not C: = NULL is never true, so nothing would match.

## Remember this

- A self join lists the same table twice with two aliases, and needs an ON test that links the two copies.
- Use LEFT JOIN in a self join when the top row has no parent.
- CROSS JOIN has no ON and returns every pairing, so N rows and M rows give N x M rows.
- A cross join is the same as an inner join whose test is always true.
