SQL - Self Join With Example
A table is joined to itself using self join .
It is a join between two copies of the same table.
SELECT selectList FROM table t1 JOIN table t2 WHERE condition
Consider Employee table having below records.
Match Employees who are from the same city
SELECT e2.Employee_FirstName AS FirstName1, e2.Employee_LastName AS LastName1,
e1.Employee_FirstName AS FirstName2,
e1.Employee_LastName AS LastName2, e2.City
FROM Employee e1 JOIN Employee e2 ON e1.EmployeeID <> e2.EmployeeID AND e1.City = e2.City