What You’ll Learn
In this lesson, you will learn how to use a SQL self join to compare rows stored in the same table. You will apply the technique to employee-manager relationships and to other related records within one table.
- Understand why a table sometimes needs to be joined to itself.
- Use different aliases to represent two roles played by the same table.
- Compare an employee with their manager.
- Find related employees who share the same manager.
- Avoid common self-join problems such as duplicate pairs and accidental Cartesian products.
The Concept
A self join is a regular SQL join in which a table appears more than once in the FROM clause. Each appearance represents a different role or perspective.
For example, an employees table can contain a manager_id column that refers back to another employee’s employee_id:
employee_ididentifies the current employee.manager_ididentifies that employee’s manager.
Because both the employee and the manager are stored in the same table, you can join employees to itself to retrieve the manager’s name.
Aliases are essential in a self join. An alias such as employee identifies one instance of the table, while manager identifies the second instance. The table is the same, but the aliases let SQL distinguish the columns and roles.
Basic Example
The following example creates an employee table and lists each employee together with their direct manager. The chief executive has no manager, so the query uses an INNER JOIN and excludes that row.
CREATE TABLE employees (
employee_id INTEGER PRIMARY KEY,
employee_name VARCHAR(100) NOT NULL,
department VARCHAR(100) NOT NULL,
salary INTEGER NOT NULL,
manager_id INTEGER,
FOREIGN KEY (manager_id) REFERENCES employees(employee_id)
);
INSERT INTO employees (employee_id, employee_name, department, salary, manager_id)
VALUES
(1, 'Alice Morgan', 'Executive', 150000, NULL),
(2, 'Bob Chen', 'Engineering', 95000, 1),
(3, 'Carol Diaz', 'Engineering', 105000, 1),
(4, 'David Singh', 'Engineering', 72000, 2),
(5, 'Emma Wilson', 'Sales', 88000, 1),
(6, 'Frank Moore', 'Sales', 70000, 5);
SELECT
employee.employee_name AS employee_name,
employee.department,
manager.employee_name AS manager_name
FROM employees AS employee
INNER JOIN employees AS manager
ON employee.manager_id = manager.employee_id
ORDER BY employee.employee_id;Expected Output
employee_name | department | manager_name
--------------+-------------+-------------
Bob Chen | Engineering | Alice Morgan
Carol Diaz | Engineering | Alice Morgan
David Singh | Engineering | Bob Chen
Emma Wilson | Sales | Alice Morgan
Frank Moore | Sales | Emma WilsonHow the Code Works
The table is listed twice with two different aliases:
FROM employees AS employee
INNER JOIN employees AS manageremployee represents the person being reported, while manager represents the person they report to. The join condition connects the employee’s foreign key to the manager’s primary key:
ON employee.manager_id = manager.employee_idFor example, David Singh has a manager_id of 2. The self join finds the row whose employee_id is 2, which is Bob Chen.
The SELECT clause uses qualified column names such as employee.employee_name and manager.employee_name. Without the aliases, SQL would not know which instance of employee_name you wanted to return.
An INNER JOIN returns only employees with a matching manager. To include top-level employees such as Alice, use a LEFT JOIN instead:
SELECT
employee.employee_name AS employee_name,
manager.employee_name AS manager_name
FROM employees AS employee
LEFT JOIN employees AS manager
ON employee.manager_id = manager.employee_id
ORDER BY employee.employee_id;With the left join, Alice appears with a NULL manager name because her manager_id is NULL. The choice between INNER JOIN and LEFT JOIN depends on whether rows without a related record should be included.
Another Example
A self join can also find pairs of employees who share the same manager. This can help identify teammates for collaboration, team directories, or workload comparisons.
This query joins the table twice as first_employee and second_employee. Both employees must have the same manager, and the condition comparing their IDs ensures that each pair appears only once.
SELECT
first_employee.employee_name AS employee_one,
second_employee.employee_name AS employee_two,
manager.employee_name AS shared_manager,
first_employee.department
FROM employees AS first_employee
INNER JOIN employees AS second_employee
ON first_employee.manager_id = second_employee.manager_id
AND first_employee.employee_id < second_employee.employee_id
INNER JOIN employees AS manager
ON first_employee.manager_id = manager.employee_id
ORDER BY shared_manager, employee_one;The condition first_employee.employee_id < second_employee.employee_id is important. Without it, the query would return both Bob Chen, Carol Diaz and Carol Diaz, Bob Chen. It would also match an employee with themselves unless the query explicitly prevented that.
This query returns Bob Chen and Carol Diaz as coworkers who share Alice Morgan as their manager. Employees with managers who have only one direct report do not produce a pair.
A direct self join is useful for one-level relationships. If you need to find every person above an employee across multiple management levels, consider using recursive CTEs for hierarchical data.
Common Mistakes
- Using the same alias for both table instances: Each appearance needs a distinct alias, such as
employeeandmanager. - Joining the wrong columns: An employee’s
manager_idshould match the manager’semployee_id. Joining two primary keys usually does not express the relationship you need. - Forgetting about
NULL: A top-level employee may have no manager. Use aLEFT JOINwhen that employee should remain in the result. - Returning duplicate pairs: When finding related pairs, add an ordering condition such as
first_employee.employee_id < second_employee.employee_id. - Leaving out the join condition: A missing or incomplete
ONcondition can create many unrelated combinations. Always describe precisely how the two table instances are related.
Try It Yourself
Using the employees table from the basic example, write a query that returns every employee and their manager’s department. Include employees without managers, and sort the result by the employee’s name.
Use a LEFT JOIN, return the employee name, employee department, and manager department, and give the output columns clear aliases.
Challenge
Write a query that finds employees who earn more than their direct managers.
Your result must:
- Return the employee’s name.
- Return the employee’s salary.
- Return the manager’s name.
- Return the manager’s salary.
- Return the difference between the employee’s salary and the manager’s salary as
salary_difference. - Sort the results from the largest salary difference to the smallest.
Solution
SELECT
employee.employee_name AS employee_name,
employee.salary AS employee_salary,
manager.employee_name AS manager_name,
manager.salary AS manager_salary,
employee.salary - manager.salary AS salary_difference
FROM employees AS employee
INNER JOIN employees AS manager
ON employee.manager_id = manager.employee_id
WHERE employee.salary > manager.salary
ORDER BY salary_difference DESC;The self join gives the query access to both salaries in the same row of the result. The WHERE clause keeps only employees whose salary is greater than their manager’s salary, and the calculated expression shows the difference. The inner join naturally excludes employees without managers because they cannot be compared with a manager.
Key Takeaways
- A self join relates rows within the same table by listing that table more than once.
- Use distinct aliases to represent different roles, such as employee and manager.
- Join a foreign key such as
manager_idto the referenced primary key such asemployee_id. - Use a
LEFT JOINwhen rows without related records should remain in the result. - When finding pairs, use an ordering condition to prevent reversed duplicates and self-matches.



