SQL Self Join

Table Of Contents:

  1. What Is SQL Self Join?
  2. Syntax Of SQL Self Join.
  3. Examples Of SQL Self Join.

(1) What Is SQL Self Join?

  • When you are joining the same table to itself it’s called Self Join.

(2) Syntax Of SQL Self Join

Syntax:

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

(3) Examples Of SQL Self Join

Customer:

Example-1:

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;

Leave a Reply

Your email address will not be published. Required fields are marked *