Oracle基礎(chǔ)學(xué)習(xí)之子查詢
首先使用子查詢的時候注意事項包括,子查詢可以嵌套多層和子查詢需要圓括號()括起來,下面我們來看看詳細(xì)的介紹。
基礎(chǔ)介紹
1,wherer:子查詢一般會返回單行單列 單行多列 多行單列 ;
2,having:子查詢會返回單行單列,同時表示要使用統(tǒng)計函數(shù);
3,from:子查詢返回多行多列數(shù)據(jù)(表結(jié)構(gòu));
4,select:返回單行單列 (一般不使用);
示例詳解
where(進(jìn)行數(shù)據(jù)行的篩選操作):
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:指的是與子查詢返回的內(nèi)容相同。
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:
查詢出高于公司平均工資的職位名稱 職位人數(shù) 平均工資。
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(重點):
查詢出每個部門的名稱 位置 部門人數(shù)。
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(+);
多表查詢和子查詢都能實現(xiàn)統(tǒng)計,那么那種方式更好呢?
答:在實際的工作當(dāng)中,子查詢的主要目地是解決多表查詢的性能問題,所以在開發(fā)中使用的是 最多的。最大作用是解決多表查詢帶來的笛卡爾積影響性能的問題。
復(fù)雜查詢= 簡單查詢+限定查詢+ 多表查詢+ 分組統(tǒng)計查詢 +子查詢;
總結(jié)
以上就是關(guān)于Oracle子查詢的全部內(nèi)容,希望本文的內(nèi)容對大家學(xué)習(xí)或者使用Oracle能有所幫助,如果有疑問大家可以留言交流。
相關(guān)文章
Oracle刪除重復(fù)的數(shù)據(jù),Oracle數(shù)據(jù)去重復(fù)
這篇文章主要介紹了Oracle刪除重復(fù)的數(shù)據(jù),Oracle數(shù)據(jù)去重復(fù),需要的朋友可以參考下2016-08-08Windows系統(tǒng)下Oracle?12c安裝保姆級圖文教程詳解
這篇文章主要給大家介紹了關(guān)于Windows系統(tǒng)下Oracle?12c安裝保姆級圖文教程的相關(guān)資料,Oracle數(shù)據(jù)庫12c的安裝是一個復(fù)雜的過程,但通過正確的安裝前置條件的準(zhǔn)備,精心的安裝過程確實可以讓Oracle?12c穩(wěn)定、高效地運行在各類操作系統(tǒng)中,需要的朋友可以參考下2023-09-09delete archivelog all無法清除歸檔日志解決方法
最近在因歸檔日志暴增,使用delete archivelog all貌似無法清除所有的歸檔日志,究竟是什么原因呢?本文將為您解答,需要的朋友可以參考下2012-12-12Oracle?ORA-00904:標(biāo)識符無效解決方法(太坑了!!)
最近執(zhí)行sql時oracle報錯ORA-00904:標(biāo)識符無效,所以這篇文章主要給大家介紹了關(guān)于Oracle?ORA-00904:標(biāo)識符無效解決方法的相關(guān)資料,需要的朋友可以參考下2022-10-10oracle的導(dǎo)入導(dǎo)出注意事項及心得分享
導(dǎo)入導(dǎo)出oracle方案是備份和恢復(fù)的主旋律,有一點點在使用過程中的體會,在此與大家分享下,希望對大家有所幫助2013-09-09