Enrollments closing soon for Post Graduate Certificate Program in Applied Data Science & AI By IIT Roorkee | 3 Seats Left

  Apply Now

SQL Tutorial

24 / 68

Self-Joins

Self-Join means joining of a table with itself.

In above 'emp' table, column 'mgrempid' stores employee id of manager of each employee and manager is also an employee existing in same 'emp' table.

So, if we want to display employee name followed by employee's manager name in same rows, then self-join has to be used.

select e.empid, concat(e.empfname,' ',e.emplname) "employee", e.mgrempid, concat(m.empfname,' ',m.emplname) "manager"
from emp e, emp m
where e.mgrempid = m.empid
order by 1,2
;

ANSI join syntax:

select e.empid, concat(e.empfname,' ',e.emplname) "employee", e.mgrempid, concat(m.empfname,' ',m.emplname) "manager"
from emp e join emp m
on e.mgrempid = m.empid
order by 1,2
;

All above joins are called Inner joins where there will be output only if columns in WHERE clause have matching data in both tables.
In above output, empid 1 is not displayed because it doesn't have any manager.

select * from emp where empid = 1;

So, how to display record for that employee sans manager detail?


No hints are availble for this assesment

Answer is not availble for this assesment

Loading comments...