Select max number of employee in any department joining Employee table




To find the maximum number of employees in a department, you can use the following SQL query:



SELECT department_name, COUNT(employee_id) AS num_employees
FROM employee
INNER JOIN department ON employee.department_id = department.department_id
GROUP BY department_name
ORDER BY num_employees DESC
LIMIT 1;



This query will join the employee and department tables on the department_id column and count the number of employees in each department using the COUNT() function. It will then group the results by department and order them in descending order based on the number of employees. The LIMIT the clause will limit the result to only the department with the maximum number of employees.


Assuming you have two tables employee and department with the following data:

employee table:

employee_idemployee_namedepartment_id
1John Doe1
2Jane Smith1
3Mike Johnson2
4Lisa Brown2
5Sarah Lee2
6Tom Davis3

department table:

department_iddepartment_name
1Sales
2Marketing
3IT

To test the query, you can copy and paste it into your database management system's query editor or console and execute it.

SELECT department_name, COUNT(employee_id) AS num_employees FROM employee INNER JOIN department ON employee.department_id = department.department_id GROUP BY department_name ORDER BY num_employees DESC LIMIT 1;


The query should return the department with the maximum number of employees, which in this case is the Marketing department with 3 employees.

Select max number of employee in any department joining Employee table Select max  number of   employee  in any  department  joining Employee table Reviewed by Mukesh Jha on 1:55 AM Rating: 5

No comments:

Add your comment

All Right Reserved To Mukesh Jha.. Theme images by Jason Morrow. Powered by Blogger.