Fetch All Employees who are also Managers
We need to use SELF JOIN on the table that contains employees to fetch all employees who are also managers from the same table.
- SELF JOIN, which is a join on the same table
Let's assume a table with the below table definition and data, where we need to fetch employees who are also managers.
id name manager_id
===========================
1 Arun 3
2 Kiran 4
3 James 5
4 Sammy
5 Martin 4
SQL Query
SELECT DISTINCT id FROM employees emp
INNER JOIN employees man
ON emp.id = man.id;
This query returns the list of employee IDs who are also managers from the table.
Overall
We now know the SQL Query to fetch all the employees who are also managers from the table.