一:返回结果为单行单列和单行多列子查询。
当子查询的返回结果为单行单列数据记录时,该子查询语句一般在主查询语句WHERE字句里,通常会包含比较运算符(”=,<,>“);
select name
from t_employee
where sal>(
select sal
from t_employee
where ename='smith'
);
select name,sal,job
from t_employee
where (sal,job) = (
select sal,job
from t_employee
where name='SMITH'
);
二:返回结果为 多行单列子查询。
有雇员表t_employee和部门表t_dept.
(1).当子查询的返回结果为多行单列数据记录时,该子查询语句一般会在主查询语句where 字语句里出现。通常会包含in,any,all,exists等关键字。
select *
from t_employee
where deptno in(
select deptno
from t_dept
);
(2).带有any关键字的子查询.
关键子any用来表示主查询的条件为满足子查询返回查询结果中任意一条记录,该关键字有三种匹配方式,分别如下:
=any :其功能与关键字in一样。
>any(>=any) :比子查询中返回数据记录中最小的数据要大于(大于等于)记录;
<any(<=any) :比子查询中返回数据记录中最大的数据要小于(大于等于)记录;
select name,sal
from t_employee
where sal=>any(
select sal from t_employee
where job='manager');
(3).带有all关键字的子查询.
关键子all用来表示主查询的条件为满足子查询返回查询结果中任意一条记录,该关键字有2种匹配方式,分别如下:
>all(>=all) :比子查询中返回数据记录中最大的数据还要大的记录;
<all(<=all) :比子查询中返回数据记录中最小的数据要小于(大于等于)记录;
select name,sal
from t_employee
where sal=>all(
select sal from t_employee
where job='manager');