Find Employees with More Than One Manager Using SQL Server

Find Employees with More Than One Manager Using MS SQL Server Query?


To find employees who report to more than one manager, you can use a combination of SQL aggregation and grouping functions. Assume you have a table named EmployeeManagers with the following columns:

  • EmployeeID: The ID of the employee.
  • ManagerID: The ID of the manager.

Here's a step-by-step SQL query to identify employees with more than one manager:

SQL Query

SELECT EmployeeID
FROM EmployeeManagers
GROUP BY EmployeeID
HAVING COUNT(DISTINCT ManagerID) > 1;

Explanation:

  1. GROUP BY EmployeeID:

    • This groups the rows by EmployeeID, so that you can aggregate data for each employee.
  2. COUNT(DISTINCT ManagerID):

    • This counts the number of distinct managers for each employee.
  3. HAVING COUNT(DISTINCT ManagerID) > 1:

    • The HAVING clause filters the results to include only those employees who have more than one distinct manager.

Example

Consider the following example data in the EmployeeManagers table:

EmployeeIDManagerID
1100
1101
2100
3102
3103
3100

Using the provided query, the result will be:

EmployeeID
1
3

This result indicates that employees with IDs 1 and 3 have more than one manager.

You can run this query in SQL Server Management Studio or any SQL query tool connected to your SQL Server to find employees with more than one manager.


Post a Comment

Previous Post Next Post