文章目录
- 子查询
-
- 1、子查询的基本使用
- 2、 单行子查询
-
- 2.1、单行比较查询
- 2.2、HAVING 中的子查询
- 2.3、CASE中的子查询
- 3、多行子查询
- 4、相关子查询
- 5、EXISTS 与 NOT EXISTS关键字
- 6、练习
子查询
- 子查询指一个查询语句嵌套在另一个查询语句内部的查询,这个特性从MySQL 4.1开始引入。
- SQL 中子查询的使用大大增强了 SELECT 查询的能力,因为很多时候查询需要从结果集中获取数据,或者需要从同一个表中先计算得出一个数据结果,然后与这个数据结果(可能是某个标量,也可能是某个集合)进行比较。
1、子查询的基本使用
- 子查询(内查询)在主查询之前一次执行完成。
- 子查询的结果被主查询(外查询)使用 。
- 子查询要包含在括号内
- 将子查询放在比较条件的右侧
- 单行操作符对应单行子查询,多行操作符对应多行子查询
1、查找比Abel工资高的员工
SELECT salary
FROM employees
WHERE last_name = 'Abel';
SELECT last_name, salary
FROM employees
WHERE salary > 11000;
SELECT e2.last_name, e2.salary
FROM employees e1, employees e2
WHERE e1.last_name = 'Abel'
AND e1.salary < e2.salary;
SELECT last_name,salary
FROM employees
WHERE salary > (
SELECT salary
FROM employees
WHERE last_name = 'Abel'
);
- 题目中子查询和自连接方式对比,自连接效率更高。
- 子查询实际上是通过未知表进行查询后的条件判断。
- 自连接是通过已知的自身数据表进行条件判断,因此在大部分 DBMS 中都对自连接处理进行了优化。
2、 单行子查询
2.1、单行比较查询
1、题目:查询工资大于149号员工工资的员工的信息
SELECT last_name, salary
FROM employees
WHERE salary >
(SELECT salary
FROM employees
WHERE employee_id = 149);
2、题目:返回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);
3、题目:返回公司工资最少的员工的last_name,job_id和salary
SELECT last_name, job_id, salary
FROM employees
WHERE salary =
(SELECT MIN(salary)
FROM employees);
4、查询与141号或174号员工的manager_id和department_id相同的其他员工的employee_id,manager_id,department_id
SELECT employee_id, manager_id, department_id
FROM employees
WHERE manager_id IN
(SELECT manager_id
FROM employees
WHERE employee_id IN (174,141))
AND department_id IN
(SELECT department_id
FROM employees
WHERE employee_id IN (174,141))
AND employee_id NOT IN(174,141);
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);
2.2、HAVING 中的子查询
- 首先执行子查询。
- 向主查询中的HAVING 子句返回结果。
题目:查询最低工资大于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);
2.3、CASE中的子查询
题目:显式员工的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 employees
WHERE location_id = 1800)
) THEN
'Canada'
ELSE
'USA'
END)
FROM employees;
3、多行子查询
- 多行子查询也称为集合比较子查询
- 内查询返回多行
- 使用多行比较操作符
操作符 |
含义 |
IN |
等于列表中的任意一个 |
ANY |
需要和单行比较操作符一起使用,和子查询返回的某一个值比较 |
ALL |
需要和单行比较操作符一起使用,和子查询返回的所有值比较 |
SOME |
实际上是ANY的别名,作用相同,一般常使用ANY |
1、题目:返回其它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';
2、题目:返回其它job_id中比job_id为‘IT_PROG’部门所有工资都低的员工的员工号、姓名、job_id以及salary
SELECT employee_id, last_name, job_id, salary
FROM employees
WHERE salary < ALL
( SELECT salary
FROM employees
WHERE job_id = 'IT_PROG')
AND job_id <> 'IT_PROG';
3、题目:查询平均工资最低的部门id
SELECT department_id
FROM employees
GROUP BY department_id
HAVING AVG(salary) =
(SELECT MIN(avg_sal)
FROM (
SELECT AVG(salary) avg_sal
FROM employees
GROUP BY department_id
) dept_avg_sal
);
SELECT department_id
FROM employees
GROUP BY department_id
HAVING AVG(salary) <= ALL (
SELECT AVG(salary) avg_sal
FROM employees
GROUP BY department_id
);
4、相关子查询
- 如果子查询的执行依赖于外部查询,每执行一次外部查询,子查询都要重新计算一次,这样的子查询就称之为关联子查询。
- 相关子查询按照一行接一行的顺序执行,主查询的每一行都执行一次子查询。
1、题目:查询员工中工资大于本部门平均工资的员工的last_name,salary和其department_id
SELECT last_name, salary, department_id
FROM employees e1
WHERE salary >
(SELECT AVG(salary)
FROM employees e2
WHERE e2.department_id = e1.department_id);
SELECT last_name, salary, e1.department_id
FROM employees e1,
(SELECT department_id, AVG(salary) dept_avg_sal
FROM employees
GROUP BY department_id) e2
WHERE e1.department_id = e2.department_id
AND e1.salary > e2.dept_avg_sal;
- from型的子查询:子查询是作为from的一部分,子查询要用()引起来,并且要给这个子查询取别名, 把它当成一张“临时的虚拟的表”来使用。
3、题目:查询员工的id,salary,按照department_name 排序
SELECT employee_id, salary
FROM employees e
ORDER BY (
SELECT department_name
FROM departments d
WHERE e.department_id = d.department_id
);
4、题目:若employees表中employee_id与job_history表中employee_id相同的数目不小于2,输出这些相同id的员工的employee_id,last_name和其job_id
SELECT e.employee_id, e.last_name, e.job_id
FROM employees e
WHERE 2 <= (SELECT COUNT(*)
FROM job_history
WHERE employee_id = e.employee_id);
5、EXISTS 与 NOT EXISTS关键字
- 关联子查询通常也会和 EXISTS操作符一起来使用,用来检查在子查询中是否存在满足条件的行。
- 如果在子查询中不存在满足条件的行:条件返回 FALSE,继续在子查询中查找。
- 如果在子查询中存在满足条件的行:条件返回 TRUE,不再子查询中继续查找。
- NOT EXISTS关键字表示如果不存在某种条件,则返回TRUE,否则返回FALSE。
1、题目:查询公司管理者的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.manager_id);
SELECT DISTINCT e1.employee_id, e1.last_name, e1.job_id, e1.department_id
FROM employees e1 JOIN employees e2
WHERE e1.employee_id = e2.employee_id;
SELECT employee_id, last_name, job_id, department_id
FROM employees
WHERE employee_id IN (
SELECT DISTINCT manager_id
FROM employees);
2、题目:查询departments表中,不存在于employees表中的部门的department_id和department_name
SELECT department_id, department_name
FROM departments d
WHERE NOT EXISTS (SELECT *
FROM employees
WHERE department_id = d.department_id);
6、练习
- 嵌套select子表,子表必须要有别名。From … rename.
- 作为函数子集,子表不能有别名。ANY(no name);
SELECT last_name, salary
FROM employees
WHERE department_id = (
SELECT department_id
FROM employees
WHERE last_name = 'Zlotkey');
SELECT employee_id, last_name, salary
FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);
SELECT last_name, job_id, salary
FROM employees
WHERE salary > ALL (SELECT salary FROM employees
WHERE job_id = 'SA_MAN');
SELECT employee_id, last_name
FROM employees
WHERE department_id = ANY ( SELECT department_id
FROM employees
WHERE last_name LIKE '%u%');
SELECT employee_id
FROM employees
WHERE department_id IN (SELECT department_id
FROM departments
WHERE location_id = 1700);
SELECT last_name, salary
FROM employees
WHERE manager_id IN ( SELECT employee_id FROM employees
WHERE last_name = 'King');
SELECT last_name, salary
FROM employees
WHERE salary = (SELECT MIN(salary) FROM employees);
SELECT department_id, AVG(salary) dept_avgsal
FROM employees
GROUP BY department_id;
SELECT MIN(dept_avgsal)
FROM (i) t_dept_avgsal;
SELECT department_id
FROM employees
GROUP BY department_id
HAVING AVG(salary) = (ii);
SELECT * FROM departments WHERE department_id = (iii);
SELECT *
FROM departments
WHERE department_id = (SELECT department_id
FROM employees
GROUP BY department_id
HAVING AVG(salary) = (SELECT MIN(dept_avgsal)
FROM (SELECT AVG(salary) dept_avgsal
FROM employees
GROUP BY department_id) t_dept_avgsal));
SELECT *
FROM departments
WHERE department_id = (SELECT department_id
FROM employees
GROUP BY department_id
HAVING AVG(salary) <= ALL ( SELECT AVG(salary) dept_avgsal
FROM employees
GROUP BY department_id));
SELECT *
FROM departments
WHERE department_id = (SELECT department_id
FROM employees
GROUP BY department_id
HAVING AVG(salary) = ( SELECT AVG(salary) dept_avgsal
FROM employees
GROUP BY department_id
ORDER BY dept_avgsal
LIMIT 0, 1));
SELECT *
FROM departments d, (
SELECT department_id, AVG(salary) dept_avgsal
FROM employees
GROUP BY department_id
ORDER BY dept_avgsal
LIMIT 0, 1) t_dept_avgsal
WHERE d.department_id = t_dept_avgsal.department_id;
SELECT d.*, AVG(salary)
FROM employees e
JOIN departments d
ON e.department_id = d.department_id
GROUP BY e.department_id
HAVING AVG(salary) = (SELECT MIN(avg_sal)
FROM (SELECT AVG(salary) avg_sal
FROM employees
GROUP BY department_id) t_avg_sal);
SELECT d.*, AVG(salary) avg_sal
FROM departments d
JOIN employees e
ON e.department_id = d.department_id
WHERE d.department_id = (SELECT department_id
FROM employees
GROUP BY department_id
HAVING AVG(salary) <= ALL( SELECT AVG(salary) avg_sal
FROM employees
GROUP BY department_id));
SELECT d.*, AVG(salary) avg_sal
FROM departments d
JOIN employees e
ON e.department_id = d.department_id
WHERE d.department_id = (SELECT department_id
FROM employees
GROUP BY department_id
HAVING AVG(salary) = ( SELECT AVG(salary) avg_sal
FROM employees
GROUP BY department_id
ORDER BY avg_sal
LIMIT 0, 1));
SELECT d.*, avg_sal
FROM departments d
JOIN (SELECT department_id, AVG(salary) avg_sal
FROM employees
GROUP BY department_id
HAVING AVG(salary) = (SELECT AVG(salary) avg_sal
FROM employees
GROUP BY department_id
ORDER BY avg_sal
LIMIT 0, 1)) t_avg_sal
ON d.department_id = t_avg_sal.department_id;
SELECT d.*, avg_sal
FROM departments d, (
SELECT department_id, AVG(salary) avg_sal
FROM employees
GROUP BY department_id
HAVING AVG(salary) = ( SELECT AVG(salary) avg_sal
FROM employees
GROUP BY department_id
ORDER BY avg_sal
LIMIT 0, 1)) t_avg_sal
WHERE d.department_id = t_avg_sal.department_id;
SELECT d.*, avg_sal
FROM departments d, ( SELECT department_id, AVG(salary) avg_sal
FROM employees
GROUP BY department_id
ORDER BY avg_sal
LIMIT 0, 1) t_avg_sal
WHERE d.department_id = t_avg_sal.department_id;
SELECT j.*, avg_sal
FROM jobs j, ( SELECT job_id, AVG(salary) avg_sal
FROM employees
GROUP BY job_id
ORDER BY avg_sal DESC
LIMIT 0, 1) t_avg_sal
WHERE j.job_id = t_avg_sal.job_id;
SELECT department_id, AVG(salary) avg_sal
FROM employees
WHERE department_id IS NOT NULL
GROUP BY department_id
HAVING avg_sal > (SELECT AVG(salary) FROM employees);
SELECT *
FROM employees
WHERE employee_id IN (SELECT DISTINCT manager_id FROM employees);
SELECT employee_id, last_name, salary
FROM employees e1
WHERE EXISTS ( SELECT *
FROM employees e2
WHERE e2.manager_id = e1.employee_id);
SELECT employee_id, MIN(salary)
FROM employees e,
(SELECT department_id, MAX(salary) max_sal
FROM employees
GROUP BY department_id
ORDER BY max_sal
LIMIT 0,1) dept_max_sal
WHERE e.department_id = dept_max_sal.department_id
SELECT last_name, employee_id, department_id, email, salary
FROM employees e, ( SELECT manager_id
FROM departments
WHERE department_id = ( SELECT department_id
FROM employees
GROUP BY department_id
ORDER BY AVG(salary) DESC
LIMIT 0, 1)) t_magr
WHERE e.employee_id = t_magr.manager_id;
SELECT employee_id,last_name, department_id, email, salary
FROM employees
WHERE employee_id IN (
SELECT DISTINCT manager_id
FROM employees
WHERE department_id = (
SELECT department_id
FROM employees e
GROUP BY department_id
HAVING AVG(salary) >= ALL(
SELECT AVG(salary)
FROM employees
GROUP BY department_id
)
)
);
SELECT department_id
FROM departments
WHERE department_id NOT IN (SELECT DISTINCT department_id
FROM employees
WHERE job_id = 'ST_CLERK');
SELECT department_id
FROM departments d
WHERE NOT EXISTS (
SELECT *
FROM employees e
WHERE d.`department_id` = e.`department_id`
AND job_id = 'ST_CLERK'
);
SELECT last_name
FROM employees
WHERE manager_id IS NULL;
SELECT last_name
FROM employees e1
WHERE NOT EXISTS (
SELECT *
FROM employees e2
WHERE e1.manager_id = e2.employee_id
);
SELECT employee_id, last_name, hire_date, salary
FROM employees
WHERE manager_id = (SELECT employee_id FROM employees
WHERE last_name = 'De Haan');
SELECT e.employee_id, last_name, salary
FROM employees e, (SELECT department_id, AVG(salary) avg_sal
FROM employees
GROUP BY department_id) t_avgsal
WHERE e.department_id = t_avgsal.department_id
AND salary > t_avgsal.avg_sal;
SELECT employee_id, last_name, salary
FROM employees
WHERE department_id IN (SELECT department_id FROM employees
GROUP BY department_id
HAVING salary > AVG(salary));
SELECT department_name
FROM departments
WHERE department_id IN (SELECT department_id
FROM employees
GROUP BY department_id
HAVING COUNT(employee_id) > 5);
SELECT department_name
FROM departments d
WHERE 5 < (
SELECT COUNT(*)
FROM employees e
WHERE d.`department_id` = e.`department_id`
);
SELECT country_id
FROM locations
WHERE location_id IN ( SELECT location_id
FROM departments
GROUP BY location_id
HAVING COUNT(department_id) > 2);
SELECT country_id
FROM locations l
WHERE 2 < (
SELECT COUNT(*)
FROM departments d
WHERE l.`location_id` = d.`location_id`
);