Oracle(第五天)
阿新 • • 發佈:2020-11-13
子查詢
1、單行子查詢:
例如:select * from emp where sal>(select sal from emp where ename = 'ALLEN');
2、多行子查詢:any all in
大於any就是大於最小值
select * from emp where sal>any(select sal from emp where deptno=10);
select * from emp where sal>(select min(sal) from emp where deptno=10);
大於all就是大於最大值
select * from emp where sal>all(select sal from emp where deptno=10);
select * from emp where sal>(select max(sal) from emp where deptno=10);
小於any就是小於最大值
select * from emp where sal<any(select sal from emp where deptno=10);
select * from emp where sal<(select max(sal) from emp where deptno=10);
小於all就是小於最小值
select * from emp where sal<all(select sal from emp where deptno=10);
select * from emp where sal<(select min(sal) from emp where deptno=10);
in (用法舉例如下)
3、多列子查詢:(瞭解)
select deptno , job from emp where deptno != 10;
select * from emp where (deptno,job) in (select deptno , job from emp where deptno != 10);
4、相關子查詢:
每執行一次主查詢,就要執行一次子查詢。
查詢各個部門工資比平均工資高的人的資訊
select * from emp where deptno = 10 and sal > (select avg(sal) from emp where deptno=10);
select * from emp where deptno = 20 and sal > (select avg(sal) from emp where deptno=20);
select * from emp where deptno = 30 and sal > (select avg(sal) from emp where deptno=30);
select * from emp e1 where sal > (select avg(sal) from emp e2 where e1.deptno = e2.deptno);
5、練習題截圖: