DQL语言
子查询
含义:
出现在其他语句中的select语句,称为子查询或内查询
外部的查询语句,称为主查询或外查询
分类:
按子查询出现的位置:
select后面
仅仅支持标量子查询
from后面
支持表子查询
where或having后面: ※
支持标量子查询(单行) ※
支持列子查询(多行) ※
支持行子查询
exists后面(相关子查询)
支持标量子查询
支持列子查询
支持行子查询
支持表子查询
按结果集的行列数不同:
标量子查询(结果集只有一行一列)
列子查询(结果集只有一列多行)
行子查询(结果集有一行多列)
表子查询(结果集一般为多行多列)
一、where或having后面
1.标量子查询(单行子查询)
2.列子查询(多行子查询)
3.行子查询(多列多行)
特点:
① 子查询放在小括号内
② 子查询一般放在条件的右侧
③ 标量子查询:一般搭配着单行操作符使用
> < >= <= = <>
列子查询,一般搭配着多行操作符使用
IN、any/some、all
④ 子查询的执行优先主查询执行,主查询的条件用到了子查询的结果
1.标量子查询
#案例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号员工的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
)
#案例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 MIN(salary),department_id
FROM employees
GROUP BY department_id
#③ 筛选②,满足min(salary)>①
SELECT MIN(salary),department_id
FROM employees
GROUP BY department_id
HAVING MIN(salary)> (
SELECT MIN(salary)
FROM employees
WHERE department_id = 50
)
2.列子查询(多行子查询)
#案例1:返回location_id是1400或1700的部门中的所有员工姓名
#① 查询location_id是1400或1700的部门编号
SELECT department_id
FROM departments
WHERE location_id IN(1400,1700) # =ANY
#② 查询①的员工姓名,要求部门号是①列表中的一个
SELECT last_name
FROM employees
WHERE department_id IN(
SELECT 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'
#② 查询员工的员工号、姓名、job_id 以及salary,要求salary < any(①)
SELECT employee_id,last_name,job_id,salary
FROM employees
WHERE salary < ANY( # WHERE salary <(
SELECT DISTINCT salary # SELECT DISTINCT max(salary)
FROM employees
WHERE job_id = 'IT_PROG'
)AND job_id <> 'IT_PROG';
#案例3:返回其它部门中比job_id为‘IT_PROG’部门所有工资都低的员工的员工号、姓名、job_id 以及salary
SELECT employee_id,last_name,job_id,salary
FROM employees
WHERE salary < ALL( #WHERE salary <(
SELECT DISTINCT salary #SELECT DISTINCT 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 e
WHERE e.department_id = d.`department_id` #作用类似group by
) 个数
FROM departments d;
#案例2:查询员工号=102的部门名
SELECT (
SELECT department_name
FROM departments d
INNER JOIN employees e
ON e.`department_id`=d.`department_id`
WHERE e.`employee_id` = 102
) 部门名;
三、from后面
将子查询结果充当一张表,要求必须起别名
#案例:查询每个部门的平均工资的工资等级
#① 查询每个部门的平均工资
SELECT AVG(salary),department_id
FROM employees
GROUP BY department_id
#② 连接①的结果集和job_grades表,筛选条件平均工资
SELECT ag_dep.*,j.`grade_level`
FROM (
SELECT AVG(salary) ag,department_id
FROM employees
GROUP BY department_id
) ag_dep
INNER JOIN job_grades j
ON ag_dep.ag BETWEEN j.`lowest_sal` AND `highest_sal`
四、exists后面(相关子查询)
语法: exists (完整的查询语句)
结果: 1或0
SELECT EXISTS (SELECT employee_id FROM employees);
#案例1:查询有员工的部门名
#IN 方法
SELECT department_name
FROM departments d
WHERE d.`department_id` IN (
SELECT e.`department_id`
FROM employees e
) ;
#exists方法
SELECT department_name
FROM departments d
WHERE EXISTS(
SELECT *
FROM employees e
WHERE d.`department_id` = e.`department_id`
)
#案例2 :查询没有女朋友的男神信息
#IN 方法
SELECT boyName
FROM boys bo
WHERE bo.id NOT IN(
SELECT boyfriend_id
FROM beauty
)
#exists方法
SELECT boyName
FROM boys bo
WHERE NOT EXISTS(
SELECT boyName
FROM beauty b
WHERE b.`boyfriend_id` = bo.id);
练习
- 查询和 Zlotkey 相同部门的员工姓名和工资
SELECT last_name,salary
FROM employees e
WHERE e.department_id = (
SELECT department_id
FROM employees e
WHERE e.last_name = 'Zlotkey'
)
- 查询工资比公司平均工资高的员工的员工号,姓名和工资。
select employee_id,last_name,salary
from employees e
where e.`salary` > (
select avg(salary)
from employees
);
- 查询各部门中工资比本部门平均工资高的员工的员工号, 姓名和工资
SELECT employee_id,last_name,salary
FROM (
SELECT AVG(salary) 平均工资,department_id
FROM employees e
GROUP BY e.department_id
) 各部门平均工资
INNER JOIN employees d
ON d.department_id = 各部门平均工资.department_id
WHERE d.salary > 各部门平均工资.平均工资
- 查询和姓名中包含字母 u 的员工在相同部门的员工的员工号和姓名
SELECT employee_id,last_name,d.department_id
FROM employees d
WHERE department_id IN (
SELECT DISTINCT e.department_id
FROM employees e
WHERE e.last_name LIKE '%u%')
- 查询在部门的 location_id 为 1700 的部门工作的员工的员工号
SELECT employee_id
FROM employees
WHERE department_id IN (
SELECT department_id
FROM departments
WHERE `location_id` = 1700)
- 查询管理者是 King 的员工姓名和工资
SELECT d.last_name,d.salary
FROM employees d
WHERE d.manager_id IN (
SELECT e.employee_id
FROM employees e
WHERE e.last_name = 'K_ing');
- 查询工资最高的员工的姓名,要求 first_name 和 last_name 显示为一列,列名为 姓.名
SELECT CONCAT(first_name,last_name) '姓.名'
FROM employees
WHERE salary=(
SELECT MAX(salary)
FROM employees
);
总结
标量就是表中的一个元素值,列就是表中一列值,图表是以列为单位的,所以一行可以视为多行多列的特殊情况。
SELECT 后跟列表属性栏名称,故只能支持变量子查询
FROM就是从待选表中取数据,故支持表子查询
WHERE是筛选条件,因此包容性最强,既可以是单值限定,即标量子查询,也可以是列表限定,即列/行子查询。
EXISTS返回的是描述表中是否有元素的boolean值,故支持表子查询。