此前都是根据各种条件查询出一个结果或数据,子查询是对这个结果数据再次的比较,打印结果。
举例:
#查询出 比last_name为'Abel' 薪资salary要高的员工ID与名字
# 方式一:查出'Abel'的工资 再查比这个工资高的员工
SELECT salary
FROM employees
WHERE last_name = 'Abel';
SELECT last_name,salary
FROM employees
WHERE salary > 11000;
# 方式二:自连接
SELECT e2.employee_id,e2.last_name,e2.salary
FROM employees e1,employees e2
WHERE e1.last_name = 'Abel'
AND e1.salary < e2.salary ;
# 方式三:子查询
SELECT employee_id,last_name,salary
FROM employees
WHERE salary >
(
SELECT salary
FROM employees
WHERE last_name = 'Abel'
);
注意事项
- 子查询要包含在括号内
- 将子查询放在比较条件的右侧
- 单行操作符对应单行子查询,多行操作符对应多行子查询
第一种分类方式:
内查询的结果返回一条还是多条记录,将子查询分为单行子查询
、多行子查询
。
子查询从数据表中查询了数据结果,如果这个数据结果只执行一次,然后这个数据结果作为主查询的条件进行执行,那么这样的子查询叫做
不相关子查询
。
子查询需要执行多次,即采用循环的方式,先从外部查询开始,每次都传入子查询进行查询,然后再将结果反馈给外部,这种嵌套的执行方式就称为
相关子查询
。
操作符 | 含义 |
---|---|
= | equal to |
> | greater than |
>= | greater than or equal to |
< | less than |
<= | less than or equal to |
<> | not equal to |
# 查询工资大于149号员工工资的员工的信息
SELECT employee_id,last_name,salary
FROM employees
WHERE salary > (
SELECT salary
FROM employees
WHERE employee_id = 149
);
# 返回job_id与141号员工相同,salary比143号员工多的员工姓名,job_id和工资
SELECT employee_id,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
);
# 返回公司工资最少的员工的last_name,job_id和salary
# 方法一:单行子查询
SELECT last_name,job_id,salary
FROM employees
WHERE salary =
(
SELECT MIN(salary)
FROM employees
);
# 方法二:order by + limit
SELECT last_name,job_id,salary
FROM employees
ORDER BY salary ASC
LIMIT 0,1;
问题:
查询与141号或174号员工的manager_id和department_id相同的其他员工的employee_id,manager_id,department_id
# 不成对比较方式:
SELECT employee_id,manager_id,department_id
FROM employees
WHERE manager_id IN
(
SELECT manager_id
FROM employees
WHERE employee_id IN (141,174)
)
AND department_id IN
(
SELECT department_id
FROM employees
WHERE employee_id IN (141,174)
)
AND employee_id NOT IN (141,174);
# 成对比较方法
SELECT employee_id,manager_id,department_id
FROM employees
WHERE (manager_id,department_id) IN
(
SELECT manager_id,department_id
FROM employees
WHERE employee_id IN (141,174)
)
AND employee_id NOT IN (141,174);
# 查询每个部门的最低工资, 但每个部门最低工资是大于50号部门的最低工资 ,显示部门与它的最低工资
SELECT department_id,MIN(salary)
FROM employees
GROUP BY department_id
HAVING MIN(salary) >
(
SELECT MIN(salary)
FROM employees
WHERE department_id = 50
);
# 题目:
# 显示员工的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;
SELECT last_name, job_id
FROM employees
WHERE job_id =
(SELECT job_id
FROM employees
WHERE last_name = 'Haas');
子查询不返回任何行
SELECT employee_id, last_name
FROM employees
WHERE salary =
(SELECT MIN(salary)
FROM employees
GROUP BY department_id);
多行子查询使用单行比较符
题:返回其它job_id中比job_id为‘IT_PROG’部门任一工资低的员工的员工号、姓名、job_id 以及salary
首先查出 job_id为‘IT_PROG’部门 的全部工资
SELECT salary
FROM employees
WHERE job_id = 'IT_PROG'
使用ANY:
# 返回其它job_id中比job_id为‘IT_PROG’部门任一工资低的员工的员工号、姓名、job_id 以及salary
SELECT employee_id,last_name,job_id,salary
FROM employees
WHERE salary < ANY
(
SELECT salary
FROM employees
WHERE job_id = 'IT_PROG'
)
AND job_id <> 'IT_PROG'; -- <>不等于运算符 --
-- AND job_id != 'IT_PROG';
ANY 是 只要和‘IT_PROG’部门 任意一条薪资 对比 比它小就打印
使用ALL:
# 返回其它job_id中比job_id为‘IT_PROG’部门所有工资都低的员工的员工号、姓名、job_id以及salary
SELECT employee_id,last_name,job_id,salary
FROM employees
WHERE salary < ALL
(
SELECT salary
FROM employees
WHERE job_id = 'IT_PROG'
)
AND job_id <> 'IT_PROG';
-- AND job_id != 'IT_PROG';
ALL 是 需要和‘IT_PROG’部门 所有薪资 对比 比它们都小得就打印
题目:查询平均工资最低的部门id
# 方法一:
SELECT department_id
FROM employees
GROUP BY department_id
HAVING AVG(salary) <= ALL
(
SELECT AVG(salary)
FROM employees
GROUP BY department_id
)
方法二:
SELECT department_id
FROM employees
GROUP BY department_id
HAVING AVG(salary) =
(
SELECT MIN(avg_sal)
FROM
(
SELECT AVG(salary) avg_sal
FROM employees
GROUP BY department_id
) dept_avg_sal
)
结论:在SELECT中,除了GROUP BY 和 LIMIT之外,其他位置都可以声明子查询!
如果子查询的执行依赖于外部查询,通常情况下都是因为子查询中的表用到了外部的表,并进行了条件关联,因此每执行一次外部查询,子查询都要重新计算一次,这样的子查询就称之为关联子查询
。
相关子查询按照一行接一行的顺序执行,主查询的每一行都执行一次子查询。
题目:查询员工中工资大于本部门平均工资的员工的last_name,salary和其department_id
# 查询员工中工资大于本部门平均工资的员工的last_name,salary和其department_id
SELECT last_name,salary,department_id
FROM employees e1
WHERE salary >
(
SELECT AVG(salary)
FROM employees e2
WHERE e1.department_id = e2.department_id
)
# 查询员工中工资大于本部门平均工资的员工的last_name,salary和其department_id
# 使用 FROM中子查询 方式
# 即 自建一张表(表中数据是与主表 进行比较使用的)
# 可以先写from 子查询内表 先单独运行 看看
SELECT e1.last_name,e1.salary,e1.department_id
FROM employees e1,(SELECT department_id,AVG(salary) avg_sal
FROM employees GROUP BY department_id) e2
WHERE e1.department_id = e2.department_id
AND e1.salary > e2.avg_sal
# from 内表 先求出了 每个部门的平均工资-->自建表
from型的子查询:子查询是作为from的一部分,子查询要用()引起来,并且要给这个子查询取别名,
把它当成一张“临时的虚拟的表”来使用。
题目:查询员工的id,salary,按照department_name 排序
SELECT employee_id,salary
FROM employees e
ORDER BY
(
SELECT department_name
FROM departments d
WHERE e.department_id = d.department_id
)
# 查询公司管理者的employee_id,last_name,job_id,department_id信息
# 方式一 自连接
SELECT m.employee_id,m.last_name,m.job_id,m.department_id
FROM employees e,employees m
WHERE e.employee_id = m.employee_id
# 方式二 EXISTS子查询
SELECT employee_id,last_name,job_id,department_id
FROM employees e1
WHERE EXISTS
(
SELECT *
FROM employees e2
WHERE e1.employee_id = e2.manager_id
)
# 方式三 子查询
# employee_id 必须是 manager_id 并且过滤重复的
SELECT employee_id,last_name,job_id,department_id
FROM employees
WHERE employee_id IN
(
SELECT DISTINCT manager_id
FROM employees
);
NOT EXISTS
#查询departments表中,不存在于employees表中的部门的
# department_id和department_name
SELECT department_id,department_name
FROM departments
WHERE NOT EXISTS
(
SELECT department_id
FROM employees
WHERE employees.department_id = departments.department_id
)
使用相关子查询依据一个表中的数据更新另一个表的数据。
格式:
UPDATE table1 alias1
SET column = (SELECT expression
FROM table2 alias2
WHERE alias1.column = alias2.column);
使用相关子查询依据一个表中的数据删除另一个表的数据。
题目: 员工表中谁的工资比Abel的高?
#方式1:自连接
SELECT e2.last_name,e2.salary
FROM employees e1,employees e2
WHERE e1.last_name = 'Abel'
AND e1.`salary` < e2.`salary`
#方式2:子查询
SELECT last_name,salary
FROM employees
WHERE salary > (
SELECT salary
FROM employees
WHERE last_name = 'Abel'
);
问题:以上两种方式有好坏之分吗?
解答:自连接方式好!
题目中可以使用子查询,也可以使用自连接。
一般情况建议使用自连接,因为在许多 DBMS 的处理过程中,对于自连接的处理速度要比子查询快得多。
可以这样理解:
子查询实际上是通过未知表进行查询后的条件判断,而自连接是通过已知的自身数据表进行条件判断,因此在大部分 DBMS 中都对自连接处理进行了优化。