0 votes
1.8k views
in MySQL by
How to get employees with their managers in mysql? Mysql query to show employees with their managers.

1 Answer

0 votes
by
selected by
 
Best answer

Table: `employee`
id    name    salary    seniorId
1    Raju    50000    NULL
2    Pinku    40000    1
3    Sonu    35000    2
4    Rahul    30000    3
5    Suresh    25000    4
6    Dinesh    15000    2
7    Pinku    18000    3
8    Juno    1200000    NULL

Self join Query:
SELECT e1.id,e1.name,e1.salary,e.name  AS manager FROM employee AS e JOIN employee AS e1 ON e.id=e1.seniorId

Result: After execute self join:

id    name    salary    manager
2    Pinku    40000    Raju
3    Sonu    35000    Pinku
4    Rahul    30000    Sonu
5    Suresh    25000    Rahul
6    Dinesh    15000    Pinku
7    Pinku    18000    Sonu

Share:- Whatsapp Facebook Facebook


Welcome to Developerhelpway Q&A, where you can ask questions and receive answers from other members of the community.

Categories

...