A SELF JOIN is a regular join where a table is joined with itself. It's useful for comparing rows within the same table — for example, listing employees with their managers (both rows in the same employees table). When using a SELF JOIN you alias the table to differentiate each instance.
e1 and e2) to reference separate row instances.e1.manager_id = e2.employee_id).
-- Syntax: alias the same table twice and join on the related column
SELECT table1.column, table2.column
FROM table1
INNER JOIN table2
ON table1.common_column = table2.common_column;
-- Example: Get employees and their managers (same table with self join)
SELECT e1.employee_name AS Employee, e2.employee_name AS Manager
FROM employees e1
INNER JOIN employees e2
ON e1.manager_id = e2.employee_id;
-- Example: List products in the same category
(exclude identical products)
SELECT p1.product_name AS Product1, p2.product_name AS Product2
FROM products p1
INNER JOIN products p2
ON p1.category_id = p2.category_id
WHERE p1.product_id != p2.product_id;
e1 refers to the employee row, and e2 refers to the manager row. The join condition pairs each employee with their manager's record.WHERE p1.product_id != p2.product_id ensures you don't pair a product with itself.employee & manager or p1 & p2).employees table.customers table where a customer referred another customer (use a SELF JOIN).