A self join in MySQL joins a table to itself by giving it two different aliases, so each row can be compared with or linked to other rows of the same table. The mysql self join is typically used for hierarchies such as employees and managers, and for comparing rows within one table, such as finding duplicates or pairs. There is no special SELF JOIN keyword: it is an ordinary join where both sides are the same table.
This guide uses employees(id, name, salary, manager_id), where manager_id refers to another row's id.
Some data refers to itself. In an employee table every person has a manager, and the manager is also an employee stored in the same table:
Meera is the top of the tree, so her manager_id is NULL. To show each person next to their manager's name, you need two views of the same table at once.
Aliases are mandatory, because MySQL needs to tell the two copies apart:
Read it as: for each employee e, find the row m whose id equals the employee's manager_id. Meera is missing because her manager_id is NULL and an inner join drops rows without a match.
Use a LEFT JOIN to keep employees that have no manager:
Now Meera appears with "no manager". Choose INNER or LEFT JOIN depending on whether the root rows matter in your report.
A self join can compare rows with a non-equality condition. This finds employees who earn more than their manager:
Another pattern lists pairs of people in the same city without repeating them or pairing a row with itself. The condition a.id < b.id does both:
Without a.id < b.id you would get every pair twice, plus each row paired with itself.
This lists every row whose email appears at least once more in the table. For a simple duplicate count, GROUP BY email HAVING COUNT(*) > 1 is usually shorter and faster; use the self join when you need the full rows.
A self join shows only one level. For a manager chain of any depth, MySQL 8.0 and later supports recursive common table expressions:
The first query is the starting point and the second repeatedly joins the table to the rows found so far. Note that this recursive query itself is a self join in disguise.
Index the column used in the join, here manager_id and the primary key id. Without an index, MySQL scans the table for each row, which grows quadratically on large tables. Check the plan with EXPLAIN.
Sometimes you only want to compare against a subset of the same table. Join the table to a filtered copy by putting the condition in the second alias, or in a derived table:
This lists only employees whose manager earns at least 6000. The derived table m is still the same underlying table, but filtered before the join. The same idea works for finding the previous or next row by date: join a table to itself with b.created_at < a.created_at and pick the latest with MAX. On MySQL 8.0 and later, window functions such as LAG() often replace these self joins and are easier to read, so consider them when you compare each row with its neighbour.
No. You use JOIN or LEFT JOIN with the same table listed twice under different aliases.
You used the same table twice without distinct aliases. Give each occurrence its own alias.
Yes, and it is the usual choice for hierarchies where the top row has no parent.
When the depth is unknown or more than one level, such as a full reporting chain or category tree.
A self join is a normal join of a table with itself using two aliases. Use it for parent-child columns, row comparisons and duplicate checks, add a.id < b.id to avoid mirrored pairs, switch to LEFT JOIN to keep root rows, and use a recursive CTE for deeper hierarchies.