SQL Interview Questions | Find the manager name
Question
Refer to the following table and answer the questions related to it.
empid | empname | managerid | deptid | Salary |
1 | Emp1 | 0 | 1 | 6000 |
2 | Emp2 | 0 | 5 | 6000 |
3 | Emp3 | 1 | 1 | 2000 |
13 | Emp13 | 2 | 5 | 2000 |
11 | Emp11 | 2 | 1 | 2000 |
9 | Emp9 | 1 | 5 | 3000 |
8 | Emp8 | 3 | 1 | 3500 |
7 | Emp7 | 2 | 5 | NULL |
3 | Emp3 | 1 | 1 | 2000 |
The Employee and manager id are in the same table. Is it possible to get the manager names for the corresponding employee? How will you proceed for the same?
in progress
1
SQL
55 years
3 Answers
1658 views
Great Grand Master 1
Answers ( 3 )
select e.empname, e.empid, m.empname as manager, e.managerid
from emp e, emp m
where e.managerid = m.empid
select e1.empname, e1.empid, e2.empname as manager, from emp e1, emp e2 where e1.managerid = e2.empid
Select e1.empname as Employee ,e2. empname as Manager from employees e1
JOIN employees e2 ON
e1.managerId=e2.empid;
SELECT e.empname, m.empname AS manager_name
FROM employees e
JOIN employees m on e.managerid = m.employeeid;