目录
子查询
子查询指一个查询语句嵌套在另一个查询语句内部的查询,这个特性从MySQL 4.1开始引入。
SQL 中子查询的使用大大增强了 SELECT 查询的能力,因为很多时候查询需要从结果集中获取数据,或者
需要从同一个表中先计算得出一个数据结果,然后与这个数据结果(可能是某个标量,也可能是某个集
合)进行比较。
1.引入
需求:谁的工资比Abel的高?
方式1:不推荐!!
SELECT salary
FROM employees
WHERE last_name='Abel';
SELECT last_name,salary
FROM employees
WHERE salary>11000;
方式2:自连接
SELECT last_name,salary
FROM employees e1,employees e2
WHERE e2.`salary`>e1.`salary`#多表的连接条件
AND e1.`last_name`='Abel';
方式3:子查询
SELECT last_name,salary
FROM employees
WHERE salary > (
SELECT salary
FROM employees
WHERE last_name='Abel'
);
2.称谓的规范
外查询(或主查询)、内查询(或子查询)
子查询(内查询)在主查询之前一次执行完成。
子查询的结果被主查询(外查询)使用 。
注意事项
子查询要包含在括号内
将子查询放在比较条件的右侧
单行操作符对应单行子查询,多行操作符对应多行子查询
3.子查询的分类
角度1:从内查询返回的结果的条目数
单行子查询 vs 多行子查询
角度2:内查询是否被执行多次
相关子查询 vs 不相关子查询
比如:相关子查询的需求:查询工资大于本部门平均工资的员工信息。
相关子查询的需求:查询工资大于本公司平均工资的员工信息。
4.单行子查询
4.1单行操作符:= != > >= < <=
题目:查询工资大于149号员工工资的员工的信息
子查询的编写技巧(或步骤):①从里往外写 ②从外往里写
SELECT employee_id,last_name,salary
FROM employees
WHERE salary > (
SELECT salary
FROM employees
WHERE employee_id=149
);
题目:返回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);
题目:返回公司工资最少的员工的last_name,job_id和salary
SELECT last_name,job_id,salary
FROM employees
WHERE salary=
(SELECT MIN(salary)
FROM employees);
题目:查询与141号员工的manager_id和department_id相同的其他员工的employee_id, manager_id,department_id
SELECT employee_id,manager_id,department_id
FROM employees
WHERE (manager_id,department_id)=(SELECT manager_id,department_id
FROM employees
WHERE employee_id=141)
AND employee_id !=141;
4.2 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);
4.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 departments
WHERE location_id=1800) THEN 'Canada'
ELSE 'USA'
END) location
FROM employees;
4.4 子查询中的空值问题
SELECT last_name, job_id
FROM employees
WHERE job_id = (SELECT job_id
FROM employees
WHERE last_name = 'Haas');
4.5 非法使用子查询(=为单行操作符,应该使用in)
SELECT employee_id, last_name
FROM employees
WHERE salary = (SELECT MIN(salary)
FROM employees
GROUP BY department_id);
5.多行子查询
5.1多行子查询的操作符:in any all some(同any)
5.2举例:
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)=(SELECT MIN(avg_sal)
FROM (SELECT AVG(salary) avg_sal
FROM employees
GROUP BY department_id) dept_avgsal);
5.3空值问题
SELECT last_name
FROM employees
WHERE employee_id NOT IN ( SELECT manager_id
FROM employees );
6.相关子查询
题目:查询员工中工资大于本公司平均工资的员工的last_name,salary和其department_id
SELECT last_name,salary,department_id
FROM employees
WHERE salary > (SELECT AVG(salary)
FROM employees
WHERE department_id=60);
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 last_name,salary,e.department_id
FROM employees e,(SELECT department_id,AVG(salary) avg_sal
FROM employees
GROUP BY department_id) dept_avg_sal
WHERE e.`department_id`=dept_avg_sal.department_id
AND e.`salary`>dept_avg_sal.avg_sal;
题目:查询员工的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)ASC;
结论:在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`);
exists
题目:查询公司管理者的employee_id,last_name,job_id,department_id信息
方式1:
SELECT employee_id,last_name,job_id,department_id
FROM employees e1
WHERE EXISTS (SELECT *
FROM employees e2
WHERE e1.`employee_id`=e2.`manager_id`);
方式2:自连接
SELECT DISTINCT e1.employee_id,e1.last_name,e1.job_id,e1.department_id
FROM employees e1 JOIN employees e2
ON e1.`employee_id`=e2.`manager_id`;
方式3:
SELECT employee_id,last_name,job_id,department_id
FROM employees
WHERE employee_id IN(SELECT DISTINCT manager_id
FROM employees);
题目:查询departments表中,不存在于employees表中的部门的department_id和department_name
方式1:
SELECT department_id,department_name
FROM departments d
WHERE NOT EXISTS(SELECT *
FROM employees e
WHERE d.`department_id`=e.`department_id`);
问题:谁的工资比Abel的高?
解答:
#方式1:自连接
SELECT e2.last_name,e2.salary
FROM employees e1,employees e2
WHERE e1.last_name = 'Abel'
AND e1.`salary` < e2.`salary`
#方式2:子查询
SELECT last_name,salary
FROM employees
WHERE salary > ( SELECT salary
FROM employees
WHERE last_name = 'Abel' );
问题:以上两种方式有好坏之分吗?
解答:自连接方式好!
题目中可以使用子查询,也可以使用自连接。一般情况建议你使用自连接,因为在许多 DBMS 的处理过
程中,对于自连接的处理速度要比子查询快得多。
可以这样理解:子查询实际上是通过未知表进行查询后的条件判断,而自连接是通过已知的自身数据表
进行条件判断,因此在大部分 DBMS 中都对自连接处理进行了优化。