Oracle學習筆記(七)——Oracle表的複雜查詢

一、數據分組
1、數據分組——max,min,avg,sum,count
  • 顯示所有員工中的最高工資和最低工資
    • select max(sal),min(sal) from emp;
  • 顯示所有員工的平均工資和工資總和
    • select avg(sal),sum(sal) from emp;
  • 計算員工數量
    • select count(sal) from emp;
  • 顯示工資最高的員工的名字和工作崗位
    • select ename,job,sal from emp where sal=(select max(sal) from emp);
  • 顯示工資高於平均工資的員工信息
    • select ename,sal from emp where sal>(select avg(sal) from emp);

2、group by和having子句
  • group by用於對查詢的結果分組統計。
  • having子句用戶限制分組顯示結果。
  • 顯示每個部門的平均工資和最高工資
    • select avg(sal),max(sal),deptno from emp group by deptno;
  • 顯示每個部門的每種崗位的平均工資和最低工資
    • select avg(sal),min(sal),deptno,job from emp group by deptno,job;
  • 顯示平均工資低於2000的部門號和它的平均工資
    • select deptno,avg(sal) from emp group by deptno having avg(sal)<2000;

3、對數據分組的總結
  • 分組函數只能出現在選擇列表、having、order by子句中
  • 如果在select語句中同時包含group by、having、order by,那麼他們的順序是group by、having、order by。
  • 在選擇列中,如果有列、表達式和分組函數,那麼這些列和表達式必須有一個出現在group by子句中,否則就會出錯。
    • select deptno,avg(sal) from emp group by deptno having avg(sal)<2000;這裏deptno就一定要出現在group by中。


二、多表查詢
1、多表查詢:基於兩個和兩個以上的表或是視圖的查詢。
2、示例
  • 顯示僱員名、僱員工資及所在部門的名字【笛卡爾集】
    • 注:多表查詢的條件是,條件不能少於  (表的個數-1)
    • select ename,sal,dname from emp,dept where emp.deptno=dept.deptno; 
  • 顯示部門號爲10的部門名、員工名和工資
    • select  dname ,ename,sal from dept,emp where dept.deptno=emp.deptno and dept.deptno=10; 
  • 顯示各個員工的姓名、工資、及其工資的級別
    • select  ename,sal,grade from emp,salgrade  where sal between losal and hisal ;
  • 顯示僱員名、僱員工資及所在部門的名字,並按部門排序
    • select  ename,sal,dname from emp,dept where emp.deptno=dept.deptno order by emp.deptno ;

3、自連接:在同一張表的連接查詢。
  • 顯示某個員工的上級領導的姓名
    • select worker.ename,boss.ename from emp worker,emp boss where worker.mgr=boss.empno and worker.ename= 'FORD';


三、子查詢
1、子查詢:嵌入在其它sql語句中的select語句,也叫嵌套查詢。
2、單行子查詢:只返回一行數據的子查詢語句。
  • 顯示與SMITH同一部門的所有員工
    • select ename,deptno from emp wheredeptno=(select deptno from emp where ename='SMITH');

3、多行子查詢:返回多行數據的子查詢。
  • 查詢和部門10的工作相同的僱員的名字、崗位、工資、部門號
    • select ename,job,sal,deptno from emp where job in (select distinct job from emp where deptno=10);

4、在多行子查詢中使用all操作符
  • 顯示工資比部門30的所有員工的工資高的員工的姓名、工資和部門號
    • select ename,sal,deptno from emp where sal>all (select sal from emp where deptno=30);
    • select ename,sal,deptno from emp where sal>(select max(sal) from emp where deptno=30);

5、在多行子查詢中使用any操作符
  • 顯示工資比部門30的所有員工的工資高的員工的姓名、工資和部門號
    • select ename,sal,deptno from emp where sal>any (select sal from emp where deptno=30);

6、多列子查詢
  • 顯示與SMITH的部門和崗位完全相同的所有員工
    • select * from emp where (deptno,job)=(select deptno,job from emp where ename='SMITH');
    • 注:字段順序應該一樣,否則無法匹配

7、在from子句中使用子查詢
  • 顯示高於自己部門平均工資的員工的信息
    • 查詢各個部門的平均工資和部門號:select deptno,avg(sal) mysal from emp group by deptno;
    • 把以上的查詢結果看作是一張子表:select * from emp a1,(select deptno,avg(sal) mysal from emp group by deptno) a2 where a1.deptno=a2.deptno and a1.sal>a2.avg(sal);
  • 說明:在from 子句中使用子查詢時,該子查詢會被作爲一個視圖來對待,因此叫做內嵌視圖。當在from子句中使用子查詢時,必須給子查詢指定別名
  • 給表取別名,不能加as;給列取別名,可以加as。

8、用查詢結果創建新表
  • create table mytable(id,name,sal job,deptno) as select empno,ename,sal job,deptno from emp;


四、分頁查詢
1、oracle分頁一共有三種方式:
1)rownum分頁,只能採用二分的方法,一次只能截取一次。
  • rownum表示oracle自動爲表分配的行號,隨着行數變化而變化。
  • 子查詢:select * from emp
  • 顯示rownum:select a1.*,rownum rn from (select * from emp) a1;
  • 選取特定行:select a1.*,rownum rn from (select * from emp) a1 where rownum<=10 ;
  • select a2.*,rownum rn from(select a1.*,rownum rn from (select * from emp) a1 where rownum<=10 ) a2 where rn>=6 ;
  • 注:如果查詢指定咧,則只需要修改最內層的子查詢的顯示字段。
    • 改動如下:select ename,sal from emp
  • 注:排序如下:
    • select a2.*,rownum from(select a1.*,rownum rn from (select * from emp order by sal) a1 where rownum<=10 ) a2 where rn>=6 ;
  • select * from (select t.*,rownum rn from (select * from t_xiaoxi order by cid desc) t where rownum<10000) where rn>9980;
  • 執行時間爲0.1秒。

2、根據rowid來分
  • select * from t_xiaoxi where rowid in (select rid from (select rownum rn,rid from (select rowid rid,cid from t_xiaoxi order by cid desc) where rownum<10000) where rn>9980) order by cid desc;
  • 執行時間爲0.03秒

3、按分析函數來分
  • select * from (select t.*,row_number() over (order by cid desc) rk from t_xiaoxi t) where rk<10000 and rk>9980;
  • 執行時間1.01秒

4、按效率:ROWID>ROWNUMBER>分析函數


五、合併查詢
1、使用集合操作符號union, union all, intersect, minus
2、union:用於取得兩個結果集的並集。當使用該操作符時,自動去掉結果集中重複行。
  • select ename, sal, job from emp where sal>2500 union select ename, sal, job from emp where job='manager';

3、union all:與union相似,但不會取消重複航,而且不會排序。

4、intersect:用於取兩個結果集的交集。

5、minus:用於取得兩個結果級的差集,它只會顯示存在第一個集合中,而不存在第二個集合中的數據。


六、創建新的數據庫
1、創建數據庫有兩種方法:
1)通過oracle提供的嚮導工具
2)手工步驟直接創建

2、DBCA(數據庫配置助手)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章