SQL Self Join
A self join in SQL is a regular join, but the table is joined with itself. This is useful for comparing rows within the same table. A common use case is to find hierarchical relationships, such as an employee and their manager stored in the same table.
Here’s a basic example to illustrate the concept:
Example Table: Employees
EmployeeID | EmployeeName | ManagerID |
---|---|---|
1 | John | NULL |
2 | Jane | 1 |
3 | Jim | 1 |
4 | Jack | 2 |
5 | Jill | 2 |
Scenario
We want to find each employee along with their manager's name.
SQL Self Join Query
SELECT
e1.EmployeeID AS EmployeeID,
e1.EmployeeName AS EmployeeName,
e2.EmployeeName AS ManagerName
FROM
Employees e1
LEFT JOIN
Employees e2
ON
e1.ManagerID = e2.EmployeeID;
Explanation
e1
ande2
are aliases for the same tableEmployees
.e1.ManagerID = e2.EmployeeID
is the join condition that matches employees with their managers.- We use
LEFT JOIN
to include employees who do not have a manager (e.g., the CEO).
Output
EmployeeID | EmployeeName | ManagerName |
---|---|---|
1 | John | NULL |
2 | Jane | John |
3 | Jim | John |
4 | Jack | Jane |
5 | Jill | Jane |
Detailed Steps:
-
Employee with ID 1 (John):
ManagerID
isNULL
, so there is no matchingEmployeeID
ine2
.- Result:
EmployeeName
= John,ManagerName
=NULL
.
-
Employee with ID 2 (Jane):
ManagerID
is1
, which matchesEmployeeID
of John ine2
.- Result:
EmployeeName
= Jane,ManagerName
= John.
-
Employee with ID 3 (Jim):
ManagerID
is1
, which matchesEmployeeID
of John ine2
.- Result:
EmployeeName
= Jim,ManagerName
= John.
-
Employee with ID 4 (Jack):
ManagerID
is2
, which matchesEmployeeID
of Jane ine2
.- Result:
EmployeeName
= Jack,ManagerName
= Jane.
-
Employee with ID 5 (Jill):
ManagerID
is2
, which matchesEmployeeID
of Jane ine2
.- Result:
EmployeeName
= Jill,ManagerName
= Jane.
This example shows how a self join can be used to link rows within the same table to find hierarchical or relational data.
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Copyright 2023-2025 © All rights reserved.