From子句中的子查询
查询工资大于本部门平均工资的员工。
select ename,e.deptno,sal,asal
from emp e,
(select deptno ,avg(sal) asal from emp group by deptno) a
where e.deptno=a.deptno and sal>asal;
A为视图,为什么要使用别名asal,因为表达式不能当列的名称,别名的本质使用方法是使非法的合法化。

相互关联的子查询
select ename,sal ,deptno
from emp o
where sal>(select avg(sal) from emp where deptno=o.deptno) ;

先运行主查询,得到第一行,将DEPTNO传入到子查询,由子查询求出AVG(SAL),在判定主查询的行是否符合查询的条件。
执行计划是将子查询看作视图的关联。这叫做SQL的自动改写。
Exists操作
找领导,其中3是常量,你写什么都可以。
当子查询有行时,Exists返回true
查到行后就不再继续查询
当子查询没有行时为假,Exists返回false
select ename,empno,mgr from emp o
where exists(select 3 from emp where mgr=o.empno);

