đ A SELF JOIN is a technique where a table is joined with itself. It is useful when the data in a single table has relationships between its own rows, such as employees and their managers, categories and parent categories, or students and mentors.
đ What is a SELF JOIN?
A SELF JOIN joins a table to itself by treating it as two separate tables using table aliases. Although only one table exists, SQL views each alias as an independent table during query execution.
Information
đ¯ Why Use a SELF JOIN?
A SELF JOIN is useful whenever records in a table are related to other records within the same table.
- đ Display employees and their managers.
- đ Represent parent-child relationships.
- đ Compare records within the same table.
- đ Find duplicate or related data.
- đ Model hierarchical structures.
đ Sample Table
Consider the following Employees table:
| EmployeeID | EmployeeName | ManagerID | Department |
|---|---|---|---|
| 1 | John | NULL | Management |
| 2 | Alice | 1 | IT |
| 3 | Bob | 1 | Finance |
| 4 | David | 2 | IT |
| 5 | Emma | 2 | IT |
đ Understanding the Relationship
In this table:
- EmployeeID uniquely identifies each employee.
- ManagerID stores the EmployeeID of the employee's manager.
- Both columns belong to the same table, making a SELF JOIN possible.
đ Basic Syntax
SELF JOIN Syntax
SELECT a.column_name,
b.column_name
FROM table_name AS a
JOIN table_name AS b
ON a.common_column = b.common_column;đĄ Basic SELF JOIN Example
Display every employee along with their manager's name.
Employees and Their Managers
SELECT e.EmployeeName AS Employee,
m.EmployeeName AS Manager
FROM Employees AS e
LEFT JOIN Employees AS m
ON e.ManagerID = m.EmployeeID;Result:
| Employee | Manager |
|---|---|
| John | NULL |
| Alice | John |
| Bob | John |
| David | Alice |
| Emma | Alice |
Important
đˇī¸ Understanding Table Aliases
Table aliases allow SQL to distinguish between the two references to the same table.
| Alias | Represents |
|---|---|
| e | Employee |
| m | Manager |
đ SELF JOIN with WHERE
Filter the joined results using the WHERE clause.
Employees Managed by John
SELECT e.EmployeeName,
m.EmployeeName AS Manager
FROM Employees e
JOIN Employees m
ON e.ManagerID = m.EmployeeID
WHERE m.EmployeeName = 'John';đ SELF JOIN with ORDER BY
Sort the results for better readability.
Sort Employees by Name
SELECT e.EmployeeName,
m.EmployeeName AS Manager
FROM Employees e
LEFT JOIN Employees m
ON e.ManagerID = m.EmployeeID
ORDER BY e.EmployeeName;đ Comparing Rows Within the Same Table
A SELF JOIN can also compare records inside a table.
Employees in the Same Department
SELECT e1.EmployeeName AS Employee1,
e2.EmployeeName AS Employee2,
e1.Department
FROM Employees e1
JOIN Employees e2
ON e1.Department = e2.Department
WHERE e1.EmployeeID < e2.EmployeeID;The condition e1.EmployeeID < e2.EmployeeID prevents duplicate pairs and avoids matching an employee with themselves.
âī¸ INNER SELF JOIN vs LEFT SELF JOIN
| Join Type | Behavior |
|---|---|
| INNER JOIN | Returns only rows that have matching records. |
| LEFT JOIN | Returns all rows from the first alias, even if no matching record exists. |
đŧ Real-World Example
A company wants a report showing every employee together with their reporting manager.
Employee Hierarchy Report
SELECT e.EmployeeID,
e.EmployeeName,
m.EmployeeName AS ManagerName,
e.Department
FROM Employees e
LEFT JOIN Employees m
ON e.ManagerID = m.EmployeeID
ORDER BY e.EmployeeName;This query produces an organizational hierarchy showing each employee and their direct manager.
â ī¸ Common Mistakes
- â Forgetting to use table aliases.
- â Joining on the wrong columns.
- â Using an INNER JOIN when unmatched rows should also be returned.
- â Comparing every row with itself unintentionally.
Warning
â ī¸ Best Practices
Best Practice
đ Key Points to Remember
- đ A SELF JOIN joins a table with itself.
- đ Table aliases are required.
- đ It is commonly used for hierarchical and parent-child relationships.
- đ It can be implemented using INNER JOIN or LEFT JOIN.
- đ It can compare records within the same table.
- đ It is widely used for organizational structures, categories, and relationship analysis.