Oracle(第五天)

子查詢
 一、單行子查詢:
    例如:select * from emp where sal>(select sal from emp where ename = 'ALLEN');
 二、多行子查詢: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 (用法舉例以下)
這三種語法輸出結果同樣。
三、多列子查詢:(瞭解)
  select deptno , job from emp where deptno != 10;
  select * from emp where (deptno,job) in (select deptno , job from emp where deptno != 10);
四、相關子查詢:
  每執行一次主查詢,就要執行一次子查詢。
  查詢各個部門工資比平均工資高的人的信息
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);
 
五、練習題截圖:
 
 
 
用的表也是第四天的表。
相關文章
相關標籤/搜索