子查询和关联查询的优缺点_Part12:SQL复杂查询(视图、子查询、标量子查询、关联子查询)+校园成绩查询...

6ce078a8fefba63a3333edb84ec42f96.png

一.大纲

a902277bd803cf8cd8763facc533c303.png

6fd1299c36415e19b2ae8ef0b7cbb787.png

bad5792fd8ceb3000c52f19e8d00f8f5.png

代码顺序:

16e06c9ab1aaf86d44c715a507fa84a3.png

二、实战

1.创建“按性别汇总”视图

557347b838fd03484ae289dcf5acb682.png

528de9c619ade35b8a37698956e05a61.png
运行结果

1.1 #使用视图查询数据

5d82810f32a06eb3d2b18bdc0b80c943.png
视图查询

2.子查询

2.1找出每个课程里成绩最低的学号

step2:先对课程分组,然后找到每个课程的最小值,最后找到相应的学号

step3:

#先对课程分组,然后找到每个课程的最小值

select min(成绩)

from score

group by 课程号;

#最后根据最低成绩找到相应的学号

select 学号,成绩

from score

where 成绩 in (

select min(成绩)

from score

group by 课程号

);

a1f630cbd607fb8eff55cf223a350f17.png

9d978354a7efc1c4fef6789ac38087e5.png

2.2哪些学生的成绩比课程0002的全部成绩里的任意一个高呢?

step2:找到课程0002里面的成绩,按照成绩使用> any(成绩)找到符合要求的学生学号

step3:

select 学号,成绩

from score

where 成绩 > any(

select 成绩

from score

where 课程号='0002');

b4816a2738e3874c6fa440f5aec711ba.png

2.3哪些学生的成绩比课程0002的全部成绩里都(all)高呢?

ff4219d469757c935790a50aeff6c3b3.png

3 标量子查询

3.1#定义差生是<=60,优等生>80分,要求找出成绩在差生平均成绩和优等生平均成绩之间的学生学号和成绩

step2:先求出差生的平均成绩,和优等生的平均成绩,然后后根据between avg(差生) and avg( 优等生)查询出符合条件的学生学号和成绩

step3:

select 学号,成绩

from score

where 成绩 between (

select avg(成绩)

from score

where 成绩 <=60)

and

(

select avg(成绩)

from score

where 成绩>80);

d2a3d4b71afd677099b8967d6a57f880.png

4 关联子查询

4.1查找每个课程中大于对应课程平均成绩的学生

step2:先求出每门课程的平均分,然后找出成绩高于对应学科平均分的学生

step3:

6effa78bbf450a5323396fbfde843047.png

拓展练习

练习链接:

SELECT within SELECT Tutorial/zh​sqlzoo.net

873fe81a7528da9841076704512b9260.png
SELECT name FROM world
  WHERE population >
     (SELECT population FROM world
      WHERE name='Russia')

2.第二道题题目要找的是国家名字,不是人均gdp

699b130060ca65e6d4e77c7c546217a6.png
select name
from world
where continent='Europe' and
gdp/population >
(
select gdp/population
from world
where name='United Kingdom'
)

a9c59720012ea62216649c6a1cbc6182.png
select name,continent
from world
where continent in
(
select continent
from world
where name='Argentina' or name='Australia'
)
order by name;

6ee800078fcca40a747db019c32a8ac1.png
select name,population
from world
where population between
(select population
from world
where name='Canada'
)+1
and
(select population
from world
where name='Poland'
)-1

bf3b23e90d64307ab5cbc32925bba0c9.png
#此处用concat表示百分数%,round函数*100可以使小数变成整数
select name,concat(
round
(
population/(select population from world where name='Germany')*100,0)
,'%')
from world
where continent='Europe';

a70b51a543eb4c66d33387e0b65fc997.png
select name
from world
where gdp > all(
select gdp
from world
where continent='Europe'and gdp>0);#因为有些gdp是null,如果不加>0,出不了结果

16071debdf8286b2508ee9c39ec5bb07.png
SELECT continent, name, area 
FROM world
WHERE area =any(
select max(area)
from world
where area>0
group by continent
);

0504816df862272f57d932a71f85c917.png
#要求输出每个州按照首字母排序的第一个国家的州和名字。用<=就只能取到第一个国家
select continent,name
from world a
where name <=all(
select name
from world b
where a.continent=b.continent
group by continent
)

bbcba993e9542b1c0fbe2ceaaf5a35de.png
select name,continent,population
from world a
where 25000000>= all(
select population
from world b
where a.continent =b.continent
group by continent
)

439e639c9d131647b43226851394cc17.png
select name,continent
from world a
where population > all
(
select 3*population
from world b
where a.continent=b.continent
and a.name<>b.name
group by continent
)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值