一、子查询
1、含义
嵌套在其他语句内部的select语句称为子查询或内查询,即一个查询的结果作为另一个查询的条件。
外面的语句可以是insert、update、delete、select等,一般select作为外面语句较多
外面如果为select语句,则此语句称为外查询或主查询。
2、分类
(1)按功能不同(结果集的行列数不同)
① 标量子查询(结果集只有一行一列)
② 列子查询(结果集只有一列多行)
③行子查询(结果集只有多行多列,一行多列)
④ 表子查询(结果集一般为多行多列)
(2)按子查询出现的位置
select 后面:
仅仅支持标量子查询
from 后面:
表子查询
where 或 having 后面:
标量子查询
列子查询
行子查询
exists 后面:
标量子查询
列子查询
行子查询
表子查询
二、where 或 having 后面
1、使用子查询
特点:
① 子查询放在小括号内;
② 子查询一般放在条件的右侧;
③ 标量子查询,一般搭配单行操作符使用(>,>=, <, <=, = ,<>)
④ 列子查询,一般搭配多行操作符使用 (IN、ANY/SOME、 ALL)
⑤ 子查询的执行优先于主查询执行,主查询的条件用到了子查询的结果
2、标量子查询(单行子查询)
(1)谁的工资比 Abel 高?
① 查询 Abel 的工资
SELECT
salary
FROM
employees
WHERE last_name = 'Abel' ;
② 查询员工的信息,salary 大于 ①
SELECT *
FROM employees
WHERE salary >
(SELECT
salary
FROM
employees
WHERE last_name = 'Abel'
);
(2)返回 job_id 与 141号员工相同,salary比 143 号员工多的员工姓名,job_id 和工资
① 查询141号的 job_id
SELECT
job_id
FROM
employees
WHERE employee_id = 141 ;
② 查询143 号工资
SELECT
salary
FROM
employees
WHERE employee_id = 143 ;
③ 查询员工的姓名,job_id 和工资,要求 job_id=① 并且 salary > ②
SELECT *
FROM employees
WHERE job_id = (
SELECT job_id
FROM employees
WHERE employee_id = 141
)
AND salary > (
SELECT salary
FROM employees
WHERE employee_id = 143
);
(3)返回公司工资最少的员工的last_name, job_id 和 salary
① 查询公司的最低工资
SELECT
MIN(salary)
FROM
employees ;
② 查询last_name, job_id和 salary,要求 salary = ①
SELECT last_name, job_id, salary
FROM employees
WHERE salary = (
SELECT MIN(salary)
FROM employees
);
(4)查询最低工资大于50号部门最低工资的部门id和其最低工资
① 查询 50号部门的最低工资
SELECT
MIN(salary)
FROM
employees
WHERE department_id = '50' ;
② 查询每个部门的最低工资
SELECT department_id, MIN(salary)
FROM employees
GROUP BY department_id;
③ 在②上面筛选,满足 min(salry) > ①
SELECT department_id, MIN(salary)
FROM employees
GROUP BY department_id
HAVING MIN(salary) > (
SELECT MIN(salary)
FROM employees
WHERE department_id = '50'
);
(5)非法使用标量子查询:子查询结果不是一行一列
SELECT department_id, MIN(salary)
FROM employees
GROUP BY department_id
HAVING MIN(salary) > (
SELECT salary
FROM employees
WHERE department_id = '50'
);
3、列子查询(多行子查询)
多行子查询操作符:
多行比较操作符:
操作符 含义
IN、NOT IN (不)等于列表中的任意一个
ANY、SOME 和子查询返回的某一个值比较
ALL 和子查询返回的所有值比较
(1)返回location_id是1400或1700的部门中的所有员工姓名
① 查询 location_id 是1400或1700的部门编号
SELECT DISTINCT
department_id
FROM
departments
WHERE location_id IN (1400, 1700) ;
② 查询员工姓名,要求部门号是①列表中的某一个
SELECT last_name
FROM employees
WHERE department_id IN (
SELECT DISTINCT department_id
FROM departments
WHERE location_id IN (1400, 1700)
);
使用 ANY 实现:
SELECT last_name
FROM employees
WHERE department_id = ANY (
SELECT DISTINCT department_id
FROM departments
WHERE location_id IN (1400, 1700)
);
(2)返回其他工种中比 job_id为 'IT_PROG'工种任意工资低的员工的:工号、姓名、job_id 以及 salary
① 查询 job_id 为 'IT_PROG' 的工资
SELECT DISTINCT salary
FROM employees
WHERE job_id = 'IT_PROG';
② 查询部门不是 'IT_PROG' 的部门中比①任意工资低
SELECT employee_id, last_name, job_id, salary
FROM employees
WHERE job_id != 'IT_PROG'
AND salary < ANY(
SELECT DISTINCT salary
FROM employees
WHERE job_id = 'IT_PROG'
);
使用 ANY 实现:
SELECT employee_id, last_name, job_id, salary
FROM employees
WHERE job_id != 'IT_PROG'
AND salary < (
SELECT DISTINCT MAX(salary)
FROM employees
WHERE job_id = 'IT_PROG'
);
(3)返回其他工种中比job_id为 'IT_PROG'工种所有工资都低的员工的员工号、姓名、job_id 以及 salary
① 查询工种为 'IT_PROG' 的工资
SELECT DISTINCT salary
FROM employees
WHERE job_id = 'IT_PROG';
② 查询工种不是 'IT_PROG' 的工种中都比①工资低
SELECT employee_id, last_name, job_id, salary
FROM employees
WHERE job_id != 'IT_PROG'
AND salary < ALL(
SELECT DISTINCT salary
FROM employees
WHERE job_id = 'IT_PROG'
);
方式二:
SELECT employee_id, last_name, job_id, salary
FROM employees
WHERE job_id != 'IT_PROG'
AND salary < (
SELECT DISTINCT MIN(salary)
FROM employees
WHERE job_id = 'IT_PROG'
);
4、行子查询(结果集一行多列或多行多列)
(1)查询员工编号最小并且工资最高的员工信息
① 查询最小的员工编号
SELECT MIN(employee_id)
FROM employees;
② 查询最高工资
SELECT MAX(salary)
FROM employees;
③ 查询员工信息
SELECT *
FROM employees
WHERE employee_id = (
SELECT MIN(employee_id)
FROM employees
) AND salary = (
SELECT MAX(salary)
FROM employees
);
④ 方式二:
SELECT *
FROM employees
WHERE (employee_id, salary) = (
SELECT MIN(employee_id), MAX(salary)
FROM employees
);
三、select 后面
1、select 后面仅仅支持标量子查询
2、案例
(1)查询每个部门的员工个数
SELECT d.*,(
SELECT COUNT(*)
FROM employees e
WHERE e.department_id = d.`department_id`
) 个数
FROM departments d;
(2)查询员工号 = 102的部门号
SELECT (
SELECT department_name
FROM departments d
INNER JOIN employees e
ON d.department_id = e.department_id
WHERE e.employee_id = '102'
) 部门名;
四、from 后面
1、from 后面的子查询结果充当一张表,必须要起别名
2、案例
(1)查询每个部门的平均工资的工资等级
① 查询每个部门的平均工资
SELECT AVG(salary), department_id
FROM employees
GROUP BY department_id;
② 连接①的结果集合job_grades表,筛选条件平均工资
SELECT ag_dep.*, g.grade_level
FROM (
SELECT AVG(salary) ag, department_id
FROM employees
GROUP BY department_id
) ag_dep
INNER JOIN job_grades g
ON ag_dep.ag BETWEEN g.lowest_sal AND g.highest_sal;
五、exists 后面
1、exists 子查询
exists 又称为相关子查询
子查询的结果有没有值,返回布尔值。
语法:
exists(完整的查询语句)
结果:1 或 0
2、案例
(1)
SELECT EXISTS(SELECT employee_id FROM employees WHERE salary = 30000); #0
(2)查询有员工名的部门名
① 方式一
SELECT department_name
FROM departments d
WHERE EXISTS(
SELECT *
FROM employees e
WHERE e.`department_id` = d.`department_id`
);
② 方式二
SELECT department_name
FROM departments d
WHERE d.`department_id` IN(
SELECT department_id
FROM employees
);
(3)查询没有女朋友的男生信息
① 方式一:in
SELECT bo.*
FROM boys bo
WHERE bo.id NOT IN (
SELECT boyfriend_id
FROM beauty
);
② 方式二:exists
SELECT bo.*
FROM boys bo
WHERE NOT EXISTS(
SELECT boyfriend_id
FROM beauty b
WHERE b.`boyfriend_id` = bo.`id`
);
六、案例
1、查询工资最低的员工信息: last_name, salary
① 查询最低的工资
SELECT
MIN(salary)
FROM
employees ;
② 查询last_name,salary,要求salary=①
SELECT last_name,salary
FROM employees
WHERE salary=(
SELECT MIN(salary)
FROM employees
);
2、查询平均工资最低的部门信息
方式一:
① 各部门的平均工资
SELECT AVG(salary),department_id
FROM employees
GROUP BY department_id;
② 查询①结果上的最低平均工资
SELECT MIN(ag)
FROM (
SELECT AVG(salary) ag,department_id
FROM employees
GROUP BY department_id
) ag_dep;
③ 查询哪个部门的平均工资=②
SELECT AVG(salary),department_id
FROM employees
GROUP BY department_id
HAVING AVG(salary)=(
SELECT MIN(ag)
FROM (
SELECT AVG(salary) ag,department_id
FROM employees
GROUP BY department_id
) ag_dep
);
④ 查询部门信息
SELECT d.*
FROM departments d
WHERE d.`department_id`=(
SELECT department_id
FROM employees
GROUP BY department_id
HAVING AVG(salary)=(
SELECT MIN(ag)
FROM (
SELECT AVG(salary) ag,department_id
FROM employees
GROUP BY department_id
) ag_dep
)
);
方式二:
① 各部门的平均工资
SELECT AVG(salary),department_id
FROM employees
GROUP BY department_id;
② 求出最低平均工资的部门编号
SELECT department_id
FROM employees
GROUP BY department_id
ORDER BY AVG(salary)
LIMIT 1;
③ 查询部门信息
SELECT *
FROM departments
WHERE department_id=(
SELECT department_id
FROM employees
GROUP BY department_id
ORDER BY AVG(salary)
LIMIT 1
);
3、查询平均工资最低的部门信息和该部门的平均工资
① 各部门的平均工资
SELECT
AVG(salary),
department_id
FROM
employees
GROUP BY department_id ;
② 求出最低平均工资的部门编号
SELECT
AVG(salary),
department_id
FROM
employees
GROUP BY department_id
ORDER BY AVG(salary)
LIMIT 1 ;
③ 查询部门信息
SELECT d.*,ag
FROM departments d
JOIN (
SELECT AVG(salary) ag,department_id
FROM employees
GROUP BY department_id
ORDER BY AVG(salary)
LIMIT 1
) ag_dep
ON d.`department_id`=ag_dep.department_id;
4、查询平均工资最高的 job 信息
① 查询最高的job的平均工资
SELECT
AVG(salary),
job_id
FROM
employees
GROUP BY job_id
ORDER BY AVG(salary) DESC
LIMIT 1 ;
② 查询job信息
SELECT *
FROM jobs
WHERE job_id=(
SELECT job_id
FROM employees
GROUP BY job_id
ORDER BY AVG(salary) DESC
LIMIT 1
);
5、查询平均工资高于公司平均工资的部门有哪些?
① 查询平均工资
SELECT
AVG(salary)
FROM
employees ;
② 查询每个部门的平均工资
SELECT
AVG(salary),
department_id
FROM
employees
GROUP BY department_id ;
③ 筛选②结果集,满足平均工资>①
SELECT AVG(salary),department_id
FROM employees
GROUP BY department_id
HAVING AVG(salary)>(
SELECT AVG(salary)
FROM employees
);
6、查询出公司中所有 manager 的详细信息
① 查询所有manager的员工编号
SELECT DISTINCT
manager_id
FROM
employees ;
② 查询详细信息,满足employee_id=①
SELECT *
FROM employees
WHERE employee_id = ANY(
SELECT DISTINCT manager_id
FROM employees
);
7、各个部门中 最高工资中最低的那个部门的 最低工资是多少
① 查询各部门的最高工资中最低的部门编号
SELECT department_id
FROM employees
GROUP BY department_id
ORDER BY MAX(salary)
LIMIT 1;
② 查询①结果的那个部门的最低工资
SELECT MIN(salary) ,department_id
FROM employees
WHERE department_id=(
SELECT department_id
FROM employees
GROUP BY department_id
ORDER BY MAX(salary)
LIMIT 1
);
8、查询平均工资最高的部门的 manager 的详细信息: last_name, department_id, email, salary
① 查询平均工资最高的部门编号
SELECT
department_id
FROM
employees
GROUP BY department_id
ORDER BY AVG(salary) DESC
LIMIT 1 ;
② 将employees和departments连接查询,筛选条件是①
SELECT
last_name, d.department_id, email, salary
FROM
employees e
INNER JOIN departments d
ON d.manager_id = e.employee_id
WHERE d.department_id =
(SELECT
department_id
FROM
employees
GROUP BY department_id
ORDER BY AVG(salary) DESC
LIMIT 1) ;
七、练习
1、查询和Zlotkey相同部门的员工姓名和工资
① 查询Zlotkey的部门
SELECT
department_id
FROM
employees
WHERE last_name = 'Zlotkey' ;
② 查询部门号=①的姓名和工资
SELECT
last_name,
salary
FROM
employees
WHERE department_id =
(SELECT
department_id
FROM
employees
WHERE last_name = 'Zlotkey') ;
2、查询工资比公司平均工资高的员工的员工号,姓名和工资。
① 查询平均工资
SELECT
AVG(salary)
FROM
employees ;
② 查询工资>①的员工号,姓名和工资。
SELECT
last_name,
employee_id,
salary
FROM
employees
WHERE salary >
(SELECT
AVG(salary)
FROM
employees) ;
3、查询各部门中工资比本部门平均工资高的员工的员工号, 姓名和工资
① 查询各部门的平均工资
SELECT
AVG(salary),
department_id
FROM
employees
GROUP BY department_id ;
② 连接①结果集和employees表,进行筛选
SELECT
employee_id,
last_name,
salary,
e.department_id
FROM
employees e
INNER JOIN
(SELECT
AVG(salary) ag,
department_id
FROM
employees
GROUP BY department_id) ag_dep
ON e.department_id = ag_dep.department_id
WHERE salary > ag_dep.ag ;
4、查询和姓名中包含字母u的员工在相同部门的员工的员工号和姓名
① 查询姓名中包含字母u的员工的部门
SELECT DISTINCT
department_id
FROM
employees
WHERE last_name LIKE '%u%' ;
② 查询部门号=①中的任意一个的员工号和姓名
SELECT last_name,employee_id
FROM employees
WHERE department_id IN(
SELECT DISTINCT department_id
FROM employees
WHERE last_name LIKE '%u%'
);
5、查询在部门的location_id为1700的部门工作的员工的员工号
① 查询location_id为1700的部门
SELECT DISTINCT
department_id
FROM
departments
WHERE location_id = 1700 ;
② 查询部门号=①中的任意一个的员工号
SELECT employee_id
FROM employees
WHERE department_id =ANY(
SELECT DISTINCT department_id
FROM departments
WHERE location_id = 1700
);
6、查询管理者是King的员工姓名和工资
① 查询姓名为king的员工编号
SELECT
employee_id
FROM
employees
WHERE last_name = 'K_ing' ;
② 查询哪个员工的manager_id = ①
SELECT last_name,salary
FROM employees
WHERE manager_id IN(
SELECT employee_id
FROM employees
WHERE last_name = 'K_ing'
);
7、查询工资最高的员工的姓名,要求first_name和last_name显示为一列,列名为 姓.名
① 查询最高工资
SELECT
MAX(salary)
FROM
employees ;
② 查询工资=①的姓.名
SELECT CONCAT(first_name,last_name) "姓.名"
FROM employees
WHERE salary=(
SELECT MAX(salary)
FROM employees
);
子查询小结:
(1)子查询结果只要是单列,则在 where 后面作为条件;
(2)子查询结果只要是多列,则子 from 后面作为表进行二次查询;