选择员工人数最多的部门

时间:2015-05-24 17:04:19

标签: mysql

我有两张表EMP(id,name,DEPT_id)DEPT(id ,name)。我需要找到最多员工工作的部门。请帮忙。

11 个答案:

答案 0 :(得分:1)

尝试此查询。

SELECT a.name,Max(a.NumEmp) AS maxEmpCount FROM ( SELECT d.name,COUNT(*) AS NumEmp FROM EMP e INNER JOIN DEPT d  ON e.DEPT_id = d.id GROUP BY e.DEPT_id ) AS a GROUP BY a.name

答案 1 :(得分:0)

您可以尝试此查询。

Select Id, Name from Dept
Where Id = (Select Top(1) DeptId from Emp 
            Group By DeptId
            order by Count(DeptId) desc)

答案 2 :(得分:0)

比其他两个解决方案稍微冗长一点,但它会完成工作......随时调整以方便您。

select countbydept.*
from
(
  -- from EMP table, let's count number of records per dept
  -- and then sort it by count (highest to lowest)
  -- and take just the first value. We just care about the highest
  -- count
  select dept_id, count(*) as counter
  from emp
  group by dept_id
  order by counter desc
  limit 1
) as maxcount

inner join

(
  -- let's repeat the exercise, but this time let's join
  -- EMP and DEPT tables to get a full list of dept and 
  -- employe count
  select
    dept.id,
    dept.`name`,
    count(*) as numberofemployees
  from dept
  inner join emp on emp.dept_id = dept.id
  group by dept.id, dept.`name`
) countbydept 

-- combine the two queries's results by matching the employee count
on countbydept.numberofemployees = maxcount.counter

示例:http://sqlfiddle.com/#!9/7d6a2d/1

答案 3 :(得分:0)

您可以创建视图来查找它。

JOINS

答案 4 :(得分:0)

现在,给出了这两个表的EMP(id,name,DEPT_id)和DEPT(id,name)。现在,我在表格中插入一些条目:

SELECT COUNT(*) AS NO_OF_EMPLOYEES,
       DEPARTMENT.DEPT_NAME
  FROM EMP, DEPARTMENT
 WHERE EMP.DEPT_ID=DEPARTMENT.DEPT_ID
 GROUP BY EMP.DEPT_ID
 ORDER BY NO_OF_EMPLOYEES;

此查询生成以下内容:

NO_OF_EMPLOYEES DEPT_NAME
3               Research
3               Finance
4               Sales
4               Product

现在,提供正确结果的查询:

SELECT COUNT(*) AS MAX_NO_OF_EMPLOYEES,
       DEPARTMENT.DEPT_NAME
  FROM EMP, DEPARTMENT
 WHERE EMP.DEPT_ID=DEPARTMENT.DEPT_ID
 GROUP BY EMP.DEPT_ID
HAVING MAX_NO_OF_EMPLOYEES=(
    SELECT COUNT(*) AS NO_OF_EMPLOYEES
      FROM EMP
     GROUP BY DEPT_ID
     ORDER BY NO_OF_EMPLOYEES DESC
     LIMIT 1
);

它将生成:

MAX_NO_OF_EMPLOYEES    DEPT_NAME
4                      Sales
4                      Product  

答案 5 :(得分:0)

这个问题可以通过多种方式解决

  • 使用子查询

    SELECT name FROM dept WHERE id IN (SELECT dept_id FROM emp HAVING COUNT(dept_id) IN (SELECT MAX(COUNT(dept_id)) FROM emp) GROUP BY dept_id)
    
  • 使用加入

    SELECT name FROM emp e INNER JOIN dept d ON e. dept_id = d. id HAVING COUNT(e.dept_id) IN (SELECT MAX(COUNT(dept_id)) from emp) group by dept_id)
    

答案 6 :(得分:0)

这将给出员工人数最多的部门的部门名称。

Select DEPT_NAME from department where DEPT_ID = (select DEPT_ID from (Select DEPT_ID, count(DEPT_ID) from Employee group by DEPT_ID order by count(DEPT_ID) desc) where rownum = 1);

答案 7 :(得分:0)

您可以使用with这样的语句来解决此问题:

with deps as
 (select dep.department_name as dep_name, count(emp.employee_id) as cnt
    from departments dep
   inner join employees emp
      on emp.department_id = dep.department_id
   group by dep.department_name)
select deps.dep_name,cnt from deps 
where cnt=(select max(cnt) from deps)

OR

select dep.department_name as dep_name, count(emp.employee_id) as cnt
  from departments dep
 inner join employees emp
    on emp.department_id = dep.department_id
 group by dep.department_name
having count(emp.employee_id) >= all (select count(emp.employee_id) as cnt
                                        from departments dep
                                       inner join employees emp
                                          on emp.department_id =
                                             dep.department_id
                                       group by dep.department_name)

OR

with s1 as
 (select dep.department_name as dep_name,
         count(emp.employee_id) over(partition by dep.department_name) as cnt
    from departments dep
   inner join employees emp
      on emp.department_id = dep.department_id
   order by cnt desc),
s2 as
 (select s1.dep_name,
         s1.cnt,
         row_number() over(order by cnt desc) as row_num
    from s1)
select dep_name from s2 where row_num = 1

这些解决方案适用于我们没有top(1)limit 1

的Oracle这样的数据库

答案 8 :(得分:0)

select Top 1 d.DNAME,count(e.ename) as counts from emp e,dept d where d.DEPTNO=e.DEPTNO
group by d.DNAME
order by counts desc

select d.DNAME,count(e.ename) as counts from emp e,dept d where d.DEPTNO=e.DEPTNO
group by d.DNAME
having count(e.ename) = (select max(micount) from (select count(deptno) micount from emp group by DEPTNO) a)

答案 9 :(得分:0)

如果您只有emp表,则下面的查询将为您提供结果-

select a.* from (select deptno, dense_rank() over(order by count(*) desc ) as rank  from dbo.emp group by deptno) a  where a.rank =1

答案 10 :(得分:-1)

SELECT department_id, count(employee_id) as 'No_of_Emp'
    FROM employees
    GROUP BY department_id
    ORDER BY No_of_Emp DESC