MySQL Self Join

MySQL Self Join

MySQL Self Join

A Self Join is a regular join where a table is joined with itself. It is used when there is a need to compare rows within the same table or establish a relationship between rows in a single table.


Syntax

SELECT A.column1, B.column2, ... FROM table_name A JOIN table_name B ON A.common_column = B.common_column;
  • A and B: Aliases for the same table.
  • common_column: The column used to match rows within the same table.

Example Table

Table: employees

+----+----------+-----------+ | id | name | manager_id| +----+----------+-----------+ | 1 | Alice | NULL | | 2 | Bob | 1 | | 3 | Carol | 1 | | 4 | David | 2 | | 5 | Eva | 2 | +----+----------+-----------+
  • Each employee has a manager_id referring to their manager's id.

1. Basic Self Join

List employees and their managers:

SELECT e.name AS employee, m.name AS manager FROM employees e LEFT JOIN employees m ON e.manager_id = m.id;

Result

+----------+----------+ | employee | manager | +----------+----------+ | Alice | NULL | | Bob | Alice | | Carol | Alice | | David | Bob | | Eva | Bob | +----------+----------+

2. Find Employees Without Managers

SELECT e.name AS employee FROM employees e LEFT JOIN employees m ON e.manager_id = m.id WHERE m.id IS NULL;

Result

+----------+ | employee | +----------+ | Alice | +----------+

3. Find Managers with More Than One Employee

SELECT m.name AS manager, COUNT(e.id) AS num_employees FROM employees e JOIN employees m ON e.manager_id = m.id GROUP BY m.name HAVING num_employees > 1;

Result

+----------+---------------+ | manager | num_employees | +----------+---------------+ | Alice | 2 | | Bob | 2 | +----------+---------------+

4. Compare Rows in the Same Table

Find pairs of employees who report to the same manager:

SELECT e1.name AS employee1, e2.name AS employee2, m.name AS manager FROM employees e1 JOIN employees e2 ON e1.manager_id = e2.manager_id AND e1.id < e2.id JOIN employees m ON e1.manager_id = m.id;

Result

+-----------+-----------+----------+ | employee1 | employee2 | manager | +-----------+-----------+----------+ | Bob | Carol | Alice | | David | Eva | Bob | +-----------+-----------+----------+

5. Find Employees in a Management Hierarchy

Find employees and their top-level managers (managers who don't report to anyone):

SELECT e.name AS employee, m1.name AS manager, m2.name AS top_manager FROM employees e LEFT JOIN employees m1 ON e.manager_id = m1.id LEFT JOIN employees m2 ON m1.manager_id = m2.id;

Result

+----------+----------+-------------+ | employee | manager | top_manager | +----------+----------+-------------+ | Alice | NULL | NULL | | Bob | Alice | NULL | | Carol | Alice | NULL | | David | Bob | Alice | | Eva | Bob | Alice | +----------+----------+-------------+

6. Find Employees Managed Indirectly

Find employees and their second-level managers (their manager's manager):

SELECT e.name AS employee, m.name AS manager, mm.name AS second_level_manager FROM employees e LEFT JOIN employees m ON e.manager_id = m.id LEFT JOIN employees mm ON m.manager_id = mm.id;

Result

+----------+----------+---------------------+ | employee | manager | second_level_manager| +----------+----------+---------------------+ | Alice | NULL | NULL | | Bob | Alice | NULL | | Carol | Alice | NULL | | David | Bob | Alice | | Eva | Bob | Alice | +----------+----------+---------------------+

Performance Considerations

  1. Indexing:

    • Use indexes on the columns involved in the join (e.g., manager_id and id) to optimize query performance.
  2. Table Aliases:

    • Always use table aliases to differentiate between instances of the same table.
  3. Hierarchical Queries:

    • For deep hierarchies, consider using recursive queries (if supported) or multiple levels of self joins.

Use Cases

  • Organizational Hierarchies:
    • Track relationships between employees and managers.
  • Bill of Materials (BOM):
    • Represent parent-child relationships in manufacturing.
  • Related Data in the Same Table:
    • Match products, users, or other entities within the same dataset.

Let me know if you need more examples or help with writing queries!

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close