18 August 2016

Self-join queries

Self-Join is a type of join which is used to join the same table by creating the second instance of the same table. So we join 2 instances of the same table in case of self-join. This type of join is used when there is the requirement to get the referenced data which is available in the same table.
e.g. A table contains EmpId, Ename and ManagerId
As the manager id is also an employee id. Now if we want that who is the manager of which employee. In this situation, we need to create the instance of the same table and get the required data as:

SELECT EMPID, ENAME, ENAME AS [MANAGER NAME]
FROM EMP E1, EMP E2
WHERE E1.EMPID= E2.MANAGERID

No comments: