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
e1ande2are aliases for the same tableEmployees.e1.ManagerID = e2.EmployeeIDis the join condition that matches employees with their managers.- We use
LEFT JOINto 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):
ManagerIDisNULL, so there is no matchingEmployeeIDine2.- Result:
EmployeeName= John,ManagerName=NULL.
-
Employee with ID 2 (Jane):
ManagerIDis1, which matchesEmployeeIDof John ine2.- Result:
EmployeeName= Jane,ManagerName= John.
-
Employee with ID 3 (Jim):
ManagerIDis1, which matchesEmployeeIDof John ine2.- Result:
EmployeeName= Jim,ManagerName= John.
-
Employee with ID 4 (Jack):
ManagerIDis2, which matchesEmployeeIDof Jane ine2.- Result:
EmployeeName= Jack,ManagerName= Jane.
-
Employee with ID 5 (Jill):
ManagerIDis2, which matchesEmployeeIDof 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.
Your Feedback
Help us improve by sharing your thoughts
Online Learner helps developers master programming, database concepts, interview preparation, and real-world implementation through structured learning paths.
Quick Links
© 2023 - 2026 OnlineLearner.in | All Rights Reserved.
