Beginner

572. Self JOIN for Employees

Use a Self JOIN to represent employee-manager relationships.

📘 Lesson 572 of 728 ⏱ 13 min 🎯 Intermediate

When an Employees table contains a ManagerID that references another EmployeeID in the same table, a Self JOIN can connect each employee to their manager.

Organizational hierarchies are commonly stored using self-referencing foreign keys.

Display employees along with their manager names.

SQL
SELECT
    e.EmployeeName,
    m.EmployeeName AS ManagerName
FROM Employees e
LEFT JOIN Employees m
    ON e.ManagerID = m.EmployeeID;

Used in HR analytics, organizational reporting, employee dashboards, and management structures.

  • The same table is referenced twice.
  • Aliases distinguish employee and manager records.
  • ManagerID points to EmployeeID.
  • LEFT JOIN keeps employees without managers.
  • Self-referencing relationships create hierarchies.
Why are two aliases needed in a Self JOIN?

The aliases represent two different roles of the same table, such as employee and manager.

Why use LEFT JOIN for employee-manager data?

It preserves employees who do not have a manager recorded, such as the top-level manager.

Have you finished this lesson?

Introduction to SQL – All Lessons