HOME C C++ PYTHON JAVA HTML CSS JAVASCRIPT BOOTSTRAP JQUERY REACT PHP SQL AJAX JSON DATA SCIENCE AI

SQL Self Join


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

Syntax:

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

T1 and T2 are different table aliases for the same table.

Demo Employee table


This employee table is used for examples:

demo table

SQL Self Join Example


The following SQL statement matches customers that are from the same city:


Example:

SELECT A.Employee_name AS Employee_name1, B.Employee_name AS Employee_name2, A.City
FROM employee A, employee B
WHERE A.Employee_id <> B.Employee_id
AND A.City = B.City
ORDER BY A.City;

self join table