当前位置:网站首页>SQL数据分析之子查询的综合用法和案例题【耐心整理】
SQL数据分析之子查询的综合用法和案例题【耐心整理】
2022-07-01 23:58:00 【Chung丶无际鹰】
零、写在前面
本文所有代码均是在SQL ZOO平台进行,数据也该平台下的world表和一些其他平台提供的数据表,所有代码均已通过测试。
一、子查询基础用法
1、说明
子查询本身就是一段完整的查询语句,然后用括号英文括号(包裹嵌套在主查询语包虫,
子查询可以多层嵌套最常用的子查询运用在from和where子句中。
2、查询gdp高于欧洲每个国家的所有国家名,有一些国家gdp可能为null,需要进行排除
select name
from world
where gdp is not null
and gdp>=(select max(gdp) from world where continent='Europe')
注意:记得子查询的select要加括号,而且返回的必须是一个值(max(gdp)),而不是一列值(gdp)
3、查询与澳大利亚Australia和智利Chile同一个大洲的国家和所属大洲
select continent,name
from world
where continent in (select continent from world where name in('Australia','Chile'))
order by continent,name

4、子查询详细解释
(1)子查询是可以自己正常独立运行的一段完整的查询语句,然后将子查询的查询结果作为主查询的一部分,因此子查询优先于主查询运行
(2)练习1是带比较运算符的子查询,要求子查询为标量子查询,即子查询结果为一行- -列(相当于-一个单元格)
(3)练习2是带in关键字的子查询,要求子查询为列子查询,即子查询结果为多行一列(单列)
(4)where子句中的子查询适用于查询条件无法一步到位, 需要先进行一步查询得到结果,基于这个查询结果再进行条件判断的情况,相当于我们无法直达时,需要进行换乘
二、子查询综合用法(难题)
1、查询2017年所有在爱丁堡的选区当选议员所在选区(constituency) 及其团队(party) ,已知爱丁堡选区编号为S14000021至S14000026,当选议员即各选区得票数最高的候选人
第一步:使用窗口函数对各个选区的候选人进行排序
select constituency,party,votes,
rank() over(PARTITION BY consitituency ORDER BY votes desc) as RV
from ge
where constituency between 'S14000021' and 'S14000026' and yr=2017
group by constituency,party,votes
第二步:将窗口函数作为from的子查询,筛选出当前选区得票数最高的候选人(议员)
select RK.constituency,RK.party
from (
select constituency,party,votes,
rank() over(PARTITION BY constituency ORDER BY votes desc) as RV
from ge
where constituency between 'S14000021' and 'S14000026' and yr=2017
group by constituency,party,votes
) as RK
where RK.RV=1

2、查询人口数(population)超过加拿大(Canada)但少于波兰(Poland)的国家,结果显示这些国家的国家名(name)和人口数(population)
select name,population
from world
where population > (
select population from world where name='Canada'
) and population < (
select population from world where name='Poland'
)

3、查询所有国家人口均小于25000000的大洲,及其国家名(name)和人口数(population)
select continent,name,population
from world
where continent not in (
select distinct continent from world where population>25000000
)
order by continent;
重点:所有国家人口均小于,很难判断,但可以很容易把存在有国家大于该population的大洲找出来,再not in这些大洲
4、查询在欧洲(Europe)人均gdp大于英国(United Kingdom)的国家名
select name
from world
where continent='Europe'
and (gdp/population)>(
select (gdp/population) from world where name='United Kingdom'
)

5、查询每个大洲中最大的国家(判断area),显示该大洲(continent),国家名(name)和面积(area)
第一步:将每个大洲的国土面积进行排名
select continent,name,area,
rank() over(PARTITION BY continent ORDER BY area desc) as RA
from world
group by continent,name,area
第二步:把排名好的国家作为from子查询,再筛选出每个大洲排名第一的国家
select RK.continent,RK.name,RK.area
from (
select continent,name,area,
rank() over(PARTITION BY continent ORDER BY area desc) as RA from world
group by continent,name,area) as RK
where RK.RA=1
order by RK.continent;

6、查询法国和意大利每天新增治愈人数并从高到低排名,查询结果按国家名,截至日期(年月*日),新增治愈人数,按排名排序
第一步:查询法国和意大利的国家名,截止日期,每天新增治愈人数(后一天减去前一天)
select name
,date_format(whn,'%Y年%m月%d日') 截止日期
,(recovered-lag(recovered,1) over(PARTITION BY name ORDER BY whn)) 每天新增治愈人数
from covid
where name in ('France','Italy')
group by name,whn,recovered
第二步:将第一步查询结果作为from的子查询,筛选出name,截止日期,每天新增治愈人数,并按照每天新增治愈人数进行排名
select name
,截止日期
,每天新增治愈人数
,rank() over(PARTITION BY RE.name ORDER BY RE.每天新增治愈人数 desc) 排名
from (
select name
,date_format(whn,'%Y年%m月%d日') 截止日期
,(recovered-lag(recovered,1) over(PARTITION BY name ORDER BY whn)) 每天新增治愈人数
from covid
where name in ('France','Italy')
group by name,whn,recovered
) as RE
group by RE.name,RE.截止日期,RE.每天新增治愈人数
order by 排名
写在后面:文章总结归纳于自戴师兄的课程:https://www.bilibili.com/video/BV1ZM4y1u7uF?p=4
在此课程学习的基础上进行了一些修改和验证。
边栏推荐
- [C #] dependency injection and Autofac
- Use vb Net to convert PNG pictures into icon type icon files
- 【模板】自适应辛普森积分
- [es practice] safe operation mode on ES
- Asp .NetCore 微信订阅号自动回复之文本篇
- Which app is better and more secure for stock mobile account opening
- PostgreSQL source code (58) tuple splicing heap_ form_ Tuple analysis
- Shell process control
- 时间复杂度与空间复杂度
- BlocProvider为什么感觉和Provider很相似?
猜你喜欢

Algolia's search needs are almost closed

PWN attack and defense world cgpwn2

比较通俗易懂的PID理解

Selectively inhibiting learning bias for active sampling

Is there a piece of code that makes you convinced by human wisdom

【CMake】Qt creator 里面的 cmake 配置

第六章 数据流建模

Chapter 6 data flow modeling

ARP message header format and request flow

Redis master-slave synchronization
随机推荐
关联性——组内相关系数
13 MySQL constraint
excel如何打开100万行以上的csv文件
[embedded system course design] a single key controls the LED light
Learn online case practice
Selectively inhibiting learning bias for active sampling
const // It is a const object... class nullptr_ t
在证券账户上买基金安全吗?哪里可以买基金
牛客-练习赛101-推理小丑
- Oui. Env. Fichier XXX, avec constante, mais non spécifié
Huawei HMS core joins hands with hypergraph to inject new momentum into 3D GIS
Is it safe to buy funds on Great Wall Securities?
使用htaccess文件禁止目录里的脚本执行权限
Notblank and notempty
[es practice] safe operation mode on ES
回顾数据脱敏系统
How to solve the image pop-up problem when pycharm calls Matplotlib to draw
GCC compilation
Windows10 install WSL (I) (wslregisterdistribution error)
Three methods of finding inverse numbers