列出员工人数最少的部门名称

时间:2017-04-18 15:53:20

标签: sql oracle-sqldeveloper

有两个表,employee和dept:

See this screenshot for column names

如何加入这两个表并选择员工人数最少的部门?

6 个答案:

答案 0 :(得分:0)

如果您提供了表格结构,那么最好回答。

但试一试:

SELECT deptname 
FROM dept 
WHERE deptid = (SELECT distinct deptid 
                FROM employee
                ORDER BY COUNT(dept) limit 1);

答案 1 :(得分:0)

我只是回答,因为MIN不需要而且LIMIT不是Oracle:

select d.*
from (select deptid, count(*) as cnt
      from employees e
      group by deptid
      order by count(*) asc
     ) d
where rownum = 1;

在Oracle 12C +中,您不需要子查询:

      select deptid, count(*) as cnt
      from employees e
      group by deptid
      order by count(*) asc
      fetch first 1 row only;

答案 2 :(得分:0)

请尝试以下方法......

SELECT deptid,
       deptname,
       employeeCount
FROM
(
    SELECT dept.deptid AS deptid,
           dept.deptname,
           COUNT( dept.deptid ) AS employeeCount
    FROM dept
    JOIN employee ON dept.deptid = employee.deptid
    GROUP BY dept.deptid,
             dept.deptname
)
GROUP BY deptid
HAVING employeeCount = MIN( employeeCount );

此语句以在共享字段dept上将employee加入deptid的内部查询开始。然后,它按部门对结果行进行分组,并返回部门的idname的外部查询以及该部门的员工数。

外部查询再次按部门对数据进行分组,然后选择员工人数等于最低雇员人数的部门的详细信息。

如果您有任何问题或意见,请随时发表评论。

答案 3 :(得分:0)

嗨:)所以我的答案有点难看,并且充满了嵌套查询。但我测试了它,它对我有用......

-- First I created a couple of test tables and added a few records 
drop table dept;
drop table employee;
create table dept (deptid number primary key, deptname varchar(20));
create table employee(employee_id number primary key, names varchar(20),
deptid number,foreign key (deptid) references dept(deptid));
insert into dept values(1,'HR');
insert into dept values(2,'Finance');
insert into dept values(3,'IT');
insert into employee values(1,'Tina',1);
insert into employee values(2,'Rob',1);
insert into employee values(3,'Lisa',1);
insert into employee values(4,'Will',2);
insert into employee values(5,'Lina',2);
insert into employee values(6,'Ethel',2);
insert into employee values(7,'Trevor',1);
insert into employee values(8,'Alanea',1);
insert into employee values(9,'Matthew',1);
insert into employee values(10,'Maddie',3);
insert into employee values(11,'Anna',1);
-- According to the added records, the answer we are looking for should be
the department name IT

-- select the department name from department table
select d.deptname from dept d, 
/* This is where it gets ugly - basically, it counts the number of
employees in each department, then finds the id of the department that had
the smallest count */
(select deptid from 
(select count(deptid) as counter, deptid from employee group by deptid) 
where counter =( select min(counter)from 
(select count(deptid) as counter, deptid from employee group by deptid))) minid
-- join the tables using deptid
where d.deptid = minid.deptid;

即使我更改记录以使财务成为正确答案,此查询也为我提供了正确的答案。 如果您有任何问题,请通过评论大喊大叫:)

答案 4 :(得分:0)

select department_name, count(employee_id)
from department d
inner join employee e
on d.employee_id = e.employee_id
having count(employee_id) = 
(
select min(count(employee_id)) /*This query returns minimum count*/
from department d
inner join employee e
on d.employee_id = e.employee_id
group by department_name
)
group by department_name;

答案 5 :(得分:0)

WITH temp AS (SELECT e1.department_id, count(e1.employee_id) emp_count FROM hr.employees e1 GROUP BY e1.department_id) SELECT d1.department_name, t1.emp_count employee_count FROM temp t1 ,hr.departments d1 WHERE t1.department_id = d1.department_id(+) AND NOT EXISTS (SELECT 1 FROM temp t2 WHERE t2.emp_count < t1.emp_count) ORDER BY 2,1 ;