Geek Slack

My SQL Tutorial
    About Lesson


    MySQL Self Join


    MySQL Self Join

    A self join in MySQL is a join operation where a table is joined with itself. It is useful when you need to compare rows within the same table.

    Example:

    SELECT e1.employee_id, e1.employee_name, e2.manager_name
    FROM employees e1
    JOIN employees e2 ON e1.manager_id = e2.employee_id;

    This query retrieves the employee ID, employee name, and manager name by joining the employees table with itself based on the manager ID.

    Self joins are commonly used to create hierarchical relationships or to compare records within the same table.