Skip to content

列子查询

概述

子查询返回的结果是一列(可以是多行)这种子查询称为列子查询

常用的操作符 IN,NOT IN, ANY, SOME,ALL

操作符描述
IN在指定的集合范围之类,多选一
NOT IN不在指定的集合范围之内
ANY子查询返回列表中,有任意满足一个即可
SOME与 ANY 等同,使用 SOME 的地方都可以使用 ANY
ALL子查询返回列表的所有值都满足才行

案例

查询销售部市场部所有的员工信息

  • 查询销售部市场部ID
bash
select id from dept where name = '销售部' or name = '市场部';
  • 查询销售部市场部员工信息
bash

select * from emp where dept_id in (select id from dept where name = '销售部' or name = '市场部');

查询比财务部所有人工资都高的员工信息

  • 查询财务部部门 ID
bash

select id from dept where name = '财务部';
  • 查询财务部所有人的工资
bash
select salary from emp where dept_id = (select id from dept where name = '财务部');
  • 查询比财务部所有人工资都高的员工信息
bash
select * from emp where salary > all (select salary from emp where dept_id = (select id from dept where name = '财务部'));

查询比研发部其中任意一人工资高的员工信息

  • 查询研发部门 ID
bash
select id from dept where name = '研发部';
  • 查询研发部任意一个人的工资
bash
select salary from emp where dept_id = (select id from dept where name = '研发部');
  • 查询比研发部其中任意一人工资高的员工信息
bash
select * from emp where salary > any (select salary from emp where dept_id = (select id from dept where name = '研发部'));