收藏
oracle練習(xí)題和答案 筆記2 使用scott/tiger用戶下的emp表和dept表完成下列練習(xí),,表的結(jié)構(gòu)說明如下 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); --最小值<all
select * from emp where sal < all (select sal from emp where deptno=30); ------13.列出在每個部門工作的員工數(shù)量,、平均工資和平均服務(wù)期限。 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.列出所有部門的詳細(xì)信息和部門人數(shù),。
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(經(jīng)理)的最低薪金。 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; +放在沒有匹配行的表一側(cè),所以dept表的記錄完全顯示 --標(biāo)準(zhǔn)等聯(lián)結(jié)
select dept.* from emp join dept on emp.deptno = dept.deptno; --標(biāo)準(zhǔn)的右外聯(lián)結(jié)
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; |
|