sql查询练习题含答案

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

--(1)查询20号部门的所有员工信息。

select * from emp e where =20;

--(2)查询奖金(COMM)高于工资(SAL)的员工信息。

select * from emp where comm>sal;

--(3)查询奖金高于工资的20%的员工信息。

select * from emp where comm>sal*;

--(4)查询10号部门中工种为MANAGER和20号部门中工种为CLERK的员工的信息。select * from emp e

where =10 and ='MANAGER')

or =20 and ='CLERK')

--(5)查询所有工种不是MANAGER和CLERK,

--且工资大于或等于2000的员工的详细信息。

select * from emp

where job not in('MANAGER','CLERK') and sal>=2000;

--(6)查询有奖金的员工的不同工种。

select * from emp where comm is not null;

--(7)查询所有员工工资和奖金的和。

select +nvl,0)) from emp e;

--(8)查询没有奖金或奖金低于100的员工信息。

select * from emp where comm is null or comm<100;

--(9)查询员工工龄大于或等于10年的员工信息。

select * from emp where (sysdate-hiredate)/365>=10;

--(10)查询员工信息,要求以首字母大写的方式显示所有员工的姓名。

select initcap(ename) from emp;

select upper(substr(ename,1,1))||lower(substr(ename,2)) from emp;

--(11)显示所有员工的姓名、入职的年份和月份,按入职日期所在的月份排序,

--若月份相同则按入职的年份排序。

select ename,to_char(hiredate,'yyyy') year,to_char(hiredate,'MM') month from emp

order by month,year;

--(12)查询在2月份入职的所有员工信息。

select * from emp where to_char(hiredate,'MM')='02'

--(13)查询所有员工入职以来的工作期限,用“**年**月**日”的形式表示。

select ,floor(/365)||'年'

||floor(mod(,365)/30)||'月'

||floor(mod(mod(,365),30))||'日'

from emp e;

--(14)查询从事同一种工作但不属于同一部门的员工信息。

select ,,,,,

from emp a,emp b

where = and <>;

--(15)查询各个部门的详细信息以及部门人数、部门平均工资。

select ,count,avg,,

from emp e ,dept d

where =

group by ,,

--(16)查询10号部门员工以及领导的信息。

select * from emp where empno in(

select mgr from emp where deptno=10) or deptno=10;

--(17)查询工资为某个部门平均工资的员工信息。

select * from emp

where sal in(select avg(sal) from emp group by deptno);

--(18)查询工资高于本部门平均工资的员工的信息。

select * from emp e1

where sal >(select avg(sal) from emp e2 where =;

--(19)查询工资高于本部门平均工资的员工的信息及其部门的平均工资。select e.*,

from emp e,

(select deptno,avg(sal) as avgsal from emp group by deptno) a where = and >;

--(20)统计各个工种的人数与平均工资。

select count(*),,avg from emp e

group by

--(21)统计每个部门中各个工种的人数与平均工资。

select deptno,job,count(empno),avg(sal) from emp e

group by ,

--(22)查询所有员工工资都大于1000的部门的信息。

select * from dept where deptno in

(select deptno from emp

where deptno not in

(select distinct deptno from emp where sal<1000));

--(23)查询所有员工工资都大于1000的部门的信息及其员工信息。select * from emp e join dept d

on

in (select deptno from emp

where deptno not in

(select distinct deptno from emp where sal<1000))

and =;

--(24)查询所有员工工资都在900~3000之间的部门的信息。

select * from dept

where deptno not in(

select deptno from emp

where sal not between 900 and 3000);

--(25)查询所有工资都在900~3000之间的员工所在部门的员工信息。

select * from emp a

where in

(select distinct from emp e

where between 900 and 3000);

--(26)查询每个员工的领导所在部门的信息。

select d.* from dept d

where in

(select distinct from emp e1,emp e2

where =;

--(27)查询人数最多的部门信息。

select * from dept

where deptno in

(select deptno from (select count(*) count,deptno from emp group by deptno) where count in(select max(count)

from (select count(*) count ,deptno from emp group by deptno)));

--(28)查询30号部门中工资排序前3名的员工信息。

select * from

(select sal from emp where deptno=30 order by sal desc) e

where rownum<4

--(29)查询'JONES'员工及所有其直接、间接下属员工的信息。

select e.* from emp e

start with ename='JONES'

connect by prior empno=mgr;

---(30)查询SCOTT员工及其直接、间接上级员工的信息。

select e.* from emp e

start with ename='SCOTT'

connect by prior mgr=empno;

相关文档
最新文档