1、子查询
子查询的性能优于多表查询
子查询可以在select子句,from子句,where子句中最多
where子句:子查询返回单行单列,多行单列,单行多列的数据
from子句:子查询返回的一般是多行多列的数据,作为一张临时表出现
WHERE子句:
要求查处工资比SMITH还要高的全部雇员的信息
select * from emp where sal>(select sal from emp where ename='SMITH');
查询高于公司平均工资的雇员
select * from emp where sal > (select AVG(sal) from emp);
where子句返回单行多列的数据(很少见)
select * from emp where (job,sal) = (select job,sal from emp where ename ='ALLEN');
where子句返回多行单列的数据
1、IN操作符
select * from emp where sal in (select sal from emp where job ='MANAGER');
使用IN的时候要注意NOT IN 在子查询中,如果有一个内容是null,则不会查询出任何结果
2、ANY操作符:与每一个内容匹配,有三种匹配方式
=ANY :功能与IN操作符是完全一样的
select * from emp where sal =any(select sal from emp where job ='MANAGER');
>ANY 比子查询中返回的记录最小的还要大的数据
select * from em