子查询
# 子查询
# 1. 由一个具体的需求,引入子查询
#需求:谁的工资比 Abel 的高?
SELECT employee_id,last_name,salary
FROM employees
WHERE salary > (
SELECT salary FROM employees WHERE last_name='Abel'
)
ORDER BY salary DESC;
# 2. 称谓的规范:外查询(或主查询)、内查询(或子查询)
子查询(内查询)在主查询之前一次执行完成。
子查询的结果被主查询(外查询)使用 。
注意事项
子查询要包含在括号内
将子查询放在比较条件的右侧
单行操作符对应单行子查询,多行操作符对应多行子查询
# 3. 子查询的分类
角度 1:从内查询返回的结果的条目数
单行子查询 vs 多行子查询
角度 2:内查询是否被执行多次
相关子查询 vs 不相关子查询
比如:相关子查询的需求:查询工资大于本部门平均工资的员工信息。
不相关子查询的需求:查询工资大于本公司平均工资的员工信息。
# 4. 单行子查询
4.1 单行操作符:= != > >= < <=
4.2 子查询中的空值问题
4.3 非法使用子查询
# 5.多行子查询
5.1 多行子查询的操作符:IN ANY ALL SOME(同 ANY)
IN:
SELECT employee_id, last_name
FROM employees
WHERE salary IN
(SELECT MIN(salary)
FROM employees
GROUP BY department_id);
ANY / ALL:
#题目:返回其它 job_id 中比 job_id 为‘IT_PROG’部门任一工资低的员工的员工号、姓名、job_id 以及 salary
SELECT employee_id,last_name,job_id,salary
FROM employees
WHERE job_id <> 'IT_PROG'
AND salary < ANY (
SELECT salary
FROM employees
WHERE job_id = 'IT_PROG'
); #题目:返回其它 job_id 中比 job_id 为‘IT_PROG’部门所有工资低的员工的员工号、姓名、job_id 以及 salary
SELECT employee_id,last_name,job_id,salary
FROM employees
WHERE job_id <> 'IT_PROG'
AND salary < ALL (
SELECT salary
FROM employees
WHERE job_id = 'IT_PROG'
);
#题目:查询平均工资最低的部门 id
#MySQL 中聚合函数是不能嵌套使用的。
SELECT department_id FROM employees
GROUP BY department_id
HAVING AVG(salary) <= ALL(
SELECT AVG(salary) FROM employees
GROUP BY department_id
)
5.3 空值问题
SELECT last_name
FROM employees
WHERE employee_id NOT IN (
SELECT manager_id
FROM employees
);
# 6. 相关子查询
在 SELECT 中,除了 GROUP BY 和 LIMIT 之外,其他位置都可以声明子查询!
SELECT ....,....,....(存在聚合函数)
FROM ... (LEFT / RIGHT)JOIN ....ON 多表的连接条件
(LEFT / RIGHT)JOIN ... ON ....
WHERE 不包含聚合函数的过滤条件
GROUP BY ...,....
HAVING 包含聚合函数的过滤条件
ORDER BY ....,...(ASC / DESC )
LIMIT ...,....
题目:若 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`
)
# 子查询的课后练习
# 查询和 Zlotkey 相同部门的员工姓名和工资
SELECT last_name,salary
FROM employees
WHERE department_id IN (
SELECT department_id
FROM employees
WHERE last_name = 'Zlotkey'
);
# 查询工资比公司平均工资高的员工的员工号,姓名和工资。
SELECT employee_id,last_name,salary
FROM employees
WHERE salary > (
SELECT AVG(salary) FROM employees
);
# 查询平均工资最低的部门信息
ALL:用于比较子查询返回的所有值。条件 AVG(salary) <= ALL (...) 确保 AVG(salary) 小于或等于子查询中返回的每一个值,即找到最低平均工资的部门。
ANY:用于比较子查询返回的任意值。条件 AVG(salary) <= ANY (...) 意味着 AVG(salary) 只需小于或等于子查询中返回的至少一个值。
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
)
);
# 查询平均工资最低的部门信息和该部门的平均工资(相关子查询)
#建议从内往外写,这样比较清楚能理解
SELECT AVG(salary) avg_sal
FROM employees
GROUP BY department_id
ORDER BY avg_sal ASC
LIMIT 1
SELECT d.\*,sub.avg_sal
FROM departments d
JOIN(
SELECT department_id,AVG(salary) avg_sal
FROM employees
GROUP BY department_id
ORDER BY avg_sal ASC
LIMIT 1
) sub
ON d.department_id = sub.department_id;
# 查询平均工资最高的 job 信息
SELECT AVG(salary) avg_sal
FROM employees
GROUP BY job_id
ORDER BY avg_sal DESC
LIMIT 1
SELECT j.\*,sub.avg_sal
FROM jobs j
JOIN(
SELECT job_id,AVG(salary) avg_sal
FROM employees
GROUP BY job_id
ORDER BY avg_sal DESC
LIMIT 1
) sub
ON j.job_id = sub.job_id;
# 查询平均工资高于公司平均工资的部门有哪些?
SELECT AVG(salary) FROM employees
SELECT department_id
FROM employees
WHERE department_id IS NOT NULL
GROUP BY department_id
HAVING AVG(salary) > (SELECT AVG(salary) FROM employees)
# 查询出公司中所有 manager 的详细信息
SELECT DISTINCT manager_id FROM employees WHERE manager_id IS NOT NULL
SELECT * FROM employees WHERE employee_id
IN(SELECT DISTINCT manager_id FROM employees WHERE manager_id IS NOT NULL)
# 各个部门中 最高工资中最低的那个部门的 最低工资是多少?
SELECT MIN(salary)
FROM employees e JOIN (
SELECT department_id,MAX(salary) max_sal
FROM employees
GROUP BY department_id
ORDER BY max_sal ASC
LIMIT 0,1
) t_dept_max_sal
ON e.department_id = t_dept_max_sal.department_id
# 查询平均工资最高的部门的 manager 的详细信息: last_name, department_id, email, salary
SELECT last_name, department_id, email, salary
FROM employees
WHERE employee_id IN (
SELECT DISTINCT manager_id
FROM employees e,(
SELECT department_id,AVG(salary) avg_sal
FROM employees
GROUP BY department_id
ORDER BY avg_sal DESC
LIMIT 1
) t_dept_avg_sal
WHERE e.`department_id` = t_dept_avg_sal.department_id
);
# 查询部门的部门号,其中不包括 job_id 是"ST_CLERK"的部门号
#方式 1:
SELECT department_id
FROM departments
WHERE department_id NOT IN (
SELECT DISTINCT department_id
FROM employees
WHERE job_id = 'ST_CLERK'
);
#方式 2:
SELECT department_id
FROM departments d
WHERE NOT EXISTS (
SELECT \*
FROM employees e
WHERE d.`department_id` = e.`department_id`
AND e.`job_id` = 'ST_CLERK'
);
选择所有没有管理者的员工的 last_name
SELECT last_name
FROM employees emp
WHERE NOT EXISTS (
SELECT \*
FROM employees mgr
WHERE emp.`manager_id` = mgr.`employee_id`
);
# 查询员工号、姓名、雇用时间、工资,其中员工的管理者为 'De Haan'
#方式 1:
SELECT employee_id,last_name,hire_date,salary
FROM employees
WHERE manager_id IN (
SELECT employee_id
FROM employees
WHERE last_name = 'De Haan'
);
#方式 2:
SELECT employee_id,last_name,hire_date,salary
FROM employees e1
WHERE EXISTS (
SELECT \*
FROM employees e2
WHERE e1.`manager_id` = e2.`employee_id`
AND e2.last_name = 'De Haan'
);
# 查询各部门中工资比本部门平均工资高的员工的员工号, 姓名和工资(相关子查询)
#方式 1:使用相关子查询
SELECT last_name,salary,department_id
FROM employees e1
WHERE salary > (
SELECT AVG(salary)
FROM employees e2
WHERE department_id = e1.`department_id`
); #方式 2:在 FROM 中声明子查询
SELECT e.last_name,e.salary,e.department_id
FROM employees e,(
SELECT department_id,AVG(salary) avg_sal
FROM employees
GROUP BY department_id) t_dept_avg_sal
WHERE e.department_id = t_dept_avg_sal.department_id
AND e.salary > t_dept_avg_sal.avg_sal
# 查询每个部门下的部门人数大于 5 的部门名称(相关子查询)
SELECT department_name
FROM departments d
WHERE 5 < (
SELECT COUNT(\*)
FROM employees e
WHERE d.department_id = e.department_id
# 查询每个国家下的部门个数大于 2 的国家编号(相关子查询)
SELECT country_id
FROM locations l
WHERE 2 < (
SELECT COUNT(\*)
FROM departments d
WHERE l.location_id = d.location_id
)