I have two tables in Oracle SQL developer and I am trying to figure out how to display “fname, ssn” and the employees corresponding manager “fname, snn”. Below are the tables that I have created but I cant seem to get the employee information to correspond to their subsequent manager.
Employee Table
x
| EMPLOYEE# | FNAME | LNAME | SNN | DEPARTMENT_NAME | JOB_TITLE |
| 1 | jim | box | 11 | accounting | person |
| 2 | james | boer | 22 | IT | manager |
| 3 | tom | jo | 33 | IT | person |
| 4 | tim | quo | 44 | accounting | manager |
Department Table
| MANAGER_EMPLOYEE# | DEPARTMENT_NAME |
| 2 | IT |
| 4 | accounting |
Desired Output
| EMPLOYEE NAME | Employee SSN | MANAGER NAME | MANAGER SSN |
| jim box | 11 | james boer | 22 |
| tom jo | 33 | tim quo | 44 |
This is what I have so far. It just to show the employee info and manager# as I am not sure how to actually get it to link to a managers name then display as what I want for output
select employee#, fname, lname, employee.department_name, manager_employee# from employee left outer join department on employee.employee# = department.manager_employee#
union
select employee#, fname, lname, employee.department_name, manager_employee# from employee right outer join department on employee.employee# = department.manager_employee#;
Advertisement
Answer
Looks like you need this:
select
emp.employee#,
emp.fname,
emp.lname,
emp.snn,
emp.department_name,
dep.manager_employee#,
man.fname as manager_fname,
man.lname as manager_lname
from
employee emp
left join department dep
on emp.department_name = dep.department_name
left join employee man
on dep.manager_employee# = man.employee#;