oracle经典20道笔试题

进修社 人气:1.71W

   1.列出至少有一个员工的所有部门。

oracle经典20道笔试题

select no,e

from dept,emp

where no=no

2.列出薪金比“SMITH”多的所有员工。

select * from emp

where sal >(select sal from emp where ename=’SMITH’)

3.列出所有员工的姓名及其直接上级的姓名。

select e,e

from emp yg,emp sj

where =o

4.列出受雇日期早于其直接上级的所有员工。

select e,e

from emp yg join emp sj on =o

where date

5.列出部门名称和这些部门的`员工信息,同时列出那些没有员工的部门。

select no,e,o,e

from dept left join emp

on no=no

6.列出所有“CLERK”(办事员)的姓名及其部门名称。

select * from dept,emp

where no=no

and job=’CLERK’

7.列出最低薪金大于1500的各种工作。

select job from emp

group by job

having min(sal)>1500

8.列出在部门名称为“SALES”(销售部)工作的员工的姓名,假定不知道销售部的部门编号。

方法一:

select e

from dept,emp

where no=no

and dname=’SALES’

方法二:

select * from emp

where deptno=(select deptno from dept where dname=’SALES’)

9.列出薪金高于公司平均薪金的所有员工。

select * from emp

where sal> (select avg(sal) from emp)

对比:列出薪金高于本部门平均薪金的所有员工。

方法一:

select * from emp a

where >(select avg(sal) from emp b where no=no)

缺点:相关子查询,效率低。

方法二:

select empno,ename,sal from

emp a,(select avg(sal) avg_sal from emp b where no=no) b

where no=no

and >_sal

10.列出与“SCOTT”从事相同工作的所有员工。

select * from emp

where job=(select job from emp where ename=’SCOTT’)

11.列出薪金等于部门30中员工的薪金的所有员工的姓名和薪金。

select ename,sal from emp

where sal=any(select sal from emp where deptno=30 )

12.列出薪金高于在部门30工作的所有员工的薪金的员工姓名和薪金。

方法一:

select ename,sal from emp

where sal>all(select sal from emp where deptno=30 )

方法二:

select ename,sal from emp

where sal>(select max(sal) from emp where deptno=30 )

13.列出在每个部门工作的员工数量、平均工资和平均服务期限。

select deptno,avg(trunc((sysdate-hiredate)/365)) as year

from emp

group by deptno

–参考:截断,取整函数

select trunc(99.9) from dual;

返回

99

14.列出所有员工的姓名、部门名称和工资。

select e,e,

from dept d,emp e

where no=no

说明:每个字段都加表前缀,效率要高些

15.列出所有部门的详细信息和部门人数。

select no,e,count(*)

from dept,emp

where no=no

group by no,e

16.列出各种工作的最低工资。

select job,min(sal) from emp group by job

17.列出各个部门的MANAGER(经理)的最低薪金。

select deptno,min(sal) from emp where job=’MANAGER’ group by deptno

18.列出所有员工的年工资,按年薪从低到高排序。

select empno,ename,sal*12 as 年薪 from emp

order by sal

19. 求各种工作工资最低的员工。

方法一:

select * from emp a

where sal=(select min(sal) from emp b where =)

方法二:

select emp.*

from emp a,( select job,min(sal) min_sal from emp group by job) b

where = and =_sal

20. 列出各种工作工资前3名的员工

select * from (

select empno,ename,sal,job,

dense_rank() over(partition by job order by sal desc) as 名次

from emp ) a

where a.名次<=2

order by job;

说明:用到了Oracle强大的“分区排名技术”,其中“dense_rank()”是Oracle的解析函数。

TAGS:Oracle 笔试