1. 程式人生 > >十、Oracle學習筆記:基本查詢語句各子句用法

十、Oracle學習筆記:基本查詢語句各子句用法


一、查詢語句的組成
    select * from tname where 條件 group by colName having 條件 order by colName
    這些子句的先後執行順序:
    1):from 子句:內部從右往左,從後往前執行
    2):where 子句:內部也是從右向左,從後往前執行條件
      (當有多個條件時,如果某一個條件限制的記錄數多,應該寫在最右邊)
    3):group by 子句:多個欄位欄位時,從左往右進行分組
    4):having 子句;從左往右執行,但是比較消耗記憶體,應該儘可能的避免使用
    5):select 子句:選擇欄位進行顯示,萬用字元'*'表示所有欄位,儘量避免使用
    6):order by 子句:查詢語句中最後執行的子句
    --因此根據執行順序,在select中起的別名只能在order by中使用
    

二、基礎查詢語句:DQL

   select colName1,..... from tname;

1.select子句:查詢語句中必不可少的一部分
    select後可以指定要顯示欄位名,可以使用萬用字元'*'來代替表中的所有欄位名 欄位名後可以起列別名,as可以忽略。

(1)去重限制

--關鍵字distinct,去掉重複的資料,重複資料僅顯示一次,(此關鍵字必須緊跟著select關鍵字)
--練習:查詢員工表中部門號,按部門號排序
  select distinct deptno from emp order by deptno;
--練習:檢視員工表中有哪些職位,按照職位長度降序排序
  select distinct job from emp order by length(job);
--練習:檢視員工表中有哪些獎金,按照獎金降序排序
  select distinct comm from emp order by comm;

2.from子句:查詢語句中必不可少的一部分   
    from後用來指定要操作的表物件,表名也可以起別名,直接在表名後新增別名,不能使用'as'。

--練習:查詢員工表中的員工編號,員工姓名,職位,月收入
  select empno 工號,ename 姓名,job 職位,sal+nvl(comm,0) 月收入 from emp;
--練習:查詢員工表中的所有員工資訊
  select * from emp;                
--練習:查詢所有員工資訊以及月收入
  select e.*,sal+nvl(comm,0) from emp e;
  select emp.*,sal+nvl(comm,0) from emp;

3.where子句:用來進行條件的限制,查詢出來的結果是符合限制條件的記錄

(1)符號(=,>,<,>=,<=,!=)限制

--練習:查詢工資為1250的員工資訊
  select * from emp where sal=1250;  

--多個條件時使用 and 或者 or 進行連線
--練習:查詢工資大於1250和工資小於3000;
  select * from emp where sal>1250 and sal<3000;  
--練習:查詢工資大於3000或小於1250的員工資訊
  select * from emp where sal<1250 or sal>3000;
  select * from emp where sal not between 1250 and 3000;    

(2)區間限制

--當條件在某一區間或不在某一區間內時,[not] between p1 and p2,區間為閉區間,且p1一定小於p2 
--練習:查詢工資大於1250和工資小於3000;
  select * from emp where sal between 1250 and 3000; 
--練習:查詢工資大於3000或小於1250的員工資訊
  select * from emp where sal not between 1250 and 3000;     

(3)in、any、all集合限制 

--當條件為某些固定值時可以使用集合,[not] in     用法:欄位名 in (值1,值2,......)   
--練習:查詢工資是1250和3000的資訊
  select * from emp where sal=1250 or sal=3000;
  select * from emp where sal in (1250,3000);
--練習:查詢工資不是1250和3000的資訊
  select * from emp where sal!=1250 and sal!=3000;
  select * from emp where sal not in (1250,3000);
--練習:查詢工資為1250,1500,3050,10000的員工資訊
  select * from emp where sal in(1250,1500,3050,10000);
--練習:查詢工資不為1250,1500,3050,10000的員工資訊
    select * from emp where sal not in(1250,1500,3050,10000);
    
--當all/any與集合連用時
-->all(p1,p2......)大於集合中最大的
--<all(p1,p2......)小於集合中最小的
-->any(p1,p2......)大於集合中最小的
--<any(p1,p2......)小於集合中最大的
--練習:查詢工資比這些工資(1250,2950,3000)都大,都小,任意一個大,任意一個小的員工資訊
  select * from emp where sal>all(1250,2950,3000);
  select * from emp where sal<all(1250,2950,3000);
  select * from emp where sal>any(1250,2950,3000);
  select * from emp where sal<any(1250,2950,3000);

(4)關鍵字 like模糊查詢限制

--萬用字元有兩種:
--%:表示0到多個字元
--_:表示單個字元        
--練習:查詢資訊中第2個字元為L的員工資訊
  select * from emp where ename like '_L%';
--練習:查詢職位中第三個字元為N的員工資訊
  select * from emp where job like '__N%';
--練習:查詢10,20部門中,姓名中有E或L的員工資訊
  select * from emp where deptno in (10,20) and (ename like '%E%' or ename like'%L%') ;
--練習:查詢10,20部門中,姓名中有E和L的員工資訊
  select * from emp where deptno in (10,20) and (ename like '%E%' and ename like'%L%');

(5)表示式或函式限制

--條件中可以使用表示式或者函式
--練習:查詢年薪大於20000的員工資訊
  select * from emp where ((sal+nvl(comm,0))*12)>20000;
--練習:查詢姓名叫jones的員工的職位和月薪
  select job,sal+nvl(comm,0) from emp where upper(ename)='JONES';
--練習:查詢姓名長度大於5的員工資訊
  select * from emp where length(ename)>5;
   

4.order by子句:用來進行資料的顯示排序限制

--排序,也是查詢子句中的一個子句
--位置:一定是放在查詢語句的最後
--用法:order by colName [desc/asc]
--升序:asc,預設情況不寫  降序:desc
--練習:查詢所有員工資訊按照月薪降序排序
  select * from emp order by sal desc;
--練習:查詢10和20部門的員工的姓名,部門號,月薪,獎金,按照獎金升序排序
  select ename,deptno,sal,comm from emp where deptno in (10,20) order by nvl(comm,0);
--排序時應該考慮null,null為無窮大
  
--按照兩個欄位排序:
--邏輯為:先按照第一個欄位排序,當值一致時,才會按照第二個欄位排序每個欄位都有自己的排序規則,兩個欄位之間用逗號分隔開
--格式:order by colName1 desc,colName2 desc
--練習:檢視所有員工資訊,按照工資升序,獎金降序排序
  select * from emp order by sal asc,comm desc; 

5:group by子句,通過欄位進行分組,欄位值一樣的為一組,分組欄位的列,可以寫進select子句中

--格式:group by colName,寫在from 或where 後面
--練習:檢視每個部門的最高月薪,最低月薪,平均月薪,平均獎金,總人數
  select deptno,max(sal),min(sal),avg(sal),avg(nvl(comm,0)),count(*) from emp group by deptno order by deptno;
--練習:檢視每種職位的最高月薪
  select job,max(sal),avg(nvl(comm,0)) from emp group by job;
 
--按照多個欄位進行分組(沒有資料的值會被自動隱藏了) 
--練習:按照部門和職位進行分組,檢視最高月薪,平均獎金
  select deptno,job,max(sal),avg(nvl(comm,0)) from emp group by deptno,job;

6:having子句:分組後再進行過濾作用,寫在group by子句後面,起到條件限制的作用

--練習:檢視部門平均工資大於兩千的部門號
  select deptno from emp group by deptno having avg(nvl(sal,0))>2000;
--練習:檢視部門最高月薪大於3000的部門號
  select deptno from emp group by deptno having max(sal)>3000;