MySQL Self Join

Joining a table to itself.

MySQL Self Join

A self join is a regular join, but the table is joined with itself.

This is useful for hierarchical data or comparing rows within the same table.

SELECT column_name(s)
FROM table1 T1, table1 T2
WHERE condition;

Example

SELECT A.CustomerName AS CustomerName1, B.CustomerName AS CustomerName2, A.City FROM Customers A, Customers B WHERE A.CustomerID <> B.CustomerID AND A.City = B.City ORDER BY A.City;