MySQL难点语法——子查询
本篇主要通过练习来讲解子查询的知识,进入正题之前,先熟悉数据表,表格的数据可以先不用管,主要是熟悉表格的字段名
这里子查询分为三个部分:
1、where条件子查询
这个子查询和普通的查询没什么区别,主要是先读懂题目的意思,然后将结果集组装起来
需求:1.查看职员名称和名字为chang的员工一样的所有员工的id和名字
select id, last_name, title from s_emp where title=( # 和chang员工一样的职位 select title from s_emp where last_name = "chang" );
需求:2.查看员工工资小于平均工资的所有员工的id和名字
select id, last_name, salary from s_emp where salary<( # 员工的平均工资 select avg(salary) from s_emp );
需求:3.查看部门与员工名字为Chang的员工所在部门相同,或者与区域为2的部门相同的部门所有员工的id和 名字
select id, last_name, title, dept_id from s_emp where dept_id = ( # chang员工所在的部门 select dept_id from s_emp where last_name = "chang" ) or dept_id in ( # 区域为2的部门 select dept_id from s_dept where region_id=2 );
需求:4.查看部门平均工资大于32号部门平均工资的部门id
select dept_id,salary from s_emp group by dept_id having avg(salary)>( # 32部门的平均工资 select avg(salary) from s_emp where dept_id = 32 );
需求:5.查看工资大于Smith所在部门平均工资的员工id和姓名
select id, last_name from s_emp where salary>( # smith所在的部门的平均工资 select avg(salary) from s_emp where dept_id = ( # smith所在的部门 select dept_id from s_emp where last_name = "Smith" ) );
需求:6.查看薪资高于Chang员工经理薪资的员工信息
select id, last_name, salary from s_emp where salary>( #chang员工的经理的工资 select salary from s_emp where id=( # chang员工的经理 select manager_id from s_emp where last_name = "chang" ) );
需求:7.查看薪资高于(Chang员工经理的经理所在区域的)最低工资的员工的信息
select id, last_name, salary from s_emp where salary>( select salary from s_emp where dept_id in ( # chang员工的经理的经理所在的部门的所在区域的所有部门 select id from s_dept where region_id = ( # chang员工的经理的经理所在的部门的所在区域 select region_id from s_dept where id=( # chang员工的经理的经理所在的部门 select dept_id from s_emp where id=( # chang员工的经理的经理 select manager_id from s_emp where id=( # chang员工的经理 select manager_id from s_emp where last_name="chang" ) ) ) ) ) order by salary limit 0,1 );
需求:8.查看所有客户负责员工的总工资
select sum(salary) from s_emp where id in ( # 员工负责的id select sales_rep_id from s_customer );
需求:9.查看工资大于客户负责员工最高工资的员工信息
select id, last_name from s_emp where salary>( # 员工负责的id中工资最高的薪资 select max(salary) from s_emp where id in ( # 员工负责的id select sales_rep_id from s_customer ) );
需求:10.查看客户负责员工中工资大于Chang员工的工资的员工信息
select id, last_name, salary from s_emp where id in ( # 员工负责的id select sales_rep_id from s_customer ) and salary > ( # 工资大于chang员工的工资 select salary from s_emp where last_name="chang" );
需求:11.查看部门平均工资大于Chang所在部门平均工资的部门id
select dept_id, avg(salary) from s_emp group by dept_id having avg(salary) > ( # chang员工所在部门的平均工资 select avg(salary) from s_emp where dept_id=( # chang员工所在部门 select dept_id from s_emp where last_name="chang" ) );
需求:12.查看Chang员工所在部门其他员工薪资总和
select sum(salary) from s_emp where dept_id=( # chang员工所在的部门 select dept_id from s_emp where last_name="chang" ) and last_name != "chang"; # 除了chang员工
需求:13.查询工资大于41号部门平均工资的员工,并且该员工所在部门的平均工资也要大于41号部门的平均工资
select id,last_name,salary, dept_id from s_emp where salary > ( # 高于41号部门的平均工资 select avg(salary) from s_emp where dept_id = 41 ) and dept_id in ( # 高于41号部门的平均工资的部门 select dept_id from s_emp group by dept_id having avg(salary)>( # 41号部门的平均工资 select avg(salary) from s_emp where dept_id = 41 ) );
2、数据集条件子查询
需求:1.求平均薪水最高的部门的id
select dept_id from s_emp group by dept_id having avg(salary) = ( # 部门平均工资表中的最大工资 select max(max_avg) from ( # 部门平均工资表 select avg(salary) as max_avg from s_emp group by dept_id ) as newtable ); # 排序搜寻的部门 select dept_id from s_emp group by dept_id order by avg(salary) desc limit 0, 1;
需求:2.求平均薪水最高的部门的部门名称
select id, name from s_dept where id=( # 部门平均薪资表中最高的平均工资的部门 select dept_id from s_emp group by dept_id having avg(salary) = ( # 部门平均薪资表中最高的平均工资 select max(avgs) from ( # 部门平均薪资表 select avg(salary) avgs from s_emp group by dept_id ) as newtable ) ); select id, name from s_dept where id=( select dept_id from s_emp group by dept_id order by avg(salary) desc limit 0, 1 );