Oracle基礎學習之子查詢_oracle

來源:互聯網
上載者:User

首先使用子查詢的時候注意事項包括,子查詢可以嵌套多層和子查詢需要圓括弧()括起來,下面我們來看看詳細的介紹。

基礎介紹

1,wherer:子查詢一般會返回單行單列 單行多列 多行單列 ;

2,having:子查詢會返回單行單列,同時表示要使用統計函數;

3,from:子查詢返回多行多列資料(表結構);

4,select:返回單行單列 (一般不使用);

樣本詳解

where(進行資料行的篩選操作):

a:查詢出低於公司平均工資的僱員資訊。

select * from emp where sal<(select avg(sal) from emp);

以上的查詢返回單行單列可以作為where子句的過濾條件使用;

b:查詢公司最早僱傭的僱員的資訊。

select * from emp where hiredate= (select MIN(hiredate) from emp);

C:查詢與scott從事同一工作並且工資相同的僱員資訊。

 select* from emp where (job,sal) =( select job,sal from emp where ename ='scott') and ename <>'scott';

in:指的是與子查詢返回的內容相同。

  select * from emp where sal in (select sal from emp where job = 'manager');

not in:

select* from emp where sal not in(select sal from emp where job='manager');

子查詢中不能有空。

any:

 select* from emp where sal = any(select sal from emp where job='manager');select* from emp where sal > any(select sal from emp where job='manager');

比子查詢的返回的最大值要大

 select* from emp where sal < any(select sal from emp where job='manager');

比子查詢返回的最大值要小

all:

<all :比子查詢的返回的最小值要小

     all :比子查詢的返回的最大值要大

     where子查詢的幾率很高;

having:

查詢出高於公司平均工資的職位名稱 職位人數 平均工資。

 select job,count(empno),avg(sal) from emp group by job  having avg(sal)>(select avg(sal) from emp);

select(一般不用):

查詢每個僱員的編號姓名 職位 部門名稱。

 select e.empno,e.ename,e.job, (select d.dname from dept d whered.deptno=e.deptno)from emp e;

(1+n) 次查詢;

from(重點):

查詢出每個部門的名稱 位置 部門人數。

 select d.dname,d.loc,count(e.empno) from emp e,dept d where e.deptno(+)=d.deptno group by d.dname,d.loc;

(多表查詢)

  分步1: select d.deptno,d.dname,d.locfrom dept d;  分步2:select deptno,count(empno)from emp group by deptno; 正確的查詢: select d.deptno,d.dname,d.loc,temp.count  from dept d,(select deptno,count(empno) count from emp group by deptno) temp  where d.deptno=temp.deptno(+);

多表查詢和子查詢都能實現統計,那麼那種方式更好呢?

答:在實際的工作當中,子查詢的主要目地是解決多表查詢的效能問題,所以在開發中使用的是 最多的。最大作用是解決多表查詢帶來的笛卡爾積影響效能的問題。

複雜查詢= 簡單查詢+限定查詢+ 多表查詢+ 分組統計查詢 +子查詢;

總結

以上就是關於Oracle子查詢的全部內容,希望本文的內容對大家學習或者使用Oracle能有所協助,如果有疑問大家可以留言交流。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.