oracle练习题和答案 笔记2
使用scott/tiger用户下的emp表和dept表完成下列练习,表的结构说明如下
emp员工表(empno员工号/ename员工姓名/job工作/mgr上级编号/hiredate受雇日期/sal薪金/comm佣金/deptno部门编号)
dept部门表(deptno部门编号/dname部门名称/loc地点)
工资 = 薪金 + 佣金
------1.列出至少有一个员工的所有部门
select * from dept
where deptno in
(select deptno from emp group by deptno having count(*)>1);
------2.列出薪金比“SMITH”多的所有员工。
select * from emp
where sal>(select sal from emp where ename='SMITH');
------3.列出所有员工的姓名及其直接上级的姓名。
select ename,(select ename from emp where empno=a.mgr) from emp a;
或
select ename, (select ename from emp where empno=a.mgr) as mgrname from emp a;
自连接
select a.ename,b.ename from emp a,emp b
where a.mgr=b.empno(+);外连接
------4.列出受雇日期晚于其直接上级的所有员工。
select ename from emp a
where hiredate>(select hiredate from emp where empno=a.mgr);
列出受雇日期早于其直接上级的所有员工。
select ename from emp a where
hiredate<(select hiredate from emp where empno=a.mgr);
------5.列出部门名称和这些部门的员工信息,同时列出那些没有员工的部门。
select dname,ename from dept left outer join emp
on dept.deptno=emp.deptno;
select dname,ename from dept a,emp b
where a.deptno = b.deptno(+);
------6.列出所有“CLERK”(办事员)的姓名及其部门名称。
select dname,ename from dept a,emp b
where a.deptno=b.deptno and job='CLERK';
select (select dname from dept where deptno=a.deptno) as dname ,ename
from emp a
where job='CLERK';
------7.列出最低薪金大于1500的各种工作。
select job,min(sal) msal from emp
group by job having min(sal)>1500;
------8.列出在部门“SALES”(销售部)工作的员工的姓名,假定不知道销售部的部门编号。
select ename from emp where deptno=(select deptno from dept where dname='SALES');
------9.列出薪金高于公司平均薪金的所有员工。
select ename from emp where sal>(select avg(sal) from emp);
------10.列出与“SCOTT”从事相同工作的所有员工。
select * from emp where job=(select job from emp where ename='SCOTT');
------11.列出薪金等于部门30中员工的薪金的所有员工的姓名和薪金。
select * from emp where sal in
(select sal from emp where deptno=30);
或
select * from emp where sal = any
(select sal from emp where deptno=30);
------12.列出薪金高于在部门30工作的所有员工的薪金的员工姓名和薪金。
--最大值>all
select * from emp where sal>all
(select sal from emp where deptno=30);
--最小值
(select sal from emp where deptno=30);
------13.列出在每个部门工作的员工数量、平均工资和平均服务期限。
select deptno,count(*),
trunc(avg(sal+nvl(comm,0))) avgsal,
to_char(to_date('0001-01-01','yyyy-mm-dd') + avg(sysdate-hiredate)
-366-31,'yy"年"mm"月"dd') avgday
from emp group by deptno;
------14.列出所有员工的姓名、部门名称和工资。
select ename,dname,sal+nvl(comm,0) from emp,dept where emp.deptno=dept.deptno;
------15.列出从事同一种工作但属于不同部门的员工的一种组合。
select emp.ename,emp.job,emp.deptno from emp ,
(select distinct a.job,a.deptno from emp a,emp b
where (a.job=b.job)and(a.deptno!=b.deptno) order by a.job) c
where empno = (select max(empno) from
emp where job = c.job and deptno = c.deptno)
and emp.job=c.job and emp.deptno= c.deptno
select emp.ename,emp.job,emp.deptno from emp ,
(select distinct a.job,a.deptno from emp a,emp b
where (a.job=b.job)and(a.deptno!=b.deptno) order by a.job) c
where empno = (select min(empno) from
emp where job = c.job and deptno = c.deptno)
and emp.job=c.job and emp.deptno= c.deptno
select * from (select distinct a.* from emp a,emp b
where a.deptno <> b.deptno and
a.job = b.job) c
where empno in (select min(empno)
from (select distinct a.* from emp a,emp b
where a.deptno <> b.deptno and
a.job = b.job) group by deptno,job );
------16.列出所有部门的详细信息和部门人数。
select a.*,(select count(*) from emp where deptno=a.deptno) tot
from dept a ;
------17.列出各种工作的最低工资。
select job,min(sal+nvl(comm,0)) from emp group by job;
这样写有问题
select job,min(nvl(sal+comm,0)) from emp group by job;
------18.列出MANAGER(经理)的最低薪金。
select min(sal) from emp where job='MANAGER' ;
------19.列出所有员工的年工资,按年薪从低到高排序。
select ename,(sal+nvl(comm,0))*12 tot from emp order by tot;
-----orcle的等连接
SELECT d.* FROM EMP E ,DEPT D WHERE E.DEPTNO=D.DEPTNO;
-----orcla的外连接
SELECT d.* FROM EMP E ,DEPT D WHERE E.DEPTNO(+)=D.DEPTNO;
+放在没有匹配行的表一侧,所以dept表的记录完全显示
--标准等联结
select dept.* from emp join dept
on emp.deptno = dept.deptno;
--标准的右外联结
select dept.* from emp right outer join dept
on emp.deptno = dept.deptno;
select dept.* from dept left outer join emp
on emp.deptno = dept.deptno;