SELF JOIN Examples - SQL Query

A Self join is used in condition where user needs to create a result set that joins records in a table with other records in the same table.

Consider the following table 'Students'.

Stud_IDStud_NameStud_Location
1JayaINDIA
2AlbertUSA
3AndrewUSA
4RobinUK
5ShrutiINDIA

Write a query to find out which students are from the same location as Student 'Jaya'.

Answer:

SELECT S2.Stud_Name
FROM Students S1, Students S2
WHERE S1.Stud_location = S2.Stud_location
AND S1.Stud_Name='Jaya';


In the above query S1 and S2 are used as Aliases. So this query will find the Students from same location.

Output:

Emp_Name
Jaya
Shruti