嵌套查询.docx
《嵌套查询.docx》由会员分享,可在线阅读,更多相关《嵌套查询.docx(29页珍藏版)》请在冰豆网上搜索。
![嵌套查询.docx](https://file1.bdocx.com/fileroot1/2023-7/5/09526714-285c-43dc-86b3-3f5fc133d34f/09526714-285c-43dc-86b3-3f5fc133d34f1.gif)
嵌套查询
《数据库原理及应用》电子实验报告
题目:
SQL语言(4)数据查询—嵌套查询
日期
201..
姓名
王泽勇
QQ
实验环境:
WindowsXP,SqlServer2005
实验目的:
1.理解嵌套查询的意义,明确嵌套查询的分类
2.理解嵌套查询求解方法
3.掌握带有IN谓词、比较运算符、ANY或ALL谓词、EXISTS谓词的子查询
实验内容:
1.嵌套查询
1)嵌套查询的定义
2)嵌套查询的分类
2.嵌套查询求解方法
1)不相关子查询
2)相关子查询
3.采用的数据库表
1)库表结构定义
2)库表记录
4.带有IN谓词的子查询(写出题目、关系代数表达式和sql语句)
1)IN谓词表达的意义
2)构造嵌套查询的方法
3)实例操作
【实例1】查询选修2号课程的学生信息
selectsnamefromstudentwheresnoin(selectsnofromscwherecno='2')
【实例2】查看刘晨所在院系的学生的信息
selectsno,sname,sdeptfromstudentwheresdeptin(selectsdeptfromstudentwheresname='刘晨');
【实例3】查看计算机系的全体成员
selectsno,sname,sdeptfromstudentwheresdeptin('cs');
查询选修课课名为信息系统的学号及姓名
selectsno,snamefromstudentwheresnoin(selectsnofromscwherecnoin(selectcnofromcoursewherecnamelike'信息系统'));
查看数学、计算机、信息系统的学生
selectsnamessexfromstudentwheresdeptin('cs','MA','IS');
查看非数学、计算机、信息系统的学生
selectsnamessexfromstudentwheresdeptnotin('cs','MA','IS')
`
查询168、182、178的选课成绩
select*fromscwheresnoin('200215168','200215182','200215178');
查看选修了1号2号课程的学生
selectsnofromscwherecno='1'
andsnoin(selectsnofromscwherecno='2');
查看供应商S2且工程项目表为天津的供应情况
select*fromspjwheresno='s2'andjnoin(selectjnofromjwherecitylike'天津');
查看P1、P3、P6的表单
select*fromspjwherepnoin('P1','P3','P6');
查看供应商名为东方红状态为20、10的情况
selectsno,snamefromswherecitylike'北京'andstatusin('20','30')
;
5.带有比较运算符的子查询
1)比较运算符的种类。
<,>,=,>=,<=,!
=,<>
2)实例操作(写出题目、关系代数表达式和sql语句及sql环境运算截图)
【实例1】查找平均成绩超过自己的学生
selectsno,cnofromscx
wheregrade>=(selectavg(grade)fromscywherey.sno=x.sno);
查询比计算机学院某学生年龄小的学生的姓名及年
selectsname,sagefromstudentwheresage查询比其他学院某学生年龄比计算机学元的的学生的姓名及年龄
selectsname,sagefromstudentwheresageandsdept<>'cs';
查询非计算机系年龄在速之下的学生
selectsname,sagefromstudentwheresage<(selectmax(sage)fromstudentwheresdept='CS')andsdept<>'cs';
查询其他院系比计算机学院每一位学生都小的名单
selectsname,sagefromstudentwheresage'CS';
selectsname,sagefromstudentwheresage'CS';
查询生意选修了号课程的学生的姓名
selectsnamefromstudentwhereexists(select*fromscwheresno=student.snoandcno='1');
selectsnamefromstudentwherestudent.snoin(selectsnofromscwheresno=student.snoandcno='1');
查询没有选修了号课程的学生的姓名
selectsnamefromstudentwherenotexists(select*fromscwheresno=student.snoandcno='1');
用自身连接实现表S1对象S2的姓名为刘晨的学好姓名、院系
selectsnosname,sdeptfromstudents1
whereexists(select*fromstudents2wheres2.sdept=s1.sdeptands2.snamelike'刘晨');
6.带有ANY或ALL谓词的子查询(写出题目、关系代数表达式和sql语句及sql环境运算截图)
1)ANY或ALL谓词的含义
2)ANY或ALL谓词需要配合使用比较运算符
3)ANY与ALL与集函数的对应关系
4)实例操作
【实例1】查询比计算机学院某学生年龄小的学生的姓名及年
selectsname,sagefromstudentwheresage【实例
查询比其他学院某学生年龄比计算机学元的的学生的姓名及年龄
selectsname,sagefromstudentwheresageandsdept<>'cs';
查询生意选修了号课程的学生的姓名
selectsnamefromstudentwhereexists(select*fromscwheresno=student.snoandcno='1');
查询没有选修了号课程的学生的姓名
selectsnamefromstudentwherenotexists(select*fromscwheresno=student.snoandcno='1');
查询其他院系比计算机学院每一位学生都小的名单
selectsname,sagefromstudentwheresage'CS';
select
sname,sagefromstudentwheresage'CS
7.外连接查询(写出题目、关系代数表达式和sql语句及sql环境运算截图)
1)外连接查询的定义选择一个表作为一个主要的显示的主表S且在S表里还宣示了其他表的内容的步分信息
2)左外连接查询的定义在外拦截的基础上宣示左边的内容
3)右外连接查询的定义——与右边的表为M条记录的基本表对左边的表作1:
1M条记录
4)以学生表为主表显示学号姓名、年龄、院系、课程号。
成绩
selectstudent.sno,sname,Ssex,Sage,Sdept,cno,GradefromstudentleftoutjoinSCOn(Student.sno=SC.sno);
查看零件表的信息及其来自于外表SPJ的部分信息
selectP.pno,pname,color,weight,QTYfromPleftjoinspjon(P.pno=Spj.pno);
查询供应商表的信息及其外表的QTY的值
selectS.sno,sname,Status,city,QTYfromSleftjoinSpjon(S.sno=Spj.sno);
查询工程表的信息及其外间QTY的数值
selectj.jno,jname,city,qtyfromJleftjoinSpjon(J.jno=Spj.jno);
查询课程号的内容及其选修课里的成绩
selecto,cname,cpno,ccredit,gradefrom
courseleftjoinscon(o=o);
查看学生的学号、姓名
selectstudent.sno,snamefromstudentleftouterjoinscon(student.sno=sc.sno);
8.复合条件连接查询(写出题目、关系代数表达式和sql语句及sql环境运算截图)
1)复合条件连接查询的定义——where字句中的一个条件即连接谓词where字句中可以许多连接条件的句子为复合连接
【实例1】查询2号课程且成绩在90分以上的学生
selectstudent.sno,snamefromstudent,sc
wherestudent.sno=sc.snoando='2'andsc.grade>90;
查看PNO为P3供应数量大于300的信息
selectP.pno,pnamefromP,spjwherespj.pno='P3'andspj.QTY>300;
【实例2】查询年龄在21岁下的人的平均成绩
selectstudent.sno,sname,sdept,gavgfromstudent,s_gwhere
student.sno=s_g.snoandsage<21;
查询BT——S和学生表的部分信息
selectBT_s.sno,BT_s.sname,sbirth,ssex,sdeptfromBT_s,student
whereBT_s.sno=student.snoandSage>18andssex='男';
查看性别男年龄大于5岁的人
selectsnofromstudentwheresagein(selectsagefromstudentwheresage>5)andssexlike'男';
完成情况:
1完成了***************
2完成了****************
出现的问题:
(小四宋体)
1在完成**任务时,遇到了**问题,解决思路是****,解决步骤时*****。
2在完成**任务时,遇到了**问题,解决思路是****,解决步骤时*****。
*****************