oracle查询习题

1.   查询平均工资最高的部门的部门编号、部门名称和该部门的平均工资

select * from
 (select emp.deptno no,dept.dnamename,avg(sal) avgsal
 from emp,dept
 where emp.deptno=dept.deptno
 group by (emp.deptno,dept.dname) orderby avgsal desc) where rownum=1

2.   查询所有员工的年薪、所在部门的名称,查询结果按年薪从低往高排序

select empno,ename,sal,dname
 from emp,dept
 where emp.deptno=dept.deptno
 order by sal

3.   查询每种工作的最低工资,以及领取该工资的员工姓名,查询结果显示工作名称、最低工资、领取该工资的员工姓名

select ename,e.job,e.sal from empe,(select job,min(sal) minsal
 from emp 
 group by deptno,job) t where e.job=t.joband e.sal=t.minsal

4.   查询出管理员工人数最多的人的名字和他管理的人的名字

select t.ename mgr,e.ename fromemp e,
 (select empno,ename from emp e,
  (select mgr,count(mgr) num from empgroup by mgr order by num desc) t
  where e.empno=t.mgr and rownum=1) t
 where e.mgr = t.empno

5.   查询所有员工的编号、姓名,及其上级领导的编号、姓名。显示结果按领导的年薪降序排列

selectt1.empno,t1.ename,t1.mgr,t2.sal mgrsal from emp t1,emp t2
where t1.mgr=t2.empno order by mgrsal desc

6.   查询所有领取奖金和不领取奖金的员工人数、平均工资;查询结果的列名分别为:人数、平均工资;第一行为有奖金的员工,第二行为没有奖金的员工

select count(*)ÈËÊý,avg(sal) ƽ¾ù¹¤×Ê from
(select sal,comm,nvl2(comm,1,0) nvl from emp)
group by nvl order by nvl desc

7.   查询工资不超过2500的人数最多的部门名称和该部门工资不超过2500的员工的员工人数

select * from
 (select count(*) num,emp.deptno,dnamefrom emp,dept
  where emp.deptno=dept.deptno andsal<2500
  group by emp.deptno,dname order by numdesc)
where rownum=1

8.   查询受雇日期早于其直接上级的所有员工的编号,姓名,部门名称

select t.empno,t.ename,d.dnamefrom dept d,
(select t1.empno,t1.ename,t1.deptno from emp t1,emp t2
 where t1.mgr=t2.empno andt2.hiredate>t1.hiredate) t
where d.deptno=t.deptno

9.   查询至少有4个员工的部门的部门名称

select dname from emp,dept whereemp.deptno=dept.deptno
group by dept.deptno,dept.dname having count(*)>=4

10. 查询工资比“SMITH”高的员工的基本信息

select * from emp
where sal>(select sal from emp where ename='SMITH')

11. 查询部门名称中带'S'字符的部门的员工的工资总和部门人数,显示结果为部门名称,部门员工的工资总和,部门人数

selectdname,sum(sal),count(emp.deptno)
 from emp,dept
 where dname like '%S%' andemp.deptno=dept.deptno
 group by dept.dname

12. 查询所有从事"CLERK"工作的雇员所在部门的部门名称、部门里的人数

select dname,count(*) num from empe,dept d
where e.deptno=d.deptno and job='CLERK' group by dname

13. 查询雇员领导的基本信息,要求领导的薪水要超过3000

select * from emp where empno in
(select t1.mgr from emp t1,emp t2
where t1.mgr=t2.empno and t2.sal>3000)

14. 查询在"sales"部门(销售部)工作的员工的姓名

select ename from emp e,dept d
where e.deptno=d.deptno and dname='SALES'

15. 查询工资高于30号部门的所有员工的工资的员工姓名、工资及部门名称

select ename,sal,dname from empe,dept d
where e.deptno=d.deptno
and sal>all(select sal from emp where deptno=30)

16. 查询所有部门的详细信息(部门编号、部门名称)和部门人数

selectd.deptno,d.dname,count(*) num from emp e,dept d
where e.deptno=d.deptno group by d.deptno,d.dname

17. 显示与"BLAKE"同部门的所有员工的基本信息,但不显示"BLAKE"的基本信息

select * from emp
where deptno in (select deptno from emp where ename='BLAKE')
and ename!=(select ename from emp where ename='BLAKE')

18. 查询出“KING”所在部门的部门编号、部门名称以及该部门里的员工人数

select deptno fromemp where ename='KING'
select d.deptno,dname,count(ename) from emp e, dept d where
e.deptno=d.deptno and
d.deptno=(select deptno from emp where ename='KING') group by dname,d.deptno

19. 查询出"WARD"所在部门的工作年限最大的员工的姓名

select ename from emp wherehiredate=(
select max(hiredate) from emp where
deptno=(select deptno from emp where ename='WARD'))
and deptno=(select deptno from emp where ename='WARD')

20. 查询出没有下属的员工的姓名及他的职位

select ename,jobfrom emp where empno not in
(select mgr from emp where mgr is not null)

21. 查询出员工姓名以A开头的人数最多的部门的部门名称

select dnamefrom(select count(ename) a ,dname from emp e,dept t
where e.deptno=t.deptno and ename like 'A%' group by dname order by a desc)
 where rownum=1

22. 查询出SMITH所在部门的部门名称、部门工资的平均值(注意平均值保留两位小数)

select avg(sal),dname from empe,dept t
 where e.deptno=t.deptno and dname=
 (select dname from emp e,dept t wheree.deptno=t.deptno and ename='SMITH' )

 group by dname




猜你喜欢

转载自blog.csdn.net/qq_39404258/article/details/80670528