What Is self-join In Sql?
What Is self-join In SQL? What is the requirement of self-join?
Can anyone give some appropriate description with an example?
Add comment
Answers (1)
Add AnswerAs the name defines, when the table is joined to itself then it’s called SELF JOIN. it is like each row of the particular table is joined with itself and other rows depend on some conditions. in easy words join between two copies of the same table.
Syntax
SELECT T2.coulmn1 , T2.column2 FROM Table1 T2, Table2 T2 WHERE some_condition(s);
Below is an example.
following is the Employee table and its data.
Following is Self Join Query…
SELECT A.EmpName AS Employee_Name1, B.EmpName AS Employee_Name2, A.City FROM [dbo].[Employee] A, [dbo].[Employee] B WHERE A.EmpId <> B.EmpId AND A.City = B.City ORDER BY A.City;
above is the resulting self-join. where we are joining the same table and put condition.