SQL Self-Joins
Learn how a single table can be joined with itself to reveal hierarchical relationships and pairwise comparisons hidden within your data.
What Is a Self-Join?
A self-join is a regular SQL join where a table is joined with itself. You treat the same table as if it were two separate tables by giving each copy a different alias.
Why Use a Self-Join?
Many real-world datasets store relationships within a single table. Self-joins let you unlock these hidden connections without needing extra tables.
The Key Ingredient: Aliases
Since both sides of the join reference the same table, SQL needs a way to tell them apart. You must use aliases to give each copy a unique name.
How a Self-Join Works
The General Pattern
Self-Join vs Regular Join
| Self-Join | Regular Join | |
|---|---|---|
| Tables involved | One table joined with itself | Two different tables |
| Aliases required? | Yes (mandatory) | Optional but recommended |
| Use case | Rows related to other rows in same table | Rows in table A related to rows in table B |
| SQL syntax | INNER, LEFT, RIGHT, FULL, CROSS | INNER, LEFT, RIGHT, FULL, CROSS |
| Example | Employee → Manager | Order → Customer |
INNER vs LEFT Self-Join
How to Write a Self-Join
Example 1: Employee-Manager
| id | name | manager_id |
|---|---|---|
| 1 | Alice | NULL |
| 2 | Bob | 1 |
| 3 | Carol | 1 |
| 4 | Dave | 2 |
Example 2: Pairwise Matching
Find all pairs of students who live in the same city. This is a different pattern: instead of a foreign key, we match rows on a shared attribute.
| id | name | city |
|---|---|---|
| 1 | Mei | Austin |
| 2 | Jordan | Austin |
| 3 | Sam | Denver |
| 4 | Priya | Austin |
Why \(\texttt{a.id} < \texttt{b.id}\)?
Without the inequality filter, a pairwise self-join produces duplicate and self-matching pairs. The condition \(\texttt{a.id} < \texttt{b.id}\) eliminates both problems.
Example 3: Category Hierarchy
An online store has a categories table where each category can have a parent_id pointing to another category, forming a tree.
| id | name | parent_id |
|---|---|---|
| 1 | Electronics | NULL |
| 2 | Phones | 1 |
| 3 | Laptops | 1 |
| 4 | Android | 2 |
Two Self-Join Patterns
| Hierarchical | Pairwise | |
|---|---|---|
| Relationship | Parent-child (FK → PK) | Peers sharing an attribute |
| ON clause | \(\texttt{a.fk} = \texttt{b.pk}\) | \(\texttt{a.attr} = \texttt{b.attr} \text{ AND } \texttt{a.id} < \texttt{b.id}\) |
| Join type | LEFT JOIN (include NULLs) | INNER JOIN (only matches) |
| Example | Employee → Manager | Student pairs in same city |
| Duplication risk | Low | High without \(\texttt{a.id} < \texttt{b.id}\) filter |
Common Mistakes
Question 1 of 5
What makes a join a "self-join"?
Question 2 of 5
Given this query, what role does alias m represent?
Question 3 of 5
In a pairwise self-join, why do we add the condition \(\texttt{a.id} < \texttt{b.id}\)?
Question 4 of 5
The CEO has manager_id = NULL. Which join type ensures the CEO still appears in the results?
Question 5 of 5
What happens if you write a self-join without using aliases?