当前位置:网站首页>零基础自学SQL课程 | 相关子查询
零基础自学SQL课程 | 相关子查询
2022-06-24 07:58:00 【喵宁一】
大家好,我是宁一。
今天是我们的第19课:相关子查询。
上节课我们讲的子查询,都是先一次性得出子查询的结果,再返回给主查询使用。这种子查询与主查询之间是没有关联,互不影响的。
但在相关子查询中,子查询是在主查询每一条记录层面上依次进行的,子查询依赖主查询。
相关子查询比非关联查询执行起来慢一些。但是有很多实际的应用。
实例:选出Scores成绩表中,学生编号在1~6之间的记录,找出那些超过相同科目平均成绩的学生成绩。

实例解析:要注意的是,这里我们需要知道是当前科目的平均成绩。所以使用子查询计算当前科目平均成绩的时候,要知道此时主查询的课程编号Cid是什么。
我们给主查询Scores表设置别名s,这样在子查询当前科目平均成绩的时候,加上WHERE Cid = s.Cid ,就能获得当前课程的平均成绩。
SELECT *
FROM Scores s -- 将主查询的字段加上别名
WHERE score > (
SELECT AVG(score)
FROM Scores
WHERE Cid = s.Cid -- 获取当前主查询的的课程编号Cid
)
AND Sid BETWEEN 1 AND 6;
作业:通过SId连接Scores表和Students表,Cid连接Scores表和Courses表,选出学生编号在1~6之间,那些成绩超过相同科目平均成绩的学生。
最终得到Students表的Sname列,Courses表的Cname列以及Scores表中的score列。
示例结果:

作业解析:其实就是在我们课程实例中,加上两个JOIN连接,需要注意的是,加上JOIN连接后,使用字段时要加上表的别名。比如Sid BETWEEN 1 AND 6中的Sid,要写成s.Sid。
SELECT st.Sname,c.Cname,s.score
FROM Scores s
JOIN Students st
ON s.Sid = st.Sid
JOIN Courses c
ON s.Cid = c.Cid
WHERE score > (
SELECT AVG(score)
FROM Scores
WHERE Cid = s.Cid
)
AND s.Sid BETWEEN 1 AND 6;下节课讲讲SQL数值函数和字符函数。
点击关注,更新课程第一时间通知哦~
边栏推荐
- Yolox backbone -- implementation of cspparknet
- Numpy NP in numpy c_ And np r_ Explain in detail
- 学习太极创客 — ESP8226 (十三)OTA
- Recommendation - Secret of curiosity: how many dancing angels can stand on the tip of a needle?
- 从华为WeAutomate数字机器人论坛,看政企领域的“政务新智理”
- MySQL——SQL语句
- When programmers are asked if they can repair computers... | daily anecdotes
- 110. balanced binary tree recursive method
- Depens:*** but it is not going to be installed
- 学习太极创客 — ESP8226 (十二)ESP8266 多任务处理
猜你喜欢

EasyExcel单sheet页与多sheet页写出

【使用 PicGo+腾讯云对象存储COS 作为图床】

From the Huawei weautomate digital robot forum, we can see the "new wisdom of government affairs" in the field of government and enterprises

4274. suffix expression
![The printed object is [object object]. Solution](/img/fc/9199e26b827a1c6304fcd250f2301e.png)
The printed object is [object object]. Solution

当程序员被问会不会修电脑时… | 每日趣闻

4275. Dijkstra序列
![[ES6 breakthrough] promise is comparable to native custom encapsulation (10000 words)](/img/b3/b156d75c7b4f03580c449f8499cd74.png)
[ES6 breakthrough] promise is comparable to native custom encapsulation (10000 words)
Depens:*** but it is not going to be installed

【LeetCode】415. 字符串相加
随机推荐
Linux MySQL installation
MBA-day25 最值问题-应用题
Qingcloud based "real estate integration" cloud solution
Kaformer personal notes
浮点数表示法(总结自CS61C和CMU CSAPP)
Project deployment related
当程序员被问会不会修电脑时… | 每日趣闻
嵌入式 | 硬件转软件的几条建议
2138. splitting a string into groups of length k
MySQL | store notes of Master Kong MySQL from introduction to advanced
The printed object is [object object]. Solution
RISC-V架构下 FPU Context 的动态保存和恢复
关于 GIN 的路由树
Spark - LeftOuterJoin 结果条数与左表条数不一致
【LeetCode】387. 字符串中的第一个唯一字符
Data midrange: analysis of full stack technical architecture of data midrange, with industry solutions
Huawei Router: IPSec Technology
从华为WeAutomate数字机器人论坛,看政企领域的“政务新智理”
pm2 部署 nuxt3.js 项目
Data middle office: the data middle office practice scheme of Minsheng Bank