SQL with Jupyter Notebook Tutorial

24 / 61

Self-Joins

Self-Join means joining of a table with itself.

We will create a self-join table based on 'emp_<>' table. In the resultant table, the 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.

%%sql
select e.empid, concat(e.empfname,' ',e.emplname) "employee", e.mgrempid, concat(m.empfname,' ',m.emplname) "manager"
from emp_<<your lab username>>  e, emp_<<your lab username>>  m
where e.mgrempid = m.empid
order by 1,2
;

All the 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.

%%sql
select * from emp_<<your lab username>> where empid = 1;

So, how to display record for that employee sans manager detail? We could use Outer Joins, about which we will learn next!


No hints are availble for this assesment

Answer is not availble for this assesment

Loading comments...