What is Self Join in SQL Server : Interview Question with Example

SQL Self Join : The SELF JOIN is used to join a table to itself as if the table were two tables, temporarily renaming at least one table in the SQL statement.

Example:
Lets first create table
Create Person table for Self Join example

create table tblPerson(P_ID int primary key, P_Fname varchar(20),P_Lnamevarchar(20),
Gender varchar(10),P_Father int,P_Mother int)
insert into tblPerson values('Puran','Singh','M',null,null)




Self Join Example:
select a.P_Fname,b.P_Fname from tblPerson as a
join tblperson as b on a.P_ID=b.P_Father
where a.P_Fname='ram'


Comments

Archive

Contact Form

Send