Oracle SQL部分練習題


SQL練習題    
 
 注:查詢列表不建議用 “*”
1.列出至少有一個雇員的所有部門;
a.
select * from dept where deptno in(select distinct deptno from emp);
b.
(oracle11gCBO 新特性 in(多個值)會智能過濾掉重復字段,通過執行計划驗證);
select * from dept where deptno in (select deptno from emp group by deptno having count(deptno)>=1);
c.
select * from dept a where exists (select null from emp b where a.deptno=b.deptno);
 
2.列出薪金比smith多的所有雇員
select ename from emp where sal>(select sal from emp where ename='SMITH');
 
3.列出雇員的姓名及其直接上級姓名(注:列出執行計划原由於顯示結果不同,結果顯示第二個執行計划要優秀一點,第一個執行計划走了全表掃描也符合常理,第二個走的唯一索引,索引>堆表;測試表較小沒有參考價值僅得出結果不同,有利於以后做查詢優化)
a.
select e.ename,p.ename from emp e,emp p where  p.empno(+)=e.mgr;
select p.ename,e.ename from emp p left join emp e on  e.empno=p.mgr;

  

執行計划:
Execution Plan
----------------------------------------------------------
Plan hash value: 2341341676
 
 
---------------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |    14 |   280 |     7  (15)| 00:00:01 |
|*  1 |  HASH JOIN OUTER   |      |    14 |   280 |     7  (15)| 00:00:01 |
|   2 |   TABLE ACCESS FULL| EMP  |    14 |   140 |     3   (0)| 00:00:01 |
|   3 |   TABLE ACCESS FULL| EMP  |    14 |   140 |     3   (0)| 00:00:01 |
---------------------------------------------------------------------------
 
 
Predicate Information (identified by operation id):
---------------------------------------------------
 
 
   1 - access("E"."EMPNO"(+)="P"."MGR")
 
 
 
 
Statistics
----------------------------------------------------------
          1  recursive calls
          0  db block gets
         15  consistent gets
          0  physical reads
          0  redo size
        823  bytes sent via SQL*Net to client
        523  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
         14  rows processed

  

 
 
b.
select e.ename,(select ename from emp p where  p.empno=e.mgr)as BoosName from emp e;

  

執行計划:
Execution Plan
----------------------------------------------------------
Plan hash value: 4000517069
 
 
--------------------------------------------------------------------------------
------
 
 
| Id  | Operation                   | Name   | Rows  | Bytes | Cost (%CPU)| Time
     |
 
 
--------------------------------------------------------------------------------
------
 
 
|   0 | SELECT STATEMENT            |        |    14 |   140 |     3   (0)| 00:0
0:01 |
 
 
|   1 |  TABLE ACCESS BY INDEX ROWID| EMP    |     1 |    10 |     1   (0)| 00:0
0:01 |
 
 
|*  2 |   INDEX UNIQUE SCAN         | PK_EMP |     1 |       |     0   (0)| 00:0
0:01 |
 
 
|   3 |  TABLE ACCESS FULL          | EMP    |    14 |   140 |     3   (0)| 00:0
0:01 |
 
 
--------------------------------------------------------------------------------
------
 
 
 
 
Predicate Information (identified by operation id):
---------------------------------------------------
 
 
   2 - access("P"."EMPNO"=:B1)
 
 
 
 
Statistics
----------------------------------------------------------
          1  recursive calls
          0  db block gets
         17  consistent gets
          0  physical reads
          0  redo size
        849  bytes sent via SQL*Net to client
        523  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
         14  rows processed

  

 
 
 
4.列出入職日期早於其直接上級的所有雇員
a.
select p.ename from emp e,emp p where e.empno=p.mgr  and p.hiredate<e.hiredate;

  

b.
select e.ename from emp e where e.hiredate<(select  hiredate from emp p where p.empno=e.mgr);

  

 
5、列出部門名稱和這些部門的雇員,同時列出那些沒有雇員的部門
a.
select a.dname,b.ename from dept a,emp b where  a.deptno=b.deptno(+);

  

 
b.
select a.dname,b.ename from dept a left join emp b on  a.deptno=b.deptno

  

 
6、列出所有“CLERK”(辦事員)的姓名及其部門名稱
select b.ename,a.dname from dept a,emp b where  a.deptno=b.deptno and b.job='CLERK';

  

 
7、列出各種工作類別的最低薪金,顯示最低薪金大於1500的記錄
select min(sal)as minsal,job from emp group by job having  min(sal)>1500;

  

 
--8、列出從事“SALESMAN”(銷售)工作的雇員的姓名,假定不知道銷售部的部門編號
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 ename from emp where job=(select job from emp  where ename='SCOTT');

  

 
11、列出某些雇員的姓名和薪金,條件是他們的薪金等於部門30中任何一個雇員的薪金
select ename,sal from emp where sal in (select sal from  emp where deptno=30);

  

 
12、列出某些雇員的姓名和薪金,條件是他們的薪金高於部門30中所有雇員的薪金
select ename,sal from emp where sal > (select max(sal)  from emp where deptno=30);

  

 
13、列出每個部門的信息以及該部門中雇員的數量
select a.deptno,a.dname,a.loc,b.ss from dept a,(select  deptno,count(ename) ss from emp group by deptno) b
where a.deptno=b.deptno;

  

 
14、列出所有雇員的雇員名稱、部門名稱和薪金
select a.ename,b.dname,a.sal from emp a,dept b where  a.deptno=b.deptno(+);

  

 
15、列出從事同一種工作但屬於不同部門的雇員的不同組合
select a.ename,b.ename,a.job,b.job,a.deptno,b.deptno from  emp a,emp b
where a.job=b.job and a.deptno<>b.deptno;

  

16、列出分配有雇員數量的所有部門的詳細信息,即使是分配有0個雇員
a.
select a.deptno,a.dname,a.loc,nvl(b.ss,0) from dept a,
(select deptno,count(ename) ss from emp group by deptno)  b where a.deptno=b.deptno(+);

  

b.
select a.deptno,dname,loc,count(empno) ss from dept a,emp  b where a.deptno=b.deptno(+)
group by a.deptno,a.dname,a.loc;

  

 
17、列出各種類別工作的最低工資
select job,min(sal) as minjobsal from emp group by job;

  

 
18、列出各個部門的MANAGER(經理)的最低薪金
select deptno,min(sal) ss from emp where job='MANAGER'  group by deptno;

  

 
19、列出按年薪排序的所有雇員的年薪
select ename,(sal*12+nvl(comm,0)) as nianxin from emp  order by nianxin;

  

20、列出薪金水平處於第四位的雇員
a.
select ename,a.tt from (select rownum  tt,ename,(sal*12+nvl(comm,0)) as nianxin from emp)a where  a.tt=4; 
b.
select * from (select ename,sal,rank()over(order by sal  desc)as nn from emp) where nn=4;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM