• 数据库系统概论(超详解!!!) 第三节 关系数据库标准语言SQL(Ⅳ)


    1.集合查询

    集合操作的种类

    并操作UNION

    交操作INTERSECT

    差操作EXCEPT

    参加集合操作的各查询结果的列数必须相同;对应项的数据类型也必须相同

    1. 查询计算机科学系的学生及年龄不大于19岁的学生。
    2. SELECT *
    3. FROM Student
    4. WHERE Sdept= 'CS'
    5. UNION
    6. SELECT *
    7. FROM Student
    8. WHERE Sage<=19;

    UNION:将多个查询结果合并起来时,系统自动去掉重复元组

    UNION ALL:将多个查询结果合并起来时,保留重复元组

    1. 查询选修了课程1或者选修了课程2的学生。
    2. SELECT Sno
    3. FROM SC
    4. WHERE Cno=' 1 '
    5. UNION
    6. SELECT Sno
    7. FROM SC
    8. WHERE Cno= ' 2 ';
    9. 查询计算机科学系的学生与年龄不大于19岁的学生的交集。
    10. SELECT *
    11. FROM Student
    12. WHERE Sdept='CS'
    13. INTERSECT
    14. SELECT *
    15. FROM Student
    16. WHERE Sage<=19
    17. 实际上就是查询计算机科学系中年龄不大于19岁的学生。
    18. SELECT *
    19. FROM Student
    20. WHERE Sdept= 'CS' AND Sage<=19;
    21. 查询既选修了课程1又选修了课程2的学生。
    22. SELECT Sno
    23. FROM SC
    24. WHERE Cno=' 1 '
    25. INTERSECT
    26. SELECT Sno
    27. FROM SC
    28. WHERE Cno='2 ';
    29. 也可以表示为:
    30. SELECT Sno
    31. FROM SC
    32. WHERE Cno=' 1 ' AND Sno IN
    33. (SELECT Sno
    34. FROM SC
    35. WHERE Cno=' 2 ');
    36. 查询计算机科学系的学生与年龄不大于19岁的学生的差集。
    37. SELECT *
    38. FROM Student
    39. WHERE Sdept='CS'
    40. EXCEPT
    41. SELECT *
    42. FROM Student
    43. WHERE Sage <=19;
    44. 实际上是查询计算机科学系中年龄大于19岁的学生
    45. SELECT *
    46. FROM Student
    47. WHERE Sdept= 'CS' AND Sage>19;

    2.于派生表的查询

    子查询不仅可以出现在WHERE子句中,还可以出现在FROM子句中。

    这时子查询生成的临时派生表(Derived Table)成为主查询的查询对象

    1. 找出每个学生超过他自己选修课程平均成绩的课程号
    2. SELECT Sno, Cno
    3. FROM SC, (SELECTSno, Avg(Grade)
    4. FROM SC
    5. GROUP BY Sno)
    6. AS Avg_sc(avg_sno,avg_grade)
    7. WHERE SC.Sno = Avg_sc.avg_sno
    8. and SC.Grade >=Avg_sc.avg_grade

    如果子查询中没有聚集函数,派生表可以不指定属性列,子查询SELECT子句后面的列名为其缺省属性。

    1. 查询所有选修了1号课程的学生姓名,可以用如下查询完成:
    2. SELECT Sname
    3. FROM Student,
    4. (SELECT Sno FROM SC
    5. WHERE Cno=' 1 ') AS SC1
    6. WHERE Student.Sno=SC1.Sno;

    3.Select语句的一般形式

    SELECT [ALL|DISTINCT]      

    <目标列表达式> [别名] [ ,<目标列表达式> [别名]] …  

    FROM     <表名或视图名> [别名]                

    [ ,<表名或视图名> [别名]] …                

    |()[AS]<别名>  

    [WHERE <条件表达式>]  

    [GROUP BY <列名1>[HAVING<条件表达式>]]

     [ORDER BY <列名2> [ASC|DESC]];

    1. 目标列表达式的可选格式

    目标列表达式格式

    (1) *

    (2) <表名>.*

    (3) COUNT([DISTINCT|ALL]* )

    (4) [<表名>.]<属性列名表达式>[,<表名>.]<属性列名表达式>]…     

    其中<属性列名表达式>可以是由属性列、作用于属性列 的聚集函数和常量的任意算术运算(+,-,*,/)组成的 运算公式

    2. 聚集函数的一般格式

    3. WHERE子句的条件表达式的可选格式

    4.练习

    1. /*1)查询选修了81003号课程的学生姓名;*/
    2. /*方法1:连接查询*/
    3. select sname
    4. from Student,SC
    5. where Student.Sno=SC.Sno and SC.Cno='81003';
    6. /*方法2:嵌套in*/
    7. select sname
    8. from Student
    9. where Sno in (select Sno
    10. from SC
    11. where Cno='81003');
    12. /*方法3:嵌套exists*/
    13. select sname
    14. from Student
    15. where exists (select *
    16. from SC
    17. where Sno=Student.Sno and Cno='81003');
    18. /*2)查询选修了学分为3的课程的学生学号和姓名;*/
    19. /*方法1:连接查询*/
    20. select Student.Sno ,student.sname
    21. from Student,SC,Course
    22. where Student.Sno=SC.Sno and SC.Cno=Course.Cno and Ccredit='3';
    23. /*方法2:嵌套in*/
    24. select Sno ,sname
    25. from Student
    26. where Sno in (select Sno
    27. from SC
    28. where Cno in (select Cno
    29. from Course
    30. where Ccredit='3'));
    31. /*3)找出每个超过其所在专业平均年龄的学号,姓名和年龄*/
    32. /*方法1:嵌套相关查询*/
    33. select sno,sname,YEAR(GETDATE())-YEAR(sbirthdate)as'年龄'
    34. from Student
    35. where YEAR(GETDATE())-YEAR(sbirthdate)> any(select AVG(YEAR(GETDATE())-YEAR(sbirthdate))
    36. from Student
    37. group by Smajor);
    38. /*方法2:派生表*/
    39. select distinct sno,sname,YEAR(GETDATE())-YEAR(sbirthdate)as'年龄'
    40. from Student x,(select AVG(YEAR(GETDATE())-YEAR(sbirthdate))
    41. from Student y
    42. group by y.Smajor) as avg_sex(sex)
    43. where YEAR(GETDATE())-YEAR(sbirthdate)>avg_sex.sex;
    44. /*4)查询学分大于“操作系统”的所有课程名称;*/
    45. /*方法1:嵌套>*/
    46. select Cname
    47. from Course
    48. where Ccredit >(select Ccredit
    49. from Course
    50. where Cname='操作系统');
    51. /*方法2:嵌套exists*/
    52. select Cname
    53. from Course x
    54. where exists(select *
    55. from Course y
    56. where x.Ccredit>y.Ccredit and y.Cname='操作系统');
    57. /*5)查询没有选“数据库”的学生学号;*/
    58. /*方法1:嵌套exists*/
    59. select distinct Sno
    60. from SC x
    61. where not exists(select *
    62. from SC y
    63. where y.Sno=x.Sno and exists(select *
    64. from Course
    65. where Cno=y.Cno and Cname='数据库系统概论') );
    66. /*方法2:集合差*/
    67. select distinct sno
    68. from SC
    69. except
    70. select Sno
    71. from Course,SC
    72. where Course.Cno=SC.Cno and Cname='数据库系统概论';
    73. /*方法3not in*/
    74. select distinct Sno
    75. from SC
    76. where Sno not in (select Sno
    77. from SC
    78. where Cno in (select Cno
    79. from Course
    80. where Cname='数据库系统概论') );
    81. /*6)查询与“数据库”、“数学”学分不同的所有课程名称;*/
    82. /*方法1not in*/
    83. select Cname
    84. from Course
    85. where Ccredit not in (select Ccredit
    86. from Course
    87. where Cname in (select Cname
    88. from Course
    89. where Cname in('数据库系统概论','离散数学') ));
    90. /*方法3<> all或者any*/
    91. select Cname
    92. from Course
    93. where Ccredit <> any(select Ccredit
    94. from Course
    95. where Cname in (select Cname
    96. from Course
    97. where Cname in('数据库系统概论','离散数学') ));
    98. /*7)查询平均分大于等于80分的所有课程号和课程名称;*/
    99. /*方法1:连接查询*/
    100. select Course.Cno,Cname
    101. from Course,SC
    102. where Course.Cno=SC.Cno
    103. group by Course.Cno,Cname
    104. having AVG(Grade)>='80';
    105. /*方法2:派生表*/
    106. select Course.Cno,Cname
    107. from Course,(select cno
    108. from SC
    109. group by Cno
    110. having AVG(Grade)>='80')as avg_sc(avg_cno)
    111. where Course.Cno=avg_sc.avg_cno
    112. group by Course.Cno,Cname
    113. /*8)查询同时选修了‘81001’和‘81002’号课程的学生学号和姓名;*/
    114. /*方法1:自身连接*/
    115. select student.Sno,Sname
    116. from Student ,SC s1,SC s2
    117. where Student.Sno=s1.Sno and s1.Sno=s2.Sno and s1.Cno='81001' and s2.Cno='81002'
    118. group by Student.Sno,Sname;
    119. /*方法2:嵌套in*/
    120. select Sno,Sname
    121. from Student
    122. where Sno in (select Sno
    123. from SC
    124. where Cno = '81001'and Sno in (select Sno
    125. from SC
    126. where Cno='81002'));
    127. /*方法3:集合*/
    128. select student.Sno,Sname
    129. from Student ,SC
    130. where Student.Sno=sc.Sno and Cno='81001'
    131. INTERSECT
    132. select student.Sno,Sname
    133. from Student ,SC
    134. where Student.Sno=SC.Sno and Cno='81002';
    135. /*9)查询同时选修了‘数据库系统概论’和‘数据结构’的学生学号和姓名;*/
    136. /*方法1:嵌套in*/
    137. select Sno,Sname
    138. from Student
    139. where Sno in (select Sno
    140. from SC
    141. where Cno in (select Cno
    142. from Course
    143. where Cname = '数据库系统概论') and Sno in (select Sno
    144. from SC
    145. where Cno in (select Cno
    146. from Course
    147. where Cname='数据结构')));
    148. /*方法2:集合*/
    149. select Student.Sno,Sname
    150. from Student,(select Sno
    151. from SC
    152. where Cno in (select Cno
    153. from Course
    154. where Cname = '数据库系统概论'))as x_sc(sno)
    155. where Student.Sno=x_sc.sno
    156. intersect
    157. select Student.Sno,Sname
    158. from Student,(select Sno
    159. from SC
    160. where Cno in (select Cno
    161. from Course
    162. where Cname='数据结构'))as y_sc(sno)
    163. where Student.Sno=y_sc.sno
    164. /*10)查询所有学生都选了的课程号;*/ /*嵌套exists,不存在一个学生没选的课程*/
    165. SELECT Cno
    166. FROM Course
    167. WHERE NOT EXISTS
    168. (SELECT *
    169. FROM Student
    170. WHERE NOT EXISTS
    171. (SELECT *
    172. FROM SC
    173. WHERE Sno= Student.Sno
    174. AND Cno= Course.Cno
    175. )
    176. );
    177. /*11)查询与“数据结构”具有相同先修课的课程号和课程名;*/
    178. /*方法1:自身连接*/
    179. select c1.Cno,c1.Cname
    180. from Course c1,Course c2
    181. where c1.Cpno=c2.Cpno and c2.Cname='数据结构' and c1.Cname<>'数据结构';
    182. /*方法2:嵌套in*/
    183. select Cno,Cname
    184. from Course
    185. where Cname<>'数据结构' and Cpno in (select Cpno
    186. from Course
    187. where Cname='数据结构');
    188. /*方法3:嵌套exists*/
    189. select Cno,Cname
    190. from Course x
    191. where Cname<>'数据结构' and exists (select *
    192. from Course y
    193. where y.Cpno=x.Cpno and Cname='数据结构');
    194. /*方法4:派生表*/
    195. select Cno,Cname
    196. from Course,(select Cpno
    197. from Course
    198. where Cname='数据结构')as xcourse(scpno)
    199. where Cname<>'数据结构'and Cpno=xcourse.scpno;
    200. /*12)查询所有具有不及格记录的学生学号和姓名*/
    201. /*方法1:连接查询*/
    202. select Student.Sno,Sname
    203. from Student,SC
    204. where Student.Sno=SC.Sno
    205. group by Student.Sno,Sname,Grade
    206. having Grade<'60';
    207. /*方法3:嵌套in*/
    208. select Sno,Sname
    209. from Student
    210. where Sno in (select Sno
    211. from SC
    212. where Grade<'60');
    213. /*方法3:嵌套exists*/
    214. select Sno,Sname
    215. from Student
    216. where exists(select *
    217. from SC
    218. where sno=Student.Sno and Grade<'60');
    219. /*方法4:派生表*/
    220. select Student.Sno,Sname
    221. from Student,(select Sno
    222. from SC
    223. where Grade<'60')as xsc(sno)
    224. where Student.Sno=xsc.sno
    225. group by Student.Sno,Sname
    226. /*13)查询计算机科学与技术专业学生选修的所有课程号;*/
    227. /*方法1:连接查询*/
    228. select SC.Cno
    229. from Student,SC
    230. where Student.Sno=SC.Sno
    231. group by SC.Cno,Smajor
    232. having Smajor='计算机科学与技术';
    233. /*方法2:嵌套in*/
    234. select distinct Cno
    235. from SC
    236. where Sno in (select Sno
    237. from Student
    238. where Smajor='计算机科学与技术');
    239. /*方法3:嵌套exists*/
    240. select distinct Cno
    241. from SC
    242. where exists(select *
    243. from Student
    244. where Sno=SC.Sno and Smajor='计算机科学与技术');
    245. /*方法4:派生表*/
    246. select Cno
    247. from SC,(select Sno
    248. from Student
    249. where Smajor='计算机科学与技术')as xstudent(sno)
    250. where xstudent.sno=SC.Sno
    251. group by Cno;
    252. /*14)查询所有计算机科学与技术专业学生都选的课程号;*/
    253. SELECT Cno
    254. FROM Course
    255. WHERE NOT EXISTS
    256. (SELECT *
    257. FROM Student
    258. WHERE NOT EXISTS
    259. (SELECT *
    260. FROM SC
    261. WHERE Sno= Student.Sno
    262. AND Cno= Course.Cno
    263. )and Smajor='计算机科学与技术'
    264. );
    265. /*15)查询选修了81003号课程并且不及格的学生姓名*/
    266. /*方法1:多表连接法*/
    267. select Sname
    268. from Student,SC
    269. where Student.Sno=SC.Sno
    270. group by Student.Sno,Sname,Grade,Cno
    271. having Grade<'60'and Cno='81003';
    272. /*方法2:嵌套in*/
    273. select Sname
    274. from Student
    275. where Sno in (select Sno
    276. from SC
    277. where Grade<'60'and Cno='81003');
    278. /*方法3:交集*/
    279. select Sname
    280. from Student,SC
    281. where Student.Sno=SC.Sno
    282. group by Student.Sno,Sname,Grade,Cno
    283. having Grade<'60'
    284. intersect
    285. select Sname
    286. from Student,SC
    287. where Student.Sno=SC.Sno
    288. group by Student.Sno,Sname,Grade,Cno
    289. having Cno='81003';
    290. /*方法4:派生表*/
    291. select Sname
    292. from Student,(select Sno
    293. from SC
    294. where Grade<'60'and Cno='81003')as xsc(sno)
    295. where Student.Sno=xsc.sno
    296. group by Sname;
    297. /*方法5:嵌套exists*/
    298. select Sname
    299. from Student
    300. where exists(select *
    301. from SC
    302. where Sno=Student.Sno and Grade<'60'and Cno='81003');
    303. /*16)查询选修了“数据库系统概论”并且不及格的学生姓名*/
    304. /*方法1:多表连接法*/
    305. select Sname
    306. from Student,SC,Course
    307. where Student.Sno=SC.Sno and SC.Cno=Course.Cno
    308. group by Student.Sno,Sname,Grade,Cname
    309. having Grade<'60'and Cname='数据库系统概论';
    310. /*方法2:嵌套in*/
    311. select Sname
    312. from Student
    313. where Sno in (select Sno
    314. from SC
    315. where Grade<'60'and Cno in (select Cno
    316. from Course
    317. where Cname='数据库系统概论'));
    318. /*方法3:交集*/
    319. select Sname
    320. from Student,SC
    321. where Student.Sno=SC.Sno
    322. group by Student.Sno,Sname,Grade
    323. having Grade<'60'
    324. intersect
    325. select Sname
    326. from Student,SC,Course
    327. where Student.Sno=SC.Sno and SC.Cno=Course.Cno
    328. group by Student.Sno,Sname,Cname
    329. having Cname='数据库系统概论';
    330. /*方法4:派生表*/
    331. select Sname
    332. from Student,(select Sno,Cno
    333. from SC
    334. where Grade<'60')as xsc(sno,cno),(select Cno
    335. from Course
    336. where Cname='数据库系统概论')as xcourse(cno)
    337. where Student.Sno=xsc.sno and xsc.cno=xcourse.cno
    338. group by Sname;
    339. /*17)查询计算机科学与技术专业选修了“数据库系统概论”课且成绩及格的所有学生的学号和姓名;*/
    340. /*方法1:多表连接法*/
    341. select Student.Sno,Sname
    342. from Student,SC,Course
    343. where Student.Sno=SC.Sno and SC.Cno=Course.Cno
    344. group by Student.Sno,Sname,Grade,Cname,Smajor
    345. having Grade>'60'and Cname='数据库系统概论'and Smajor='计算机科学与技术';
    346. /*方法2:嵌套in*/
    347. select Sno,Sname
    348. from Student
    349. where Smajor='计算机科学与技术' and Sno in (select Sno
    350. from SC
    351. where Grade>'60'and Cno in (select Cno
    352. from Course
    353. where Cname='数据库系统概论') );
    354. /*方法3:交集*/
    355. select Student.Sno,Sname
    356. from Student,SC
    357. where Student.Sno=SC.Sno
    358. group by Student.Sno,Sname,Grade
    359. having Grade>'60'
    360. intersect
    361. select Student.Sno,Sname
    362. from Student,SC,Course
    363. where Student.Sno=SC.Sno and SC.Cno=Course.Cno
    364. group by Student.Sno,Sname,Grade,Cname,Smajor
    365. having Cname='数据库系统概论'
    366. intersect
    367. select Sno,Sname
    368. from Student
    369. where Smajor='计算机科学与技术';
    370. /*方法4:派生表*/
    371. select Student.Sno,Sname
    372. from Student,(select Sno,Cno
    373. from SC
    374. where Grade>'60')as xsc(sno,cno),(select Cno
    375. from Course
    376. where Cname='数据库系统概论')as xcourse(cno)
    377. where Student.Sno=xsc.sno and xsc.cno=xcourse.cno and Smajor='计算机科学与技术'
    378. group by Student.Sno,Sname;
    379. /*18)查询与“刘晨”同岁且不与“刘晨”在同一个系的学生学号与姓名;*/
    380. /*方法1:嵌套in*/
    381. select Sno,Sname
    382. from Student
    383. where Sname<>'刘晨' and YEAR(GETDATE())-YEAR(sbirthdate)in (select YEAR(GETDATE())-YEAR(sbirthdate)
    384. from Student
    385. where Sname='刘晨')and Smajor not in (select Smajor
    386. from Student
    387. where Sname='刘晨');
    388. /*方法2:嵌套exists*/
    389. select Sno,Sname
    390. from Student x
    391. where Sname<>'刘晨'
    392. and exists(select *
    393. from Student y
    394. where YEAR(GETDATE())-YEAR(y.sbirthdate)=YEAR(GETDATE())-YEAR(x.sbirthdate)and y.Sname='刘晨')
    395. and not exists(select *
    396. from Student z
    397. where z.Smajor=x.Smajor and z.Sname='刘晨');
    398. /*方法3:派生表*/
    399. select Student.Sno,Sname
    400. from Student,(select Sno,YEAR(GETDATE())-YEAR(sbirthdate)
    401. from Student
    402. where Sname='刘晨')as ystudent(sno,sex),(select Sno,Smajor
    403. from Student
    404. where Sname='刘晨')as zstudent(sno,smajor)
    405. where Sname<>'刘晨'and YEAR(GETDATE())-YEAR(sbirthdate)=ystudent.sex and Student.Smajor<>zstudent.smajor

  • 相关阅读:
    Windows 小狼毫 如何在安装后指定用户配置文件文件夹
    在线表单设计器都有哪些优秀的功能?
    Python 中并发方面的差异
    如何利用DGL官方库中的rgcn链接预测代码跑自己的数据集(如何在DGL库的链接预测数据集模块定义自己的数据集类)
    移动端手指事件和手机事件:
    python开发工具以及数据类型,eval函数,运算符,系列解包赋值,逻辑运算符,位运算符,运算符优先级
    makefile-c
    在微服务架构中管理技术债务
    【字典数组NSDictionary和NSMutableDictionary的持久化 Objective-C语言】
    2023临沂大学计算机考研信息汇总
  • 原文地址:https://blog.csdn.net/2201_76115387/article/details/137010565