Home > Database > Mysql Tutorial > Oracle查询员工人数最少的部门

Oracle查询员工人数最少的部门

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-07 17:21:48
Original
1590 people have browsed it

--部门表 create table t_dept ( d_id number primary key , d_name varchar2(20) ); create sequence dept_seq

目的:查询员工人数最少的部门

知识点:分组函数,,排序,子查询,rownum

sql语句如下:

分析:

第一步:select count(*) coun,d_id deptId from t_emp group by d_id 查询t_emp表,根据部门d_id分组查询各部门的人数以d_id

第二步:select count(*) coun,d_id deptId from t_emp group by d_id order by coun asc 再orderby coun asc ,这样就按照人数升序排序,这样就第一条就是人数最少的

第三步:子查询,select * from (select count(*) coun,d_id deptId from t_emp group by d_id order by coun asc) where rownum=1 ,因为Oracle不支持select top 1,当要取第一条数据的时候,要用到oracel给查询分配的rownum列,where rownum=1就取到第一条数据了

 

结果如下:

---------------------------------------

counn deptid

---------------------------------------

2          3

linux

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template