How can Find the count of all the managers in the database
with employee and departments table
Employee Database
How can Find the count of all the managers in the database
with employee and departments table
Employee Database
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Tuhin PaulRank #7 · 71.9K reputationPosted Feb 9, 2025, 12:56 PM EST
To find the count of all distinct managers in the database, we need to identify employees who are listed as
MANAGER_IDin either theEMPLOYEEStable or theDEPARTMENTStable. Since a manager is also an employee, we can use theMANAGER_IDcolumn from both tables and join them with theEMPLOYEEStable to get the unique list of managers.We need to:
MANAGER_IDvalues from theDEPARTMENTStable.MANAGER_IDvalues from theEMPLOYEEStable.MANAGER_IDvalues using aUNIONto ensure uniqueness.MANAGER_IDvalues with theEMPLOYEEStable to retrieve the corresponding employee details.SELECT COUNT(DISTINCT e.EMPLOYEE_ID) AS Manager_Count FROM ( -- Get managers from DEPARTMENTS table SELECT MANAGER_ID FROM DEPARTMENTS WHERE MANAGER_ID IS NOT NULL UNION -- Get managers from EMPLOYEES table SELECT MANAGER_ID FROM EMPLOYEES WHERE MANAGER_ID IS NOT NULL ) AS Managers JOIN EMPLOYEES e ON Managers.MANAGER_ID = e.EMPLOYEE_ID;0.MANAGER_IDvalues are properly populated in bothDEPARTMENTSandEMPLOYEEStables.Muhammad Imran AnsariRank #326 · 7.6K reputationPosted Feb 7, 2025, 6:43 AM EST
To find the count of all the managers in a database with Employee and Departments tables, you typically need to identify which employees are managers. This is usually done by checking if an employee's ID appears in a manager-related field in the Departments table or if there is a specific column in the Employee table that indicates whether an employee is a manager.
Assumptions:
Employee Table: Contains employee details, including employee_id, name, etc.
Departments Table: Contains department details, including department_id, department_name, and manager_id (which references the employee_id of the manager).
You can count the number of unique managers by querying the
Departmentstable and counting the distinctmanager_idvalues.SELECT COUNT(DISTINCT manager_id) AS manager_count FROM Departments;Alternative Approach:
If the Employee table has a column that indicates whether an employee is a manager (e.g., is_manager), you can directly count the number of employees where is_manager is true.
SELECT COUNT(*) AS manager_count FROM Employee WHERE is_manager = TRUE;Dashrath HapaniRank #317 · 7.9K reputationPosted Feb 7, 2025, 7:11 AM EST
SELECT COUNT(DISTINCT e.manager_id) AS manager_count FROM Employee e inner join manager m on m.manager_id = e.manager_id WHERE manager_id IS NOT NULL and e.isactive =1;Consider two table manager and employee where manageid is FK in emaployee table.
Kiran KumarRank #1,268 · 1.4K reputationPosted Feb 8, 2025, 3:23 AM EST
We dont have managers table the employee table and departments we need to get joining it
But here a Mnager also a employee so how can we get it
Attached structure diagram
Kiran
Muhammad Imran AnsariRank #326 · 7.6K reputationPosted Feb 8, 2025, 4:09 AM EST
As the structure diagram,
manager_idcolumn exists in theEmployeestable (indicating that each employee has a manager, except for top-level managers who haveNULL), you can find the total number of unique managers in the database using theCOUNT(DISTINCT manager_id)function.-- SQL Query to Find the Total Count of Managers: SELECT COUNT(DISTINCT manager_id) AS TotalManagers FROM Employees WHERE manager_id IS NOT NULL;Kiran KumarRank #1,268 · 1.4K reputationPosted Feb 9, 2025, 12:43 AM EST
yes unique records can be found in employee table but need to find the distinct managers from employee and departments tables
so do I need to join the tables of both and filter based on departmentId or managerid matching
Muhammad Imran AnsariRank #326 · 7.6K reputationPosted Feb 9, 2025, 11:46 AM EST
suppose you need to find distinct managers from both the Employees and Departments tables. In that case, you should combine the
manager_idfrom the Employees table and themanager_idfrom the Departments table then useUNIONto merge distinctmanager_idvalues from both tables.Here is the query:SELECT COUNT(DISTINCT manager_id) AS TotalManagers FROM ( SELECT manager_id FROM Employees WHERE manager_id IS NOT NULL UNION SELECT manager_id FROM Departments WHERE manager_id IS NOT NULL ) AS CombinedManagers;This query gives the total count of unique managers across both Employees and Departments tables.