SELF JOIN
A table can be joined to itself
A SELF JOIN is an ordinary join where the same table appears twice under different aliases. It is useful when rows in one table relate to other rows in that same table.
SELECT
e.name AS employee,
m.name AS manager
FROM employees AS e
LEFT JOIN employees AS m ON m.id = e.manager_id;
The alias e represents the employee, while m represents the manager. Although there is only one physical table, the roles must be distinguished in the query.
Aliases are essential for clarity here
Without separate source names, it would be impossible to tell which role an id, name, or manager_id belongs to. That is why a SELF JOIN is best read as two logical copies of the same table.
SELF JOIN is not a separate SQL keyword.
The name describes a technique. The actual join type is still an ordinary one such as INNER or LEFT.
Check yourself
Exercise review
The mentor knows the solution and will help you reach it independently
Sign in to use the Mentor
The Mentor uses the current exercise and your conversation history.
Sign in
Review the key nuances and the answer after making your own attempt.
Sign in to view the solution
The solution and explanation are available after signing in.
Sign in