#子查询
#需求 谁的工资比Able高
#由一个具体的需求,引入子查询
#方式一
SELECT last_name,salary
FROM employees
WHERE last_name = 'Abel';
SELECT last_name,salary
FROM employees
WHERE salary>11000;
#方式二 自连接
SELECT emp2.last_name,emp2.salary
FROM employees emp1,employees emp2
WHERE emp1.`last_name`='Abel' AND emp2.`salary`>emp1.salary ;#sql92的不等值连接
SELECT emp2.last_name,emp2.salary
FROM employees emp1 JOIN employees emp2
ON emp1.last_name='Abel'AND emp2.`salary`>emp1.`salary`;#sql99的不等值连接
#方式三 子查询 类似一个嵌套for循环
SELECT last_name,salary
FROM employees e
WHERE e.`salary`>(
SELECT salary
FROM employees e
WHERE e.last_name ='Abel'
);
#称谓的规范:外查询(主查询)、内查询(子查询)
/*
子查询(内查询)在主查询之前一次执行完成。
子查询的结果被主查询(外查询)使用 。
注意事项
子查询要包含在括号内
将子查询放在比较条件的右侧
单行操作符对应单行子查询,多行操作符对应多行子查询*/
/*子查询的分类
角度1:从内查询返回的结果的条目数
单行子查询 vs 多行自查询
角度2:内查询是否被执行多次
相关子查询 和 不相关子查询
比如:相关子查询的需求:查询工资大于本部门平均工资的员工信息
不相关子查询的需求:查询工资大于本公司平均工资的员工信息。
*/
#单行子查询
#单行操作符 = != < <= > >= <>
#子查询的编写技巧(或步骤):① 从里往外写 ② 从外往里写
#题目:查询工资大于149号员工工资的员工的信息
SELECT employee_id,last_name,salary
FROM employees e
WHERE e.`salary`>(
SELECT salary
FROM employees e
WHERE e.`employee_id`='149'
);
#题目:返回job_id与141号员工相同,salary比143号员工多的员工姓名,job_id和工资
/*
先查出141号的job_id
查出143号的薪资是多少
在查出job_id与141号员工相同 salary比143号员工多的 员工姓名,job_id和工资
*/
SELECT last_name,job_id,salary
FROM employees e
WHERE e.`job_id`=(
SELECT job_id
FROM employees e
WHERE e.`employee_id`=141
)
AND e.`salary`>(
SELECT salary
FROM employees e
WHERE e.`employee_id`=143
);
#题目:返回公司工资最少的员工的last_name,job_id和salary
SELECT last_name,job_id,salary
FROM employees e
WHERE e.`salary`=(
SELECT MIN(salary)
FROM employees e
);
/*
题目:查询与141号员工的manager_id和department_id相同的其他员工的employee_id,
manager_id,department_id
*/
SELECT employee_id,manager_id,department_id
FROM employees e
WHERE e.`manager_id`=(
SELECT manager_id
FROM employees e
WHERE e.`employee_id`=141
)
AND e.`department_id`=(
SELECT department_id
FROM employees e
WHERE e.`employee_id`=141
)
AND e.`employee_id`<>141;
#方式二:了解
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;
#题目:查询最低工资大于50号部门最低工资的部门id和其最低工资
SELECT department_id,MIN(salary)
FROM employees
GROUP BY department_id
HAVING MIN(salary)>(
SELECT MIN(salary)
FROM employees e
WHERE e.`department_id`=50
);
/*题目:显式员工的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 d WHERE d.location_id=1800)THEN 'canada'
ELSE 'USA'END "location"
FROM employees e;
#空值问题
SELECT last_name
FROM employees
WHERE employee_id NOT IN (
SELECT manager_id
FROM employees
);
#非法使用子查询
#错误 Subquery returns more than 1 row 返回多余一行的记录
SELECT employee_id, last_name
FROM employees
WHERE salary =(SELECT MIN(salary)#多条数据
FROM employees
GROUP BY department_id
);
/*多行子查询
IN 等于列表中的任意一个
ANY 需要和单行比较操作符一起使用,和子查询返回的某一个值比较
ALL 需要和单行比较操作符一起使用,和子查询返回的所有值比较
SOME 实际上是ANY的别名,作用相同,一般常使用ANY
*/
#IN
SELECT employee_id, last_name
FROM employees
WHERE salary IN (SELECT MIN(salary)#多条数据
FROM employees
GROUP BY department_id
);
#ANY
#题目:返回其它job_id中比job_id为‘IT_PROG’部门任一工资低的员工的员工号、姓名、job_id 以及salary
SELECT e.employee_id,e.last_name,e.job_id,e.salary
FROM employees e
WHERE e.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 e.employee_id,e.last_name,e.job_id,e.salary
FROM employees e
WHERE e.`job_id`<> 'IT_PROG'
AND salary <ALL (
SELECT e.salary
FROM employees e
WHERE e.`job_id`='IT_PROG'
);
#题目:查询平均工资最低的部门id
#MySQL中聚合函数不能嵌套使用
SELECT department_id
FROM employees e
GROUP BY e.`department_id`
HAVING AVG(e.`salary`)<=ALL(
SELECT AVG(salary)
FROM employees
GROUP BY department_id
);
#方式二
SELECT department_id
FROM employees e
GROUP BY e.`department_id`
HAVING AVG(e.`salary`)=(
SELECT MIN(asl)
FROM (
SELECT AVG(e.salary) asl
FROM employees e
GROUP BY department_id
) t_avg_sal
);
#多行子查询 空值问题
SELECT last_name
FROM employees
WHERE employee_id NOT IN (
SELECT manager_id
FROM employees
);
#有null参与运算所以上面查询的结果为空
SELECT last_name
FROM employees
WHERE employee_id NOT IN (
SELECT manager_id
FROM employees
WHERE manager_id IS NOT NULL
);
#题目:查询员工中工资大于本部门平均工资的员工的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`
);
#用FROM来解决
SELECT e1.last_name,e1.salary,e1.department_id
FROM employees e1,(
SELECT department_id,AVG(salary) A_sal
FROM employees
GROUP BY department_id
)AS t_sal_avg
WHERE e1.department_id=t_sal_avg.department_id
AND e1.`salary`>t_sal_avg.A_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 *
FROM job_history;
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 与 NOT EXISTS关键字
/*
EXISTS 与 NOT EXISTS关键字
关联子查询通常也会和 EXISTS操作符一起来使用,用来检查在子查询中是否存在满足条件的行。
如果在子查询中不存在满足条件的行:
条件返回 FALSE
继续在子查询中查找
如果在子查询中存在满足条件的行:
不在子查询中继续查找
条件返回 TRUE
*/
#题目:查询公司管理者的employee_id,last_name,job_id,department_id信息
#方式一 自连接
SELECT DISTINCT mrg.employee_id,mrg.last_name,mrg.department_id
FROM employees emp,employees mrg
WHERE emp.`manager_id`=mrg.`employee_id`;
#方式二 子查询
SELECT employee_id,last_name,department_id
FROM employees e
WHERE e.`employee_id`IN(
SELECT DISTINCT manager_id
FROM employees
);
#方式三 使用EXISTS
SELECT DISTINCT employee_id,last_name,department_id
FROM employees e
WHERE EXISTS (
SELECT * #返回的是查询的记录 这里写什么都行
FROM employees e1
WHERE e.employee_id=e1.`manager_id`
);
#题目:查询departments表中,不存在于employees表中的部门的department_id和department_name
#方式一
SELECT DISTINCT d.department_id,d.department_name
FROM employees e RIGHT JOIN departments d
ON e.`department_id`=d.`department_id`
WHERE e.`department_id` IS NULL;
#方式二
SELECT department_id,department_name
FROM departments d
WHERE NOT EXISTS (
SELECT *
FROM employees d1
WHERE d.`department_id`=d1.`department_id`
);
#子查询之课后习题
#1.查询和Zlotkey相同部门的员工姓名和工资
SELECT last_name,salary,department_id
FROM employees e
WHERE e.`department_id`=(
SELECT e.department_id
FROM departments d JOIN employees e
ON e.`department_id`=d.`department_id`
AND e.`last_name`='Zlotkey'
);
#2.查询工资比公司平均工资高的员工的员工号,姓名和工资。
SELECT employee_id,last_name,salary
FROM employees
WHERE salary>(
SELECT AVG(salary)
FROM employees
);
#3.选择工资大于所有JOB_ID = 'SA_MAN'的员工的工资的员工的last_name, job_id, salary
SELECT last_name,job_id,salary
FROM employees e
WHERE e.`salary`>ALL(
SELECT salary
FROM employees e
WHERE e.`job_id`='SA_MAN'
);
#4.查询和姓名中包含字母u的员工在相同部门的员工的员工号和姓名
SELECT employee_id,last_name
FROM employees e
WHERE e.`department_id`IN(
SELECT department_id
FROM employees e
WHERE e.last_name LIKE '%u%'
);
#5.查询在部门的location_id为1700的部门工作的员工的员工号
SELECT last_name,employee_id,location_id
FROM employees e JOIN departments d
ON e.`department_id`=d.`department_id`
WHERE d.`location_id`=1700;
#6.查询管理者是King的员工姓名和工资
SELECT e1.last_name,e1.salary
FROM employees e1
WHERE e1.`manager_id`IN(
SELECT e.employee_id
FROM employees e
WHERE e.`last_name`='King'
);
#方式二 自连接加EXISTS
SELECT
FROM employees e
WHERE EXISTS(
)
#7.查询工资最低的员工信息: last_name, salary
SELECT last_name,salary
FROM employees e
WHERE e.salary=(
SELECT MIN(salary)
FROM employees e
);
#8.查询平均工资最低的部门信息
#方式一
SELECT department_id,AVG(salary)
FROM employees e
GROUP BY department_id
HAVING AVG(salary)<=ALL(
SELECT AVG(e.salary)
FROM employees e
GROUP BY department_id
);
#方式二
SELECT *
FROM departments
WHERE department_id = (
SELECT department_id
FROM employees e
GROUP BY department_id
HAVING AVG(salary)=(
SELECT MIN(avg_sal)
FROM(
SELECT AVG(salary) avg_sal
FROM employees e
GROUP BY department_id
)t_dept_avg_sal
)
);
#方式三 LIMIT
SELECT *
FROM departments d
WHERE d.`department_id`=(
SELECT department_id
FROM employees e
GROUP BY e.`department_id`
HAVING AVG(e.`salary`)=(
SELECT AVG(salary)AS avg_sal
FROM employees e
GROUP BY e.`department_id`
ORDER BY avg_sal ASC
LIMIT 0,1
)
);
#第四种方式
SELECT d.*
FROM departments d,(
SELECT department_id,AVG(salary)AS avg_sal
FROM employees
GROUP BY department_id
ORDER BY avg_sal ASC
LIMIT 0,1
)AS t_d
WHERE d.`department_id`=t_d.department_id;
#9.查询平均工资最低的部门信息和该部门的平均工资(相关子查询)
SELECT d.*,AVG(e.`salary`)AS avg_sal
FROM departments d JOIN employees e
ON d.`department_id`=e.`department_id`
WHERE d.`department_id`=(
SELECT department_id
FROM employees e
GROUP BY e.`department_id`
HAVING AVG(e.`salary`)=(
SELECT MIN(avg_sal)
FROM (
SELECT AVG(salary)AS avg_sal
FROM employees e
GROUP BY e.`department_id`
)AS t_avg_s
)
);
#10.查询平均工资最高的 job 信息
SELECT job_id,salary
FROM employees e
GROUP BY e.`job_id`
HAVING MAX(salary)=(
SELECT MAX(salary)AS max_sal
FROM employees e
GROUP BY job_id
ORDER BY max_sal DESC
LIMIT 0,1
);
#方式二
SELECT *
FROM jobs
WHERE job_id=(
SELECT job_id
FROM employees e
GROUP BY job_id
HAVING AVG(salary)=(SELECT MAX(avg_sal)
FROM (
SELECT AVG(salary)AS avg_sal
FROM employees e
GROUP BY e.`job_id`
)AS t_avg
)
);
#方式三
SELECT *
FROM jobs j
WHERE j.`job_id`=(
SELECT job_id
FROM employees
GROUP BY job_id
HAVING AVG(salary)>=ALL(#大于等于取最大值
SELECT AVG(salary)
FROM employees e
GROUP BY e.`job_id`
)
)
#第四种方式
SELECT jobs.*
FROM jobs JOIN (
SELECT job_id,AVG(salary)
FROM employees
GROUP BY job_id
ORDER BY salary DESC
LIMIT 0,1#取出salary最大的一条记录 在from里充当表结构 和jobs表连接 因为此时表中只有一条记录
)t_avgta
ON jobs.`job_id`=t_avgta.job_id
#11.查询平均工资高于公司平均工资的部门有哪些?
SELECT department_id,AVG(salary)
FROM employees e
WHERE e.`department_id` IS NOT NULL
GROUP BY department_id
HAVING AVG(salary)>(
SELECT AVG(salary)
FROM employees e
);
#12.查询出公司中所有 manager 的详细信息
#方式一:自连接
SELECT DISTINCT e2.employee_id,e2.last_name,e2.`job_id`,e2.department_id
FROM employees e1,employees e2
WHERE e1.`manager_id`=e2.`employee_id`;
#方式二:子查询
SELECT last_name,employee_id,salary,job_id
FROM employees e
WHERE e.`employee_id`IN (
SELECT DISTINCT manager_id
FROM employees
);
#方式三 EXISTS
SELECT last_name,employee_id,salary,job_id
FROM employees e
WHERE EXISTS(
SELECT DISTINCT manager_id
FROM employees e2
WHERE e.`employee_id`=e2.`employee_id` #从第一层select语句进入一条记录匹配二层select所有记录 记录匹配则打印
);
#13.各个部门中 最高工资中最低的那个部门的最低工资是多少?
#方式一
SELECT MIN(salary)
FROM employees e
WHERE e.`department_id`=(
SELECT department_id
FROM employees e
GROUP BY e.`department_id`
HAVING MAX(e.`salary`)=(
SELECT MAX(salary)AS max_sal
FROM employees e
GROUP BY e.`department_id`
ORDER BY max_sal ASC
LIMIT 0,1
)
);
#方式二
SELECT MIN(salary)
FROM employees e
WHERE e.`department_id`=(
SELECT department_id
FROM employees
GROUP BY department_id
HAVING MAX(salary)=(SELECT MIN(max_sal)#最底工资的部门和每个部门的最高工资匹配等于返回部门id
FROM(
SELECT MAX(salary)AS max_sal
FROM employees
GROUP BY department_id
)AS t_tables_max
)
);
#方式三
SELECT MIN(salary)
FROM employees e
WHERE e.`department_id`=(
SELECT department_id
FROM employees
GROUP BY department_id
HAVING MAX(salary)<=ALL(#小于等于最小的前提是都是相同的聚合函数的值
SELECT MAX(salary)
FROM employees
GROUP BY department_id
)
);
#方式四
SELECT MIN(salary)
FROM employees e,(
SELECT department_id,MAX(salary)AS max_sal
FROM employees
GROUP BY department_id
ORDER BY max_sal ASC
LIMIT 0,1
) AS t_desal
WHERE e.department_id=t_desal.department_id
#14.查询平均工资最高的部门的 manager 的详细信息: last_name, department_id, email, salary
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 last_name, department_id, email, salary
FROM employees e
WHERE e.`employee_id`IN(#匹配
SELECT employee_id#返回员工id
FROM employees e
GROUP BY department_id
HAVING AVG(e.salary)=(
SELECT MAX(avg_sal)
FROM (
SELECT AVG(salary)AS avg_sal
FROM employees
GROUP BY department_id
)AS t_avg_sal
)
);
#方式三
SELECT last_name, department_id, email, salary
FROM employees e
WHERE e.`employee_id`=(
SELECT employee_id
FROM employees e
GROUP BY department_id
HAVING AVG(e.`salary`)=(
SELECT AVG(salary)AS avg_sal
FROM employees e
GROUP BY department_id
ORDER BY avg_sal DESC
LIMIT 0,1
)
);
#方式四
SELECT last_name, department_id, email, salary
FROM employees
WHERE employee_id IN(
SELECT manager_id
FROM employees e,(
SELECT department_id,AVG(salary)AS asl
FROM employees
GROUP BY department_id
ORDER BY asl DESC
LIMIT 0,1
)AS t_dept_avg_sal
WHERE e.`department_id`=t_dept_avg_sal.department_id
);
#15. 查询部门的部门号,其中不包括job_id是"ST_CLERK"的部门号
SELECT DISTINCT department_id,job_id
FROM employees
WHERE job_id <>'ST_CLERK' AND department_id IS NOT NULL
#方式二
SELECT DISTINCT department_id,job_id
FROM employees e
WHERE e.department_id !=(
SELECT DISTINCT department_id
FROM employees
WHERE job_id='ST_CLERK'
);
#方式三
SELECT department_id,job_id
FROM employees e
WHERE NOT EXISTS (#第一层select进去一条记录 在第二层匹配所有数据 不满足条件返回
SELECT department_id
FROM employees e2
WHERE e.`department_id`=e2.`department_id`
AND e2.job_id='ST_CLERK'
)
#16. 选择所有没有管理者的员工的last_name
SELECT last_name
FROM employees
WHERE manager_id IS NULL;
#方式二
SELECT last_name
FROM employees e
WHERE NOT EXISTS (
SELECT *
FROM employees e2
WHERE e.`manager_id`=e2.`employee_id`
);
#17.查询员工号、姓名、雇用时间、工资,其中员工的管理者为 'De Haan'
SELECT employee_id,last_name,hire_date,salary
FROM employees e
WHERE e.last_name IN(
SELECT emp.last_name
FROM employees emp JOIN employees mrg
ON emp.`manager_id`=mrg.`employee_id`
AND mrg.last_name='De Haan'
);
#方式二
SELECT employee_id,last_name,hire_date,salary
FROM employees e
WHERE e.`manager_id`IN(
SELECT employee_id
FROM employees e
WHERE e.`last_name`='De Haan'
)
#方式三
SELECT employee_id,last_name,hire_date,salary
FROM employees e
WHERE EXISTS (
SELECT employee_id
FROM employees e2
WHERE e.`manager_id`=e2.`employee_id`
AND e2.last_name='De Haan'
)
#18.查询各部门中工资比本部门平均工资高的员工的员工号, 姓名和工资(相关子查询)
SELECT employee_id,last_name,salary
FROM employees e1
WHERE e1.`salary`>(#员工工资比部门平均工资高
SELECT AVG(salary)
FROM employees e
WHERE e1.`department_id`=e.`department_id`#匹配 返回 部门平均值
)
#方式二
SELECT employee_id,last_name,salary
FROM employees e1
WHERE salary > (
# 查询某员工所在部门的平均
SELECT AVG(salary)
FROM employees e2
WHERE e2.department_id = e1.`department_id`
);
#方式三
SELECT employee_id,last_name,salary
FROM employees e1,
(
SELECT department_id,AVG(salary) avg_sal
FROM employees e2 GROUP BY department_id
) dept_avg_sal
WHERE e1.`department_id` = dept_avg_sal.department_id
AND e1.`salary` > dept_avg_sal.avg_sal;
#19.查询每个部门下的部门人数大于 5 的部门名称(相关子查询)
SELECT department_name
FROM departments d
WHERE 5>(
SELECT COUNT(*)
FROM employees e
WHERE d.`department_id`=e.`department_id`
);
#20.查询每个国家下的部门个数大于 2 的国家编号(相关子查询)
SELECT country_id
FROM locations l
WHERE 2<(
SELECT COUNT(*)
FROM departments d
WHERE l.`location_id`=d.`location_id`
);
/*
子查询的编写技巧(或步骤):① 从里往外写 ② 从外往里写
如何选择?
① 如果子查询相对较简单,建议从外往里写。一旦子查询结构较复杂,则建议从里往外写
② 如果是相关子查询的话,通常都是从外往里写。
*/
|