当前位置:网站首页>零基础自学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数值函数和字符函数。
点击关注,更新课程第一时间通知哦~
边栏推荐
- 玄铁E906移植----番外0:玄铁C906仿真环境搭建
- Data middle office: a collection of middle office construction architectures of large domestic factories
- Essay - Reflection
- [quantitative investment] discrete Fourier transform to calculate array period
- 学习太极创客 — ESP8226 (十二)ESP8266 多任务处理
- Squid代理服务器应用
- Mba-day25 best value problem - application problem
- Alibaba Senior Software Testing Engineer recommends testers to learn -- Introduction to security testing
- What do you mean by waiting for insurance records? Where should I go for filing?
- Sword finger offer 55 - I. depth DFS method of binary tree
猜你喜欢

Squid代理服务器应用

小白学习MySQL - 增量统计SQL的需求

Solution: the word of jmeter5.5 on the win11 lower interface is very small

“论解不了数独所以选择做个数独游戏这件事”
![[Niuke] length of the last word of HJ1 string](/img/8b/6ba6506415b8112aea957ac5647121.png)
[Niuke] length of the last word of HJ1 string

Groovy通过withCredentials获取Jenkins凭据
![[MySQL from introduction to mastery] [advanced part] (I) character set modification and underlying principle](/img/db/e581087e550a2e460f12047685c48f.png)
[MySQL from introduction to mastery] [advanced part] (I) character set modification and underlying principle

CF566E-Restoring Map【bitset】

读CVPR 2022目标检测论文得到的亿点点启发

uniapp 开发多端项目如何配置环境变量以及区分环境打包
随机推荐
Array opposite pointer series
Common emoticons
Get post: do you really know the difference between requests??????
Epidemic situation, unemployment, 2022, we shouted to lie down!
leetcode——错误的集合
Data middle office: detailed explanation of technical architecture of data middle office
【ES6闯关】Promise堪比原生的自定义封装(万字)
Ebanb B1 Bracelet brush firmware abnormal interrupt handling
2022-06-23: given a nonnegative array, select any number to make the maximum cumulative sum a multiple of 7, and return the maximum cumulative sum. N is larger, to the 5th power of 10. From meituan. 3
Cmake命令之target_compile_options
1528. rearrange strings
Spark - the number of leftouterjoin results is inconsistent with that of the left table
解决:jmeter5.5在win11下界面上的字特别小
[noi Simulation Competition] geiguo and time chicken (structure)
【牛客】把字符串转换成整数
【LeetCode】387. 字符串中的第一个唯一字符
Opencv maximum filtering (not limited to images)
Huawei Router: IPSec Technology
"I can't understand Sudoku, so I choose to play Sudoku."
On the routing tree of gin