I have an employee table, with the following data.
For a particular manager, i want to get the list of all employees, following the hierarchy of the manager.
id name manager
1 John 6
2 Gill 7
3 Ben 2
4 Roy 8
5 Lenin 6
6 Nancy 7
7 Sam 0
8 Dolly 3
For example, i have to get the employees under manager Sam(7). As you can see, Sam does not have any manager, but he is the manager for employees Gill and Nancy, who are the managers for employees Ben and John, Lenin respectively.
So i ran a query like this:
select * from employee where manager=7;
I get the result as 2 rows, Gill and Nancy.
But now, i also want to show the employees Ben and John, Lenin in the output, as they both are under the managers Gill and Nancy, who are under Sam.
How can i structure the query to show the employees hierarchically for a manager? In other words, how can i show all Gill, Nancy, Ben, John and Lenin under the manager Sam ?
Adding a IN subquery to your WHERE clause will handle that. For example: