Database.Beginner.What is a self-join?

What is a Self-Join in SQL?

✅ A self-join is when:

A table is joined to itself.

  • We treat the same table as if it were two different tables (using aliases).
  • We match rows with other rows in the same table based on a condition.

It’s not a new type of JOIN — it’s just a regular JOIN where the table joins to itself.


Why Use a Self-Join?

  • To compare rows within the same table.
  • To find relationships between rows.
  • To link hierarchical data (e.g., employees and their managers).

Real-World Example

Imagine a table Employees:

employee_idnamemanager_id
1AliceNULL
2Bob1
3Charlie1
4Dave2
  • Each employee has a manager who is also an employee (manager_id points to employee_id).

You want to find each employee and their manager’s name.


Self-Join Example

SELECT 
    e.name AS employee_name,
    m.name AS manager_name
FROM 
    Employees e
LEFT JOIN Employees m ON e.manager_id = m.employee_id;
employee_namemanager_name
AliceNULL
BobAlice
CharlieAlice
DaveBob

✅ We use aliases e (for employee) and m (for manager) to distinguish the table used twice.


How It Works

  • e is the current employee.
  • m is the manager — but from the same table.
  • Join on:
e.manager_id = m.employee_id

→ manager’s employee_id matches employee’s manager_id.

Self-Join Use Cases

Use CaseExample
Employees and managersEmployees table joins to itself.
Finding duplicatesFind rows with the same value in a table.
Finding related recordsE.g., products with the same category, friends-of-friends in a social network.
Comparing rowsE.g., find rows where column A is greater than column B (in the same table).

Visual Diagram

Employees (alias e)         Employees (alias m)
+-------------+             +-------------+
| employee_id |             | employee_id |
| name        |             | name        |
| manager_id  |             | (other columns)
+-------------+             +-------------+

Join: e.manager_id = m.employee_id

In Short

Self-Join = Joining a table to itself using aliases, to find relationships inside the same table.

✅ Same table, treated as two different copies during the query.

This entry was posted in Без рубрики. Bookmark the permalink.

Leave a Reply

Your email address will not be published.