Given an employee and manager table, write a SQL syntax that could be used to find out an employee's manager's manager, assuming all managers are in the employee table?

Showing Answers 1 - 5 of 5 Answers

praveen1308

  • Jun 12th, 2006
 

select emp_manager from emp where emp_name = (select emp_manager from emp where emp_name ='XYZ');considering we are searching by employee name in case of emp id we can do the same

  Was this answer useful?  Yes

fkhan

  • Jun 13th, 2006
 

it is assumed that u have created a single table for populating data of Employee and Manager (.a Manager is also an Employee so s/he will be residing in the same table) ..

The sample data would be like this

EmployeeID ----- EmployeeName ------- ManagerID

1                        Akhtar                         0

2                        Bilal                              1

3                        Faheem                         2

** This query will return "Akhtar" for given EmployeeID = 3.

SELECT m.EmployeeName FROM #EmpTemp m WHERE m.EmloyeeID = (SELECT e.ManagerID FROM #EmpTemp e WHERE e.EmloyeeID = (Select t.ManagerID
FROM #EmpTemp t WHERE t.EmloyeeID = 3))

** This is solution is workable in case of 3-levels only not even to 2-levels of hierarchy. An n-level solution can be achieve by writing a Recursive stored procedure and that will also be given soon.

  Was this answer useful?  Yes

biswa

  • Jul 11th, 2006
 

 SELECT worker.ENAME || ' works for '
  2  || manager.ENAME
  3  FROM emp worker, emp manager
  4  WHERE worker.mgr = manager.empno ;

where emp is the table

EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO      GRADE                       
---------- ---------- --------- ---------- --------- ---------- ---------- ---------- ----------                       
      7369 SMITH      CLERK           7902 17-DEC-80        800                    20          1                       
      7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30          2                       
      7521 WARD       SALESMAN        7698 22-FEB-81       1250        500         30          2                       
      7566 JONES      MANAGER         7839 02-APR-81       2975                    20          4                       
      7654 MARTIN     SALESMAN        7698 28-SEP-81       1250       1400         30          2                       
      7698 BLAKE      MANAGER         7839 01-MAY-81       2850                    30          4                       
      7782 CLARK      MANAGER         7839 09-JUN-81       2450                    10          4                       
      7788 SCOTT      ANALYST         7566 19-APR-87       3000                    20          3                       
      7839 KING       PRESIDENT            17-NOV-81       5000                    10          5                       
      7844 TURNER     SALESMAN        7698 08-SEP-81       1500          0         30          2                       
      7876 ADAMS      CLERK           7788 23-MAY-87       1100                    20          1                       

     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO      GRADE                       
---------- ---------- --------- ---------- --------- ---------- ---------- ---------- ----------                       
      7900 JAMES      CLERK           7698 03-DEC-81        950                    30          1                       
      7902 FORD       ANALYST         7566 03-DEC-81       3000                    20          3                       
      7934 MILLER     CLERK           7782 23-JAN-82       1300                    10          1                       
      7982 MESSI      MANAGER         7839 28-APR-87       2800        600         40  

output is

WORKER.ENAME||'WORKSFOR'||MANAG                                                                                        
-------------------------------                                                                                        
SCOTT works for JONES                                                                                                  
FORD works for JONES                                                                                                   
ALLEN works for BLAKE                                                                                                  
WARD works for BLAKE                                                                                                   
JAMES works for BLAKE                                                                                                  
TURNER works for BLAKE                                                                                                 
MARTIN works for BLAKE                                                                                                 
MILLER works for CLARK                                                                                                 
ADAMS works for SCOTT                                                                                                  
JONES works for KING                                                                                                   
CLARK works for KING                                                                                                   

WORKER.ENAME||'WORKSFOR'||MANAG                                                                                        
-------------------------------                                                                                        
MESSI works for KING                                                                                                   
BLAKE works for KING                                                                                                   
SMITH works for FORD                                                                                                   


 

  Was this answer useful?  Yes

amy

  • Mar 20th, 2007
 

select e1.name, e2.name, e3.name
from employee e1 left join employee e2 on e1.manager_id = e2.employee_id
left join employee e3 on e2.manager_id = e3.employee_id

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.