Skip to content

Self-Joins

A Self-Join is not a different type of join; it is a regular join (usually INNER or LEFT) where a table is joined to itself. This is essential for querying data that has a hierarchical or recursive structure.

A self-join occurs when a table has a “Self-Referencing” foreign key. This means one column in the table refers to the Primary Key of the same table.

  1. Organizational Charts: An Employees table where each row has a ManagerID (which is just another EmployeeID).
  2. Product Categories: A Categories table where a sub-category has a ParentCategoryID.
  3. Historical Comparison: Comparing a row’s value to the value of the “previous” row in the same table.

Detailed Explanation: The Power of Aliasing

Section titled “Detailed Explanation: The Power of Aliasing”

To perform a self-join, you must use table aliases. Since the table name is the same, the database needs a way to distinguish between the two instances of the table.

Think of it as having two identical photocopies of the same table sitting side-by-side on your desk. You might name one “Employees” and the other “Managers”.

Self-Join Syntax
SELECT
e.Name AS EmployeeName,
m.Name AS ManagerName
FROM Staff e
JOIN Staff m ON e.ManagerID = m.EmployeeID;

We have a Staff table:

IDNameRoleManagerID
1SarahCEONULL
2MikeVP1
3JanetVP1
4SteveDev2

Task: List every employee and their manager’s name. Include the CEO.

Section titled “Task: List every employee and their manager’s name. Include the CEO.”
Hierarchy Query
SELECT
emp.Name AS StaffMember,
mng.Name AS ReportsTo
FROM Staff emp
LEFT JOIN Staff mng ON emp.ManagerID = mng.ID;
StaffMemberReportsTo
SarahNULL
MikeSarah
JanetSarah
SteveMike

Note: We used a LEFT JOIN so that Sarah (the CEO) wouldn’t be excluded from the list, even though she has no manager.

Self-joins are the building blocks of Recursive Queries.

In our example, we can see who reports to Mike (Steve). But what if we wanted to see everyone in Mike’s chain of command, across 5 different levels of management?

  • A standard self-join only looks at one level of the relationship (Parent/Child).
  • To traverse an entire tree structure, we use Recursive CTEs (Common Table Expressions), which we will cover in Module 5.

When self-joining large tables, ensure that both the Primary Key (ID) and the Self-Referencing Key (ManagerID) are indexed. Without indexes, a self-join can quickly degrade into a very slow Nested Loop join.