子查询指一个语句嵌套在另一个查询语句内部,这个特性从MySQL4.1开始引入。
SQL中子查询的使用大大增强了select的查询能力,因为很多时候查询需要从结果集中获取数据,或者需要从同一个表中先计算得出一个数据结构,然后与这个数据结果(可能是某个标量,也可能是某个集合)进行比较。
1.子查询的基本概念
1.1子查询的基本使用
-- 子查询的基本语法
SELECT select_list
FROM `table`
WHERE expr operator
(SELECT select_list
FROM TABLE);
- 子查询(内查询)在主查询之前一次执行完成。
- 子查询的结果被主查询(外查询)使用。
- 注意
- 子查询要包含在括号内
- 将子查询放在比较条件的右侧
- 单行操作符对应单行子查询,多行操作符对应多行子查询
1.2子查询的分类
1.2.1.分类方式1
我们按照内查询的结果返回一条还是多条记录,将子查询分为 单行子查询、多行子查询。
单行子查询对应的是单行操作符,所以查询的就是一条记录。
多行子查询对应的是多行操作符,所以查询的就是多条记录。
1.2.2.分类方式2
我们按照内查询是否被执行多次,将子查询分为 相关子查询 和 不相关子查询。
子查询从数据表中查询了数据结果,如果这个数据结果只执行一次,然后这个数据结果作为主查询的条件进行执行,那么这样的子查询叫做不相关子查询。
同样,如果子查询需要执行多次,即采用循环的方式,先从外部查询开始,每次都传入子查询进行查询,然后再将结果反馈给外部,这种嵌套执行方式就称为相关子查询。
2.单行子查询
2.1单行比较操作符
操作符 | 含义 |
---|---|
= | 等于 |
> | 大于 |
>= | 大于等于 |
< | 小于 |
<= | 小于等于 |
<> | 不等于 |
2.2示例代码
问题:查询工资大于149好员工工资的员工的信息
其代码为:
SELECT last_name,salary
FROM employees
WHERE salary > (
SELECT salary
FROM employees
WHERE employee_id = 149
);
查询结果为:
通过查询我们可以了解到子查询的结果为10500,(其结果如下图①所示),其查询出的salary的值均大于10500。
![](https://i-blog.csdnimg.cn/blog_migrate/bf8238117a673e26a368543a96f30792.png)
题目:返回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
);
其查询结果为如图②:
![](https://i-blog.csdnimg.cn/blog_migrate/f6bef7a20b00f1773ce58af068195b45.png)
题目:返回公司工资最少的员工的last_name,job_id和salary
其代码表示为:
SELECT last_name,job_id,salary
FROM employees
WHERE salary = (
SELECT MIN(salary)
FROM employees
);
其查询结果为:
![](https://i-blog.csdnimg.cn/blog_migrate/beefd6a902ba35dcebbcb3ead357155f.png)
查询与141号或174号员工的manager_id和department_id相同的其他员工的employee_id,manager_id,department_id
其代码表示为:
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);
其查询结果如图④所示
![](https://i-blog.csdnimg.cn/blog_migrate/9ced3beb72bab4ce4405dbe235d9a418.png)
题目:查询最低工资大于50号部门最低工资的部门id和其最低工资
其代码表示为:
SELECT department_id,MIN(salary)
FROM employees
GROUP BY department_id
HAVING MIN(salary) > (
SELECT MIN(salary)
FROM employees
WHERE `department_id` = 50
)
其查询结果如图⑤所示:
![](https://i-blog.csdnimg.cn/blog_migrate/7c524c1296dd5bae086ae4762a955ca6.png)
2.4case中的子查询
题目:显式员工的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;
其查询结果为:
2.5 子查询中的空值问题
在子查询中,如果查询的结果为空值时,不返回任何结果,也不报错。
3.多行子查询
- 也称为集合比较子查询
- 内查询返回多行
- 使用多行比较操作符
3.1多行比较操作符
操作符 | 含义 |
---|---|
IN | 等于列表中的任意一个值即可 |
ANY | 需要的单行比较操作符一起使用,和子查询返回的某一个值比较 |
ALL | 需要和单行比较操作符一起使用,和子查询返回的所有值比较 |
SOME | 实际上是ANY的别名,作用相同,一般常使用ANY |
3.2代码示例
题目:返回其它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';
其查询结果为:
题目:查询平均工资最低的部门id
其代码表示为:
SELECT department_id,AVG(salary)
FROM employees
GROUP BY department_id
HAVING AVG(salary) <= ALL (
SELECT AVG(salary)
FROM employees
GROUP BY department_id
)
其查询结果为:
4.相关子查询
如果子查询的执行依赖于外部查询,通常情况下都是因为子查询中的表用到了外部的表,并进行了条件关联,因此每执行一次外部查询,子查询都要重新计算一次,这样的子查询就称之为 关联子查询。
相关子查询按照一行接一行的顺序执行,主查询的每一行都执行一次子查询。
其查询的结构为:
SELECT column1,column2,...
FROM table1 `outer`
WHERE column1 operator
(SELECT column1,column2
FROM table2 `inner`
WHERE expr1 = `outer`.expr2
);
4.2代码示例
题目:查询员工中工资大于本部门平均工资的员工的last_name,salary和其department_id
其代码为:
SELECT last_name,salary,department_id
FROM employees e1
WHERE salary > (
SELECT AVG(salary)
FROM employees e2
GROUP BY department_id
WHERE e1.`department_id` = e2.`department_id`
);
其查询结果为:
题目:查询员工的id,salary,按照department_name 排序
SELECT employee_id,salary,department_id
FROM employees e
ORDER BY(
SELECT department_name
FROM departments d
WHERE e.`department_id` = d.`department_id`
) DESC;
为了更直观的看到所排的顺序,将'department_id'也进行查询。
其查询结果为:
题目:若employees表中employee_id与job_history表中employee_id相同的数目不小于2,输出这些相同id的员工的employee_id,last_name和其job_id
其代码表示为:
SELECT employee_id,last_name,job_id
FROM employees e
WHERE 2 <= (
SELECT COUNT(*)
FROM job_history j
WHERE e.`employee_id` = j.`employee_id`
);
其查询结果为:
4.3EXISTS与NOT EXISTS关键字
- 关联子查询通常也会和EXISTS操作符一起来使用,用来检查在子查询中是否存在满足条件的行。
- 如果在子查询中不存在满足条件的行:
- 条件返回FALSE
- 继续在子查询中寻找
- 如果在子查询中存在满足条件的行:
- 不在子查询中继续寻找
- 条件返回TRUE
- NO EXISTS关键字表示如果不存在某种条件,则返回TRUE,否则返回FALSE
题目:查询公司管理者的employee_id,last_name,job_id,department_id信息
其代码表示为:
SELECT employee_id,last_name,job_id,department_id
FROM employees e1
WHERE EXISTS (
SELECT *
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 'a'
FROM employees e
WHERE d.`department_id` = e.`department_id`
);
其查询结果为:
5.子查询练习题
# 第九章_子查询练习
#1.查询和Zlotkey相同部门的员工姓名和工资
SELECT last_name,salary,department_id
FROM employees
WHERE department_id IN (
SELECT department_id
FROM employees
WHERE last_name = 'Zlotkey'
);
#2.查询工资比公司平均工资高的员工的员工号,姓名和工资。
SELECT employee_id,last_name,salary
FROM employees
WHERE salary > (
SELECT AVG(salary)
FROM employees
);
#3.选择工资大于所有JOB_ID = 'SA_MAN'的员工的工资的员工的last_name, job_id, salary
SELECT last_name,job_id,salary
FROM employees
WHERE salary > ALL (
SELECT salary
FROM employees
WHERE job_id = 'SA_MAN'
);
#4.查询和姓名中包含字母u的员工在相同部门的员工的员工号和姓名
SELECT employee_id,last_name,department_id
FROM employees
WHERE department_id IN (
SELECT DISTINCT department_id
FROM employees
WHERE last_name LIKE '%u%'
);
#5.查询在部门的location_id为1700的部门工作的员工的员工号
SELECT employee_id
FROM employees
WHERE department_id IN (
SELECT department_id
FROM departments
WHERE location_id = 1700
);
#6.查询管理者是King的员工姓名和工资
SELECT last_name,salary,manager_id
FROM employees
WHERE manager_id IN (
SELECT employee_id
FROM employees
WHERE last_name = 'King'
);
#7.查询工资最低的员工信息: last_name, salary
SELECT last_name,salary
FROM employees
WHERE salary = (
SELECT MIN(salary)
FROM employees
);
#8.查询平均工资最低的部门信息
# 方式一
SELECT *
FROM departments
WHERE department_id = (
SELECT department_id
FROM employees
GROUP BY department_id
HAVING AVG(salary) = (
SELECT MIN(avg_salary)
FROM (
SELECT AVG(salary) avg_salary
FROM employees e2
GROUP BY department_id
) t_dept_avg_sal
)
);
#方式二
SELECT *
FROM departments
WHERE department_id = (
SELECT department_id
FROM employees
GROUP BY department_id
HAVING AVG(salary) <= ALL(
SELECT AVG(salary)
FROM employees
GROUP BY department_id
)
);
#9.查询平均工资最低的部门信息和该部门的平均工资(相关子查询)
SELECT d.* ,(
SELECT AVG(salary)
FROM employees
WHERE department_id = d.`department_id`) avg_sal
FROM departments d
WHERE department_id = (
SELECT department_id
FROM employees
GROUP BY department_id
HAVING AVG(salary) <= ALL (
SELECT AVG(salary)
FROM employees
GROUP BY department_id
)
)
#10.查询平均工资最高的 job 信息
#方式一
SELECT *
FROM jobs
WHERE job_id = (
SELECT job_id
FROM employees
GROUP BY job_id
HAVING AVG(salary) >= ALL (
SELECT AVG(salary)
FROM employees
GROUP BY job_id
)
);
#方式二
SELECT *
FROM jobs
WHERE job_id = (
SELECT job_id
FROM employees
GROUP BY job_id
HAVING AVG(salary) = (
SELECT MAX(avg_sal)
FROM (
SELECT AVG(salary) avg_sal
FROM employees
GROUP BY job_id
) t_job_avg_sal
)
);
#11.查询平均工资高于公司平均工资的部门有哪些?
SELECT department_id
FROM employees
WHERE department_id IS NOT NULL
GROUP BY department_id
HAVING AVG(salary) > (
SELECT AVG(salary)
FROM employees
);
#12.查询出公司中所有 manager 的详细信息
SELECT *
FROM employees e1
WHERE EXISTS (
SELECT manager_id
FROM employees e2
WHERE e1.`employee_id` = e2.`manager_id`
);
#13.各个部门中 最高工资中最低的那个部门的 最低工资是多少?
SELECT department_id,(
SELECT MIN(salary)
FROM employees
WHERE e.`department_id` = department_id) min_sal
FROM employees e
GROUP BY department_id
HAVING MAX(salary) <= ALL(
SELECT MAX(salary)
FROM employees
GROUP BY department_id
);
#14.查询平均工资最高的部门的 manager 的详细信息: last_name, department_id, email, salary
SELECT last_name,department_id,email,salary
FROM employees
GROUP BY department_id
HAVING AVG(salary) >= ALL (
SELECT AVG(salary)
FROM employees
GROUP BY department_id
);
#15. 查询部门的部门号,其中不包括job_id是"ST_CLERK"的部门号
SELECT department_id
FROM departments
WHERE department_id NOT IN (
SELECT DISTINCT department_id
FROM employees
WHERE job_id = 'ST_CLERK'
)
#16. 选择所有没有管理者的员工的last_name
SELECT last_name
FROM employees e1
WHERE NOT EXISTS(
SELECT *
FROM employees e2
WHERE e1.manager_id = e2.`employee_id`
);
#17.查询员工号、姓名、雇用时间、工资,其中员工的管理者为 'De Haan'
SELECT employee_id,last_name,hire_date "work_time",salary
FROM employees
WHERE manager_id IN (
SELECT employee_id
FROM employees
WHERE last_name = 'De Haan'
);
#18.查询各部门中工资比本部门平均工资高的员工的员工号, 姓名和工资(相关子查询)
SELECT employee_id,last_name,salary
FROM employees e1
WHERE salary > (
SELECT AVG(salary)
FROM employees e2
WHERE e1.`department_id` = e2.`department_id`
)
#19.查询每个部门下的部门人数大于 5 的部门名称(相关子查询)
SELECT DISTINCT department_name
FROM departments d1
WHERE 5 < (
SELECT COUNT(employee_id)
FROM employees e2
WHERE d1.`department_id` = e2.`department_id`
);
#20.查询每个国家下的部门个数大于 2 的国家编号(相关子查询)
SELECT country_id
FROM locations l
WHERE 2 < (
SELECT COUNT(*)
FROM departments d
WHERE l.`location_id` = d.`location_id`
);