一,子查询
子查询:在一个查询的内部还包括另外一个查询
select * from emp where sal>(select sal from emp where empno=7654);
单列子查询:返回的结果是一列的一个内容
单行子查询:返回多个列,有可能是一条完整的记录
多行子查询:返回多条记录
select * from emp where sal>(select sal from emp where empno=7654) and job=(select job from emp where empno=7788);
select * from emp where sal=(select min(sal) from emp);
select d.dname,ed.c,ed.a from dept d,(select deptno,count(empno) c,avg(sal) a from emp group by deptno) ed where d.deptno=ed.deptno;
select * from emp where sal in(select min(sal) from emp group by deptno);
select * from emp where sal =any(select min(sal) from emp group by deptno);
>any:表示大于最小的
<any:表示小于最大的
>all:大于最大的
<all:小于最小的
//多列子查询,同时匹配两个列
select *
from emp
where (sal,nvl(comm,-1)) in (select sal,nvl(comm,-1) from emp where
deptno=20);