oracle子查詢

最近在加強oracle查詢,在網上看到了一個不錯的視頻,把學習筆記和大家分享一下


oracle 子查詢的語法(即select語句的嵌套)

子查詢要注意的問題:
  1.子查詢語法中的小括號
  2.子查詢的書寫風格
  3.可以使用子查詢的位置:where ,select ,having,from
  4.不可以在主查詢的group by 使用
  5.from的子查詢
  6.主查詢和子查詢可以不是同一張表
  7.一般不在子查詢中使用排序,因爲對主查詢沒有意義,但在top-N分析順序,要排序
  8.執行順序:先執行子查詢,再執行主查詢,但相關查詢例外
  9.單行子查詢只能使用單行子查詢,多行子查詢使用多行子查詢(查詢結果多行)
  10.子查詢null問題


---------------------------------------------------------------------------------------------

(1).可以使用子查詢的位置:where ,select ,having,from

  1. select(在select語句後面的查詢必需是單行子查詢,即結果返回爲1條)
       
     SELECT EMPNO,ENAME,SAL,(SELECT JOB FROM EMP where empno=7839) from emp

    2.having(查詢平均薪水大於30號部門最高薪水的部門平均薪水
    select deptno,avg(sal)
         from emp
         group by deptno
         having avg(sal) >(select max(sal)
                   from emp
                           where deptno=30)

    3.from--後面是放一張表,或結果集(一條查詢語句),在from的子查詢可以看成一張新的表
       select *
       from (select empno,ename,sal,sal*12 from emp)


    4.where

        查詢工資比scott的員工

        select * from emp where sal > (select sal
                from emp
                where ename='scott')


(2).主查詢和子查詢可以不是同一張表,只要子查詢返回的結果主查詢能夠使用就行

select * from emp
           where deptno = (select deptno
                           from dept
                                where dname='sales')  --也可以使用多表查詢(數據庫只需要請求一次,根據笛卡爾積的大小才能判斷哪種方法
                                                      --比較好


(3)一般不在子查詢中使用排序,因爲對主查詢沒有意義,但在top-N分析順序,要排序

查找員工工資高的前3名:

--rownum:oracle自動加上的僞列,要得到僞列的值,必須在select語句中顯示的查詢出來
--rownum只能使用<,<=
--行號永遠按照默認的順序生成,不會隨着排序而變化
 select rownum,empno,ename,sal
 from(select * from emp order by sal desc)
 where rownum<=3;


(4).執行順序:先執行子查詢,再執行主查詢,但相關查詢例外

相關子查詢:(對於外面的表有一個要求,即必須有一個別名),可以把主查詢中的值作爲參數傳遞給子查詢
 例:查詢員工表中薪水大於本部門的平均薪水,
 select empno,ename,sal,(select avg(sal) from emp where deptno=e.deptno) salavg
 from emp e
 where sal >(select avg(sal) from emp where deptno=e.deptno)

(5).單行子查詢只能使用單行子查詢,多行子查詢使用多行子查詢(查詢結果多行)

單行操作符:=,>,>=,<,<=,<>
多行操作符:in,any,all

查詢員工信息,職位爲7566員工一樣,sal大於7588

select * from emp
         where job =
          (select job from emp where empno=7566 ) and
          sal>(select sal from emp where empno=7588)
     

查詢工資最低的員工信息
select * from emp
where sal = (select min(sal) from emp);

查詢最低工資大於20號部門最低工資的部門號和部門的最低工資


select dept,min(sal) from emp
group by deptno
having min(sal) > (select min(sal) from emp
            where deptno=20);


多行子查詢:
   select *
    from emp
    where deptno in (select * from dept where dname='sales' or dname='accounting')

還可以使用多表查詢

查詢工資比30號部門任意一個員工高的員工信息

select * from emp
         where sal > any (select sal from emp where deptno=30)
                  --(select min(sal) from emp where deptno=30)

查詢工資比30號部門所有一個員工高的員工信息

select * from emp
         where sal > all(select sal from emp where deptno=30)
             --(select max(sal) from emp where deptno=30)




(6)子查詢null問題
單行子查詢null問題
   select * from emp
          where job =
               (select job
                from emp where ename='tom')


多行子查詢的null值問題
查詢不是老闆的員工
select * from emp
      where emp not in (select mgr from emp )--如果子查詢出來的結果有null
  只要集合中有null值,那麼不能使用not in (可以使用in),因爲not in 相當於<>all,(in等同於any)
  <>null永遠爲假


正確的

select * from emp
      where emp not in (select mgr from emp where mgr is not null )



 ----------------------------華麗麗的分割線--------------------------------------------------------

分頁查詢
     select * from
      (select rownum r ,e.* from  
     (select * from emp order by sal desc) e1 where rownum<=5)
      where r>1
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章