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
);
MySQL难点语法——子查询的更多相关文章
- MySQL难点语法——连接
本篇涉及的数据表格可以自行查阅上篇<MySQL难点语法——子查询> MySQL的数据表格之间有三种连接方式:等值连接.外连接.自连接.以下是通过举例介绍这三种连接方式 1.等值连接 等值连 ...
- MySQL里面的子查询
一.子查询定义 定义: 子查询允许把一个查询嵌套在另一个查询当中. 子查询,又叫内部查询,相对于内部查询,包含内部查询的就称为外部查询. 子查询可以包含普通select可以包括的任何子句,比如:dis ...
- MySQL中IN子查询会导致无法使用索引
今天看到一个博客园的一篇关于MySQL的IN子查询优化的案例,一开始感觉有点半信半疑(如果是换做在SQL Server中,这种情况是绝对不可能的,后面会做一个简单的测试.)随后动手按照他说的做了一个表 ...
- MySQL中in子查询会导致无法使用索引问题(转)
MySQL的测试环境 测试表如下 create table test_table2 ( id int auto_increment primary key, pay_id int, pay_time ...
- 详细讲述MySQL中的子查询操作 (来自脚本之家)
继续做以下的前期准备工作: 新建一个测试数据库TestDB: ? 1 create database TestDB; 创建测试表table1和table2: ? 1 2 3 4 5 6 7 8 9 1 ...
- MySql学习(三) —— 子查询(where、from、exists) 及 连接查询(left join、right join、inner join、union join)
注:该MySql系列博客仅为个人学习笔记. 同样的,使用goods表来练习子查询,表结构如下: 所有数据(cat_id与category.cat_id关联): 类别表: mingoods(连接查询时作 ...
- 在MySQL中使用子查询和标量子查询的基本用法
一.MySQL 子查询 子查询是将一个 SELECT 语句的查询结果作为中间结果,供另一个 SQL 语句调用.MySQL 支持 SQL 标准要求的所有子查询格式和操作,也扩展了特有的几种特性.子查询没 ...
- mysql 子句、子查询、连接查询
一.mysql查询的五种子句 where子句(条件查询):按照“条件表达式”指定的条件进行查询. group by子句(分组):按照“属性名”指定的字段进行分组.group by子句通常和count( ...
- 数据库之 MySQL --- 数据处理 之 子查询 (二)
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 一 .数据库语言定义及命令行查看数据库操作 -- SQL 语言可以分为三类-- DML: 数据操纵语言. ...
随机推荐
- 个人第2次作业:熟悉使用Git工具
GIT地址 https://github.com/dxg1999 GIT用户名 dxg1999 学号后五位 62317 个人博客 我的博客 作业链接 作业内容 项目作业的整个过程 作业背景 阿超家里的 ...
- jQYERY
1.事件流: (1)事件捕获 (2)处于目标阶段 (3)事件冒泡 2.事件对象 对每一个事件都会回调函数,会有一个默认的事件对象,就是this event.target 触发的目标对象 event.t ...
- 跟着ALEX 学python day2 基础2 模块 数据类型 运算符 列表 元组 字典 字符串的常用操作
声明 : 文档内容学习于 http://www.cnblogs.com/xiaozhiqi/ 模块初始: Python的强大之处在于他有非常丰富和强大的标准库和第三方库,几乎你想实现的任何功能都有相 ...
- 15-numpy笔记-莫烦pandas-3
代码 import pandas as pd import numpy as np dates = pd.date_range('20130101', periods=6) df=pd.DataFra ...
- 初次运行git时的配置
初次运行git时的配置 # 参考文档 https://git-scm.com/book/zh/v2/%E8%B5%B7%E6%AD%A5-%E5%88%9D%E6%AC%A1%E8%BF%90%E8% ...
- NOIP 2005 采药
洛谷 P1048 采药 洛谷传送门 JDOJ 1277: [NOIP2005]采药 T3 JDOJ传送门 Description 辰辰是个天资聪颖的孩子,他的梦想是成为世界上最伟大的医师.为此,他 ...
- python的开发工具pycharm安装及激活
下面介绍一种较好用也常用的python开发工具Pycharm,此文包括安装及注册激活码 一:安装方法如下: 1:进入官网下载:https://www.jetbrains.com/ 2:下载Commun ...
- 实现 Cloneable 需要注意
产品Product里面包含BaseInfo对象:Product(productName,companyName,baseinfo)如果implement Cloneable 需要实现 注意强转类 ...
- [LeetCode] 28. Implement strStr() 实现strStr()函数
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- netcat 传输T级别大文件
接收端 nc -d -l 5002 |tar xf - nohup sh receive.sh & 发送端 tar cf - . | nc 1.1.1.1 5002 nohup ...