Beginner

634. Employees with Same Manager

Use a Self JOIN to compare employees who report to the same manager.

📘 Lesson 634 of 728 ⏱ 14 min 🎯 Intermediate

A Self JOIN can compare rows within the same table based on a shared attribute such as ManagerID.

Comparing employees within the same organizational group is useful for team-level analysis.

Find pairs of employees who report to the same manager.

SQL
SELECT
    e1.EmployeeName AS Employee1,
    e2.EmployeeName AS Employee2,
    e1.ManagerID
FROM Employees e1
INNER JOIN Employees e2
    ON e1.ManagerID = e2.ManagerID
   AND e1.EmployeeID < e2.EmployeeID;

Used in team analysis, organizational reporting, workforce planning, and duplicate relationship analysis.

  • The table is referenced twice.
  • Both employees share the same ManagerID.
  • EmployeeID < EmployeeID prevents duplicate pairs.
  • A row should not be matched with itself.
  • This pattern can compare related records.
How do you find employees with the same manager?

Self JOIN the Employees table on ManagerID and use a condition such as e1.EmployeeID < e2.EmployeeID to avoid duplicate pairs.

Why use EmployeeID < EmployeeID?

It prevents the same employee pair from appearing twice and prevents an employee from matching themselves.

Have you finished this lesson?

Introduction to SQL – All Lessons