3.4.3 嵌套查询
3.4.3 嵌套查询
一.带有in谓词 的 子查询
用自身连接完成 .note
select s1.sno, s1.sname, s1.sdept
from student s1, student s2
where s1.sdept=s2.sdept and s2.sname='刘晨'
student 表
39 查询 与 刘晨 在同一个系 学习的学生.note
假设 一个学生 只可能在一个系,,并且 属于一个系
此时 in 可用 =
select sno, sname, sdept from student
where sdept =(
select sdept from student where sname='刘晨' )
40 查询 选修了 课程名为 信息系统 的 学生 学号 和 姓名.note
select sno,sname
from student
where sno in
(
select sno
from sc
where cno in
(
select cno
from course
where cname='信息系统' )
)
1. course 表 '信息系统'的 课程
2 sc 表 选修3号课程 的 学生学号
3 student 表 取出 sno sname
用 连接查询 40.note
select student.sno, sname
from student, sc , course
where
student.sno=sc.sno and
course.cno=sc.cno and
course.cname='信息系统'
查询 选修了 2号课程 的 学生 的 姓名
select sname
from student
where sno
in(
select sno
from sc
where cno='2'
)
二.不相关 子查询
39 查询 与 刘晨 在同一个系 学习的学生 不相关 子查询.note
假设 一个学生 只可能在一个系,,并且 属于一个系
此时 in 可用 =
select sno, sname, sdept from student
where sdept =(
select sdept from student where sname='刘晨' )
p104 查询 选修了 2号课程 的 学生 的 姓名.note
40 查询 选修了 课程名为 信息系统 的 学生 学号 和 姓名.note
select sno,sname
from student
where sno in
(
select sno
from sc
where cno in
(
select cno
from course
where cname='信息系统' )
)
1. course 表 '信息系统'的 课程
2 sc 表 选修3号课程 的 学生学号
3 student 表 取出 sno sname
嵌套 39.note
1.
select sdept
from student
where sname='刘晨'
2.
select sno, sname , sdept
from student
where sdept
in('cs')
法(二 39.note
由里向外
1. 先执行 子查询
select sno, sname , sdept
from student
where sdept='cs'
2.
select sno, sname , sdept
from student
where sdept
in(
'cs'
)
三.相关子查询
41 找出每个学生 超过 他 选修课程 平均成绩 的 课程号.note
select sno, cno
from sc x
where grade>=
(
select avg(grade)
from sc y
where y.sno=x.sno
)
四.带有any all谓词的 子查询
42 某一个 学生 .note
select sname, sage
from student
where sage<any
(
select sage
from student
where sdept='cs'
)
and sdept <> 'cs'
43 所有学生.note
select sname, sage
from student
where sage<all
(
select sage
from student
where sdept='cs'
)
and sdept <> 'cs'
用聚集函数实现 42.note
select sname, sage
from student
where sage<
(
select max(sage)
from student
where sdept='cs'
)
and sdept <> 'cs'
聚集函数 43.note
46 查询 选修了全部课程的 学生姓名.note
select sname
from student
where not exists(
select *
from course
where not exists
(
select *
from sc
where sno=student.sno and cno=course.cno
)
)
五.带有exist谓词 的子查询
嵌套查询 44 查询 所有选修了 1号课程的 学生 姓名.note
select sname
from student
where exists
(
select *
from sc
where sc.sno=student.sno and cno='1'
)
连接运算 44.note
select sname
from student,sc
where sc.sno=student.sno and cno='1'
45 查询 没有 选修了 1号课程的 学生 姓名.note
select sname
from student
where not exists
(
select *
from sc
where sc.sno=student.sno and cno='1'
)
39 查询 与 刘晨 在同一个系 学习的学生 不相关 子查询.note
假设 一个学生 只可能在一个系,,并且 属于一个系
此时 in 可用 =
select sno, sname, sdept from student
where sdept =(
select sdept from student where sname='刘晨' )
exist 39 .note
六.集合查询
(一)union 交 or 或
48 .note
select *
from student
where sdept='cs' or sage<=19
48 union 并 or.note
查询 计算机系 的 学生 及 年龄不大于 19岁的 学生
select *
from student
where sdept='cs'
union
select *
from student
where sage<=19
49 .note
查询 选修了 1号课程 或 2号 课程的学生
select sno
from sc
where cno='1'
union
select sno
from sc
where cno='2'
(二).intersect 并 and 和
MySQL不支持INTERSECT.note
MySQL不支持INTERSECT
50 查询 计算机系 的 学生 与 年龄不大于 19岁的 学生.note
select *
from student s1
where s1.sdept='cs' and
exists(
select *
from student s2
where s2.sage<=19 and s1.sno=s2.sno
)
exists 51.note
select sc1.sno
from sc sc1
where sc1.cno='1' and exists
( select sno
from sc sc2
where sc1.sno=sc2.sno and sc2.cno='2'
)
in 51 查询 选修了 1号课程 和 2号 课程的学生.note
select sno
from sc
where cno='1' and sno in
( select sno
from student
where cno='2'
)
(三)except 差 减
52 查询 计算机系中 年龄大于 19岁的 学生.note
查询 计算机系中 年龄大于 19岁的 学生
select *
from student
where sdept='cs' and sage>19
MySQL不支持 except.note
MySQL不支持 except
52 查询 计算机系中 的学生 与 年龄 不大于 19岁的 学生 的 差集.note
select *
from student s1
where s1.sdept='cs' and not exists
( select *
from student s2
where s2.sage<=19 and s1.sno=s2.sno
)
3.4.3 嵌套查询相关推荐
- SQL学习笔记-嵌套查询
嵌套查询 定义: 1 .指在一个外层查询中包含有另一个内层查询.其中外层查询称为主查询,内层查询称为子查询. 2 .SQL允许多层嵌套,由内而外地进行分析,子查询的结果作为主查询的查询条件 3 .子查 ...
- sql镶嵌查询_sql数据库的嵌套查询
实验四:数据库的嵌套查询实验 学号: 姓名: 实验四:数据库的嵌套查询实验 实验目的: 加深对嵌套查询语句的理解. 实验内容: 使用 IN . 比较符. ANY 或 ALL 和 EXISTS 操作符进 ...
- SQL学习笔记 where子句用法,like关键字 嵌套查询
where子句 比较运算符: < , <= , > , >= , = , != , !< , !> . text , ntext , image .型数据不可用. ...
- ElasticSearch嵌套查询以及注意事项
2019独角兽企业重金招聘Python工程师标准>>> es实体 @Data @Document(indexName = "indexdemo", type = ...
- SQL语句 - 嵌套查询
嵌套查询的意思是,一个查询语句(select-from-where)查询语句块可以嵌套在另外一个查询块的where子句中,称为嵌套查询.其中外层查询也称为父查询,主查询.内层查询也称子查询,从查询. ...
- SQL连接查询和嵌套查询详解
连接查询 若一个查询同时涉及两个或两个以上的表,则称之为连接查询.连接查询是数据库中最最要的查询, 包括: 1.等值连接查询 2.自然连接查询 3.非等值连接查询 4.自身连接查询 5.外连接查询 6 ...
- java构造方法嵌套,laravel查询构建器中的嵌套查询
我需要根据请求ID获得1个帖子,结构如下: postId; postTitle; postContent; postImage; bandName; genreName; 标签:[tagId,tagN ...
- SQL Sever 子查询与嵌套查询
数据库表 1.带 in 的嵌套查询 查询Student表 并且 Sno 在 SC表中有 select * from Student where Sno in(select Sno from SC) 2 ...
- 优化嵌套查询 || 优化OR条件
优化嵌套查询 Mysql4.1版本之后,开始支持SQL的子查询. 这个技术可以使用SELECT语句来创建一个单列的查询结果, 然后把这个结果作为过滤条件用在另一个查询中. 使用子查询可以一次性的完成很 ...
最新文章
- [转]Windows与VC命名规则
- linux 提取字符串一部分,Linux Shell 截取字符串的方法示例
- [How TO]-Ubuntu 20.04修改终端标题栏文字
- 我的数据分析全系列教程,记录着那些大学奋斗的时光
- [转]我们为什么要用vue,他解决了什么问题,如何使用它?
- HDU - 6383 百度之星2018初赛B 1004 p1m2(二分答案)
- mongoose在子文档的array里update或insert
- 五个Metro UI 风格的网页设计
- 基于 TI 方案 CC2540 低功耗无线蓝牙模块详解
- 【数学期刊】世界著名数学期刊
- 单片机控制可控硅调压
- Spyder单步调试
- ubuntu服务器基本安全配置
- 关于Android学习的三个终极问题
- 云打码案例 python
- vue 实现元素可拖曳
- Ubuntu18修改系统语言为英文
- 英语学(xiao 二声)习__字母读音
- Centos之链接命令
- 失业了也别怕,你还有这些钱可以拿!
热门文章
- 用osgEarth实现Cesium的后处理特效(1)
- 基于GPU加速全局紧耦合的激光-IMU融合SLAM算法(ICRA2022)
- 最新!2022中国大学排名发布
- 一套完整的3D结构光系统搭建指南!
- ​多视图立体视觉: CVPR 2019 与 AAAI 2020 上的ACMH、ACMM及ACMP算法介绍
- log4j:WARN Error initializing output writer. log4j:WARN Unsupported encoding?
- 针对阿片类药物使用障碍的药物重定位
- AAAI2020 | SNERL:抛开mention级别的监督,实体链接、关系抽取我都行
- Python找出序列中出现最多的元素
- cytoscape绘图互作网络图(二)