orcale查询语句较难.按部门统计员工数,查处员工数最多的部门的第二名到第五名(列出部门名,部门位置)**

1.查询出King所在部门的工作年限最大到的员工名字

select ename from emp where hiredate in(select min(hiredate) from emp where deptno in(select deptno from emp where ename='KING'));

2.查询出工资成本最高的部门的部门号和部门名称

select d.deptno,d.dname,t.sumsal from dept d,(select deptno,sum(sal) sumsal from emp group by deptno  having sum(sal)=(select max(sum(sal)) from emp group by deptno)) t where d.deptno=t.deptno;

3.查询出King所在部门的部门号、部门名称、部门人数

select t.countno,d.dname,d.loc from dept d,(
     select count(*) countno,deptno from emp where deptno in(
           select deptno from emp where ename='KING') group by deptno )t where d.deptno=t.deptno;

4.按部门统计员工数,查处员工数最多的部门的第二名到第五名(列出部门名,部门位置)

select dept.dname,dept.loc from (select rownum nno,deptno from (select count(*) employeesum,deptno from emp group by deptno order by employeesum desc)) e,dept where e.nno between 2 and 5 and e.deptno =dept.deptno;

5.查找不属于任何部门的员工

 select * from emp where deptno is null or deptno not in(select deptno from dept);

6.查找处收入(工资加奖金),下级比自己上级还高的员工编号,员工名字,员工收入

 select e.ename,e.empno,e.sal+nvl(e.comm,0) from emp e,emp m where e.mgr= m.empno and (e.sal+nvl(e.comm,0))>(m.sal+nvl(m.comm,0));

7.查找处部门10和部门20中,工资最高第3名到工资第5名的员工的员工名字,部门名字,部门位置

 select emp.ename,dept.dname,dept.loc from emp,dept,(select rownum nno,new.* from (select * from emp where emp.deptno=10 or deptno=20 order by emp.sal desc)new )e 
 where emp.deptno=dept.deptno and e.nno>=3 and e.nno<=5 and e.empno= emp.empno;

9.得到每个月工资总数最少的那个部门的部门编号,部门名称,部门位置

  select * from dept where deptno=(select e.deptno from (select deptno,sum(sal) from emp group by deptno order by sum(sal)) e where rownum=1);

10.分部门得到平均工资等级为2级(等级表)的部门编号

 select new.dno from salgrade sa,(select deptno as dno,avg(sal) as avgsal from emp group by deptno) new where sa.grade=4 and new.avgsal between sa.losal and sa.hisal;

11.分部门得到工资大于2000的所有员工的平均工资,并且平均工资还要大于2500

 select deptno,avg(sal) from emp where sal>2000 group by deptno having avg(sal)>2500;

你可能感兴趣的:(Oracle)