分类方式1: 按内查询的结果返回一条还是多条记录,将子查询分为 单行子查询 、 多行子查询
分类方式2:按内查询是否被执行多次,将子查询划分为 相关(或关联)子查询 和 不相关(或非关联)子查询
不相关子查询:子查询从数据表中查询了数据结果,如果这个数据结果只执行一次,然后这个数据结果作为主查询的条件进行执行
相关子查询 :如果子查询需要执行多次,即采用循环的方式,先从外部查询开始,每次都传入子查询进行查询,然后再将结果反馈给外部
操作符 | 含义 |
---|---|
= | equal to |
> | greater than |
>= | greater than or equal to |
< | less than |
<= | less than or equal to |
<> | not equal to |
t1:查询工资大于149号员工工资的员工的信息
SELECT * FROM employees WHERE salary>( SELECT salary FROM employees WHERE employee_id=149 );
t2:返回job_id与141号员工相同,salary比143号员工多的员工姓名,job_id和工资
SELECT last_name,job_id,salary FROM employees WHERE job_id=( SELECT job_id FROM employees WHERE employee_id=141 )AND salary >( SELECT salary FROM employees WHERE employee_id=143 )
t3:返回公司工资最少的员工的last_name,job_id和salary
SELECT last_name,job_id,salary FROM employees WHERE salary=( SELECT MIN(salary) FROM employees );
t4:查询与141号员工的manager_id和department_id相同的其他员工的employee_id,
manager_id,department_id
SELECT employee_id,manager_id,department_id FROM employees WHERE (manager_id,department_id)=( SELECT manager_id,department_id FROM employees WHERE employee_id=141 );
首先执行子查询,向主查询中的 HAVING 子句返回结果
题目:查询最低工资大于50号部门的部门id和其最低工资
SELECT department_id,MIN(salary) min_salary FROM employees e GROUP BY e.`department_id` HAVING min_salary > ( SELECT MIN(salary) FROM employees WHERE department_id=50 );
在CASE表达式中使用单列子查询
题目:显式员工的employee_id,last_name和location。其中,若员工department_id与location_id为1800的department_id相同,则location为’Canada’,其余则为’USA’
SELECT employee_id,last_name,CASE department_id WHEN( SELECT department_id FROM departments WHERE location_id=1800) THEN 'Canada' ELSE 'USA' END location FROM employees;
空值:子查询为空值时主查询没有行返回
非法使用:多行子查询使用单行比较符
多行子查询也称为集合比较子查询,内查询返回多行,使用多行比较操作符
操作符 | 含义 |
---|---|
IN | 等于列表中的任意一个 |
ANY | 需要和单行比较操作符一起使用,和子查询返回的某一个值比较 |
ALL | 需要和单行比较操作符一起使用,和子查询返回的所有值比较 |
SOME | 实际上是ANY的别名,作用相同,一般常使用ANY |
t1:返回其它job_id中比job_id为‘IT_PROG’部门任一工资低的员工的员工号、姓名、job_id 以及salary
SELECT employee_id,job_id,salary FROM employees WHERE salary < ANY (SELECT salary FROM employees WHERE job_id='IT_PROG' )AND job_id <> 'IT_PROG';
返回其它job_id中比job_id为‘IT_PROG’部门所有工资都低的员工的员工号、姓名、job_id以及
salary
SELECT employee_id,job_id,salary FROM employees WHERE salary < ALL(SELECT salary FROM employees WHERE job_id='IT_PROG' )AND job_id <> 'IT_PROG';
查询平均工资最低的部门id
SELECT department_id,AVG(salary) avg_salary FROM employees GROUP BY department_id HAVING avg_salary <= ALL ( SELECT AVG(salary) FROM employees GROUP BY department_id );
SELECT last_name FROM employees WHERE employee_id NOT IN ( SELECT manager_id FROM employees );
说明:同样的子查询返回空值时,空值参与到主查询中会导致没有记录返回
正确示例:
如果子查询的执行依赖于外部查询,通常情况下都是因为子查询中的表用到了外部的表,并进行了条件关联,因此每执行一次外部查询,子查询都要重新计算一次,这样的子查询就称之为 关联子查询
相关子查询按照一行接一行的顺序执行,主查询的每一行都执行一次子查询,也就是子查询中使用主查询中的列
查询员工中工资大于本部门平均工资的员工的last_name,salary和其department_id
SELECT last_name,salary,department_id FROM employees e1 WHERE e1.`salary`>( SELECT AVG(salary) FROM employees e2 WHERE e1.`department_id`=e2.department_id GROUP BY e2.`department_id` );
查询员工的id,salary,按照department_name 排序
SELECT employee_id,salary FROM employees e ORDER BY ( SELECT d.department_name FROM departments d WHERE d.department_id=e.`department_id` );
若employees表中employee_id与job_history表中employee_id相同的数目不小于2,输出这些相同id的员工的employee_id,last_name和其job_id
SELECT e.employee_id,e.last_name,e.job_id FROM employees e WHERE 2<=( SELECT COUNT(*) FROM job_history j WHERE j.employee_id=e.employee_id );
关联子查询通常也会和 EXISTS操作符一起来使用,用来检查在子查询中是否存在满足条件的行
如果在子查询中不存在满足条件的行:
条件返回 FALSE
继续在子查询中查找
如果在子查询中存在满足条件的行:
不在子查询中继续查找
条件返回 TRUE
NOT EXISTS关键字表示如果不存在某种条件,则返回TRUE,否则返回FALSE
题目:查询公司管理者的employee_id,last_name,job_id,department_id信息
SELECT e1.employee_id,e1.last_name,e1.job_id,e1.department_id FROM employees e1 WHERE EXISTS ( SELECT e2.`employee_id` FROM employees e2 WHERE e2.`manager_id`=e1.`employee_id` );
查询departments表中,不存在于employees表中的部门的department_id和department_name
SELECT department_id,department_name FROM departments d WHERE NOT EXISTS ( SELECT e.`department_id` FROM employees e WHERE e.`department_id`=d.`department_id` );