子查询:
含义:出现在其他语句中的select语句,称为子查询或内查询;外部的查询语句,称为主查询或外查询。
分类:
(1)按子查询出现的位置:
select后面:仅仅支持标量子查询
from后面:支持表子查询
where或having后面:标量子查询(单行查询)
列子查询 (多行查询)
行子查询
exists后面:(相关子查询)表子查询
(2)按结果集的行列数不同:
标量子查询(结果集只有一行一列)
列子查询(结果集只有一列多行)
行子查询(结果集有一行多列)
表子查询(结果集一般为多行多列)
一、where或having后面:
1、标量子查询(单行子查询)(单行单列)
2、列子查询(多行子查询)(单列多行)
3、行子查询(多列多行)
特点:
①子查询放在小括号内
②子查询一般放在条件的右侧
③标量子查询,一般搭配着单行操作符使用(如:> < >= <= = <>)
列子查询,一般搭配着多行操作符使用in、any/some、all
操作符 | 含义 |
IN/NOT IN | 等于列表中的任意一个 |
ANY | SOME | 和子查询返回的某一个值进行比较 |
ALL | 和子查询返回的所有值进行比较 |
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'
); (注:分号最后放在括号外面)
案例二:返回job_id与141号员工相同,salary比143号员工多的员工 姓名,job_id 和工资
①查询141号员工的job_id:
SELECT job_id
FROM employees
WHERE employee_id = 141
②查询143号员工的salary
SELECT salary
FROM employees
WHERE employee_id = 143
③查询员工的姓名,job_id 和工资,要求job_id=①并且salary>②
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
);
案例三:返回公司工资最少的员工的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
);
案例四:查询最低工资大于50号部门最低工资的部门id和其最低工资
①查询50号部门的最低工资
SELECT MIN(salary)
FROM employees
WHERE department_id = 50
②查询每个部门的最低工资
SELECT MIN(salary),department_id
FROM employees
GROUP BY department_id
③ 在②基础上筛选,满足min(salary)>①
SELECT MIN(salary),department_id
FROM employees
GROUP BY department_id
2.列子查询:(多行子查询)(列子查询的结果集是一列多行)
案例一:返回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 <>ALL(
SELECT DISTINCT department_id
FROM departments
WHERE location_id IN(1400,1700)
);
案例二:返回其它工种中比job_id为‘IT_PROG’工种任一工资低的员工的员工号、姓名、job_id 以及salary
①查询job_id为‘IT_PROG’部门任一工资
SELECT DISTINCT salary
FROM employees
WHERE job_id = 'IT_PROG'
②查询员工号、姓名、job_id 以及salary,salary<(①)的任意一个
SELECT last_name,employee_id,job_id,salary
FROM employees
WHERE salary<ANY(
SELECT DISTINCT salary
FROM employees
WHERE job_id = 'IT_PROG'
) AND job_id<>'IT_PROG';
或:
SELECT last_name,employee_id,job_id,salary
FROM employees
WHERE salary<(
SELECT MAX(salary)
FROM employees
WHERE job_id = 'IT_PROG'
) AND job_id<>'IT_PROG';
注意:通过以上的俩个案例,我们可以看出ANY和SOME是一样的,并且他们可以使用,MAX和MIN进行代换,大于其中的任意 一个,就相当于大于其中最小的,而小于其中的任意一个,就相当于小于其中最大的。
但是,需要注意的是ANY和ALL的区别。大于ALL中的数,就是大于ALL中的最大值;小于ALL中的数,就是小于 ALL中的最小值。
案例三:返回其它部门中比job_id为‘IT_PROG’部门所有工资都低的员工的员工号、姓名、job_id 以及salary
SELECT last_name,employee_id,job_id,salary
FROM employees
WHERE salary<ALL(
SELECT DISTINCT salary
FROM employees
WHERE job_id = 'IT_PROG'
) AND job_id<>'IT_PROG';
或:
SELECT last_name,employee_id,job_id,salary
FROM employees
WHERE salary<(
SELECT MIN( salary)
FROM employees
WHERE job_id = 'IT_PROG'
) AND job_id<>'IT_PROG';
3、行子查询(结果集一行多列或多行多列)(用的相对较少)
案例:查询员工编号最小并且工资最高的员工信息
SELECT *
FROM employees
WHERE (employee_id,salary)=( (将多个字段当成一个虚拟的字段来使用,其中的几个字段必须是满足相 同的条件,即它们必须是都大于、都小于、都等于等)
SELECT MIN(employee_id),MAX(salary)
FROM employees
);
用以前的方法:
①查询最小的员工编号
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的后面:(仅仅支持标量子查询)
案例一:查询每个部门的员工个数
SELECT d.*,(
SELECT COUNT(*)
FROM employees
WHERE e.department_id = d.`department_id` (这里必须要注意:员工表中的部门id必须和部门表中的 id相等,否则会出现错误)
) 个数
FROM departments d;
三、FROM的后面:(放在FROM后面的一般就是一个表,所以就是将子查询的结果集充当 成一个表格来使用,充当数据源)
注意:将子查询结果充当一张表,要求必须起别名
案例:查询每个部门的平均工资的工资等级
①查询每个部门的平均工资
SELECT AVG(salary),department_id
FROM employees
GROUP BY department_id
②连接①的结果集和job_grades表,筛选条件平均工资 between lowest_sal and highest_sal
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 lowest_sal AND highest_sal;
注:就是将我们的结果集当成一张表,必须起别名,其他的和我们以前的使用一样。
四、EXISTS的后面:(相关子查询)(布尔类型,就是EXISTS后面的子查询是否有值)
语法:
exists(完整的查询语句)
结果:
1或0(布尔类型)
注:一般能用exists查询的,都能用in来代替
案例一:查询有员工的部门名
#in
SELECT department_name
FROM departments d
WHERE d.`department_id` IN(
SELECT department_id
FROM employees
)
案例二: 查询和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'
)
案例三:查询工资比公司平均工资高的员工的员工号,姓名和工资
①查询平均工资
SELECT AVG(salary)
FROM employees
②查询工资>①的员工号,姓名和工资。
SELECT last_name,employee_id,salary
FROM employees
WHERE salary>(
SELECT AVG(salary)
FROM employees
);
案例四:查询各部门中工资比本部门平均工资高的员工的员工号, 姓名和工资
①查询各部门的平均工资
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 ;
案例五:查询和姓名中包含字母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%'
);
案例六:查询在部门的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
);
案例七:查询管理者是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'
);
案例八:查询工资最高的员工的姓名,要求first_name和last_name显示为一列,列名为 姓.名
①查询最高工资
SELECT MAX(salary)
FROM employees
②查询工资=①的姓.名
SELECT CONCAT(first_name,last_name) "姓.名"
FROM employees
WHERE salary=(
SELECT MAX(salary)
FROM employees
);