本篇主要通过练习来讲解子查询的知识,进入正题之前,先熟悉数据表,表格的数据可以先不用管,主要是熟悉表格的字段名

这里子查询分为三个部分:

  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
);

MySQL难点语法——子查询的更多相关文章

  1. MySQL难点语法——连接

    本篇涉及的数据表格可以自行查阅上篇<MySQL难点语法——子查询> MySQL的数据表格之间有三种连接方式:等值连接.外连接.自连接.以下是通过举例介绍这三种连接方式 1.等值连接 等值连 ...

  2. MySQL里面的子查询

    一.子查询定义 定义: 子查询允许把一个查询嵌套在另一个查询当中. 子查询,又叫内部查询,相对于内部查询,包含内部查询的就称为外部查询. 子查询可以包含普通select可以包括的任何子句,比如:dis ...

  3. MySQL中IN子查询会导致无法使用索引

    今天看到一个博客园的一篇关于MySQL的IN子查询优化的案例,一开始感觉有点半信半疑(如果是换做在SQL Server中,这种情况是绝对不可能的,后面会做一个简单的测试.)随后动手按照他说的做了一个表 ...

  4. MySQL中in子查询会导致无法使用索引问题(转)

    MySQL的测试环境 测试表如下 create table test_table2 ( id int auto_increment primary key, pay_id int, pay_time ...

  5. 详细讲述MySQL中的子查询操作 (来自脚本之家)

    继续做以下的前期准备工作: 新建一个测试数据库TestDB: ? 1 create database TestDB; 创建测试表table1和table2: ? 1 2 3 4 5 6 7 8 9 1 ...

  6. MySql学习(三) —— 子查询(where、from、exists) 及 连接查询(left join、right join、inner join、union join)

    注:该MySql系列博客仅为个人学习笔记. 同样的,使用goods表来练习子查询,表结构如下: 所有数据(cat_id与category.cat_id关联): 类别表: mingoods(连接查询时作 ...

  7. 在MySQL中使用子查询和标量子查询的基本用法

    一.MySQL 子查询 子查询是将一个 SELECT 语句的查询结果作为中间结果,供另一个 SQL 语句调用.MySQL 支持 SQL 标准要求的所有子查询格式和操作,也扩展了特有的几种特性.子查询没 ...

  8. mysql 子句、子查询、连接查询

    一.mysql查询的五种子句 where子句(条件查询):按照“条件表达式”指定的条件进行查询. group by子句(分组):按照“属性名”指定的字段进行分组.group by子句通常和count( ...

  9. 数据库之 MySQL --- 数据处理 之 子查询 (二)

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 一 .数据库语言定义及命令行查看数据库操作 -- SQL 语言可以分为三类-- DML: 数据操纵语言. ...

随机推荐

  1. Ubuntu 16.04/18.04 安装Shutter截图工具

    在安装Shutter软件之前,先安装依赖包,下载地址: 1.libgoocanvas-common_1.0.0-1_all.deb                 http://launchpadli ...

  2. Android TextField : set focus + soft input programmatically

    Good sir, try this: edittext.setFocusableInTouchMode(true); edittext.requestFocus(); Im not sure, bu ...

  3. 【作用域】词法作用域(静态作用域,如:js)、动态作用域(如:.bash脚本)

    作用域 作用域是指程序源代码中定义变量的区域. 作用域规定了如何查找变量,也就是确定当前执行代码对变量的访问权限. JavaScript 采用词法作用域(lexical scoping),也就是静态作 ...

  4. pointnet++的pytorch实现

    代码参考:https://blog.csdn.net/weixin_39373480/article/details/88934146 def recognize_all_data(test_area ...

  5. websocket广播式实例

    1.引入相关依赖 <parent> <groupId>org.springframework.boot</groupId> <artifactId>sp ...

  6. 2,[VS入门教程] 使用Visual Studio写c语言 入门与技巧精品文~~~~优化篇

    本文导航: 关闭界面特效以提高流畅度 解决调试时出现"无法查找或打开PDB文件"的符号问题 注册微软账号并在vs登录 使用Visual Studio Team Services,同 ...

  7. 2019 年 Java 最新面试指南共 80 题,赶快收藏起来吧!

    2019 年 Java 最新面试指南共 80 题,赶快收藏起来吧! http://blog.zh66.club/index.php/archives/116/

  8. Python 脚本如何执行另一个脚本

    关于Python 脚本如何执行另一个脚本,可以使用os.system()来实现 os.system()的参数: 执行的命令 +执行的内容 举例说明: (1)显示当前文件夹下的全部目录和文件夹 os.s ...

  9. MySQL常用系统表汇总

    在这篇文章中: MySQL5.7 默认模式 Information_schema performance_schema mysql sys MYSQL SHOW 命令 致谢 概述 本篇文章虽大部分内容 ...

  10. 趣文:如何通过给MM修电脑培养感情[转]

    在修之前,向 MM 反复声明,这电脑故障是有硬件和软件之分的,如果是硬件故障,例如显卡风扇不转了,显示器连线老化,显示器分辨率超出显示器指标,等等都会导致黑屏啊,这个我不回家用专门的工具是修不好的! ...