SQL Queries to Identify Employees Working Under Multiple Managers

Have you ever wondered how many employees have worked under different managers? This kind of data can reveal a lot about how people move within a company and how management changes over time. In this blog post, we’ll show you how to write a simple SQL query to find all employees who have worked under more than one manager. Find top 50 SQL Questions here

1. Understanding the Problem

Imagine you have a table called Employee_Manager with these columns:

  • employee_id: A unique number for each employee.
  • manager_id: A unique number for each manager.
  • department: The department where the employee works.
  • person_name: The employee’s name.

Our task is to find employees who have been linked to more than one manager.

2. Creating the Employee_Manager Table

First, let’s create the Employee_Manager table. This table will store details about which managers each employee has worked under.

-- Create the table
CREATE TABLE Employee_Manager (
    employee_id INT,
    manager_id INT,
    department VARCHAR(50),
    person_name VARCHAR(100)
);

3. Inserting Sample Data

To demonstrate our queries, we’ll insert sample data into the Employee_Manager table.

INSERT INTO Employee_Manager (employee_id, manager_id, department, person_name) VALUES
(1, 101, 'Sales', 'Shivan'),
(1, 102, 'Sales', 'Shivan'),
(2, 101, 'Sales', 'Shubham'),
(3, 103, 'Marketing', 'Kumar'),
(4, 104, 'IT', 'Preeti'),
(4, 105, 'IT', 'David'),
(5, 106, 'HR', 'Preeti'),
(6, 107, 'Finance', 'Sanju'),
(7, 108, 'Operations', 'Mukesh'),
(7, 109, 'Operations', 'Mukesh'),
(8, 110, 'Sales', 'Rohit'),
(9, 111, 'Marketing', 'Anjali'),
(9, 112, 'Marketing', 'Anjali'),
(10, 113, 'IT', 'Ravi'),
(11, 114, 'Finance', 'Nisha'),
(12, 115, 'HR', 'Ritu'),
(12, 116, 'HR', 'Ritu'),
(13, 117, 'Operations', 'Amit'),
(14, 118, 'Sales', 'Suman'),
(14, 119, 'Sales', 'Suman'),
(15, 120, 'Marketing', 'Raj'),
(16, 121, 'IT', 'Kavita'),
(17, 122, 'Finance', 'Deepak'),
(17, 123, 'Finance', 'Deepak'),
(18, 124, 'HR', 'Neha'),
(19, 125, 'Operations', 'Vikram'),
(20, 126, 'Sales', 'Alok'),
(21, 127, 'Marketing', 'Meena'),
(22, 128, 'IT', 'Sameer'),
(23, 129, 'Finance', 'Nitin'),
(24, 130, 'HR', 'Seema'),
(24, 131, 'HR', 'Seema'),
(25, 132, 'Operations', 'Tarun'),
(26, 133, 'Sales', 'Monica'),
(27, 134, 'Marketing', 'Rahul'),
(28, 135, 'IT', 'Pooja'),
(28, 136, 'IT', 'Pooja'),
(29, 137, 'Finance', 'Manoj'),
(30, 138, 'Operations', 'Tina');

4. Solution 1: Using GROUP BY and HAVING

The GROUP BY and HAVING clauses help us group data and filter out groups based on specific conditions.

SELECT employee_id, person_name, department
FROM Employee_Manager
GROUP BY employee_id, person_name, department
HAVING COUNT(DISTINCT manager_id) > 1;

Detailed Explanation:

  • GROUP BY: Groups records by employee_id,person_name, and departmentso we can analyze each employee separately.
  • HAVING COUNT(DISTINCT manager_id) > 1: Filters to show only employees who have worked under more than one manager. The DISTINCTkeyword ensures we only count unique managers.

5. Solution 2: Using a Self Join

A self-join compares rows within the same table to find matches.

SELECT DISTINCT e1.employee_id, e1.person_name, e1.department
FROM Employee_Manager e1
JOIN Employee_Manager e2 ON e1.employee_id = e2.employee_id
WHERE e1.manager_id <> e2.manager_id;

Detailed Explanation:

  • Self-Join: The query joins the Employee_Manager table with itself to find pairs of records for the same employee_id.
  • Different Managers: The WHERE clause filters these pairs to include only those where manager_iddiffers, identifying employees with multiple managers.
  • Distinct Results: The DISTINCTkeyword ensures each employee appears only once in the final result set.
  • Output: The query selects employee_id, person_name, and departmentfor employees who have worked under more than one manager.

6. Solution 3: Using a Subquery

A subquery calculates the number of managers for each employee separately.

SELECT employee_id, person_name, department
FROM (
    SELECT employee_id, person_name, department, COUNT(DISTINCT manager_id) AS manager_count
    FROM Employee_Manager
    GROUP BY employee_id, person_name, department
) AS subquery
WHERE manager_count > 1;

Detailed Explanation:

  • The inner query (SELECT employee_id, person_name, department, COUNT(DISTINCT manager_id) AS manager_count FROM Employee_Manager GROUP BY employee_id, person_name, department) calculates the number of unique managers each employee has worked under by grouping the data.
  • The subquery generates a temporary result set that includes each employee’s ID, name, department, and the count of distinct managers they have worked with (manager_count).
  • The outer query (SELECT employee_id, person_name, department FROM (...) AS subquery WHERE manager_count > 1) filters this result set to select only those employees who have a manager_count greater than 1, indicating they have worked under more than one manager.
  • The final result retrieves the employee_id, person_name, and department for all employees who have been associated with multiple managers.

7. Conclusion

Finding employees who have worked under multiple managers can reveal important insights about organization. By using these SQL techniques, you can easily analyze employee-manager relationships. Whether you’re preparing for a job interview or working on a data project, knowing these methods will help you explore and understand the data better. Another important interview question is: How to Find the 2nd and 3rd Highest Salaries Using SQL.

If you have any questions or need further assistance, feel free to leave a comment below!

Kaggle Master & Senior Data Scientist ( Ambitious, Adventurous, Attentive)

Leave a Reply

Your email address will not be published. Required fields are marked *

Share This Post
Latest Post
7 Powerful Steps to Build Successful Data Analytics Project
Master Real-Time Image Resizing with Python: A 3-Step Guide
Mastering SQL for Average Product Ratings: Amazon Interview Question
SQL Query to find Top 3 Departments with the Highest Average Salary
Master SQL Today: 10 Powerful Hands-On Beginner’s Guide
Linear Regression: A Comprehensive Guide with 7 Key Insights
Meta LLAMA 3.1 – The Most Capable Large Language Model
Understanding Dispersion of Data: 4 Key Metrics Explained
Find Employees Who Joined on Same Date & Month: 3 Powerful SQL Methods
Ultimate Guide to Mastering Prompt Engineering Techniques – Part 2

Leave a Reply

Your email address will not be published. Required fields are marked *