Select max number of employee in any department joining Employee table
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_id | employee_name | department_id |
---|---|---|
1 | John Doe | 1 |
2 | Jane Smith | 1 |
3 | Mike Johnson | 2 |
4 | Lisa Brown | 2 |
5 | Sarah Lee | 2 |
6 | Tom Davis | 3 |
department table:
department_id | department_name |
---|---|
1 | Sales |
2 | Marketing |
3 | IT |
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
Reviewed by Mukesh Jha
on
1:55 AM
Rating:
No comments:
Add your comment