Beginner

571. Self JOIN

Learn how a table can be joined with itself to compare related rows within the same table.

📘 Lesson 571 of 728 ⏱ 13 min 🎯 Intermediate

A Self JOIN occurs when a table is joined to itself using different aliases. It is useful when rows in the same table have relationships with other rows in that table.

Many business datasets contain hierarchical or relational information inside a single table.

Find each employee and the name of their manager when both employees and managers are stored in the same Employees table.

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

Used for employee hierarchies, organizational structures, category relationships, and referral systems.

  • A Self JOIN joins a table to itself.
  • Different aliases are required.
  • It is useful for hierarchical data.
  • The JOIN condition connects related rows.
  • LEFT JOIN can preserve top-level records.
What is a Self JOIN?

A Self JOIN joins a table with itself using different aliases to compare or relate rows within the same table.

Where is Self JOIN commonly used?

It is commonly used for employee-manager hierarchies, parent-child relationships, and comparisons within the same table.

Have you finished this lesson?

Introduction to SQL – All Lessons