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