SQL数据查询策略15p_第1页
SQL数据查询策略15p_第2页
SQL数据查询策略15p_第3页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、实验名称:SQLB据查询一、实验目的:数据查询语句是SQL®句的重要组成部分,合理使用数据查询语句,可以极大的简化应用程序编制、快速的定位分析数据库系统的故障,查询语句是编程人员与数据库管理人员必不可少的工具,通过实验达到以下目的:(1) 加深学生对查询语句基本概念的理解与掌握,最终达到灵活应用。(2) 掌握SELECT语句的基本语法。(3) 掌握简单单表查询、连接查询、嵌套查询。(4) 学会使用聚函数和进行分组查询。二、实验内容:1、单表查询:2、连接查询3、嵌套查询三、实验环境Windowsxp系统SQLServer2000服务器四、程序源码与运行结果1、单表查询:设计查询语句完

2、成对*、distinct、like(%和_)、in、notin、betweenand、orderby、groupby等的应用。(1) 检索出学生信息表中所有女生的记录。Select*fromstudentwheresex='女'(2) 从选课成绩表中检索出所有学生的成绩,并去除重复值。selectdistinctgradefromcs(3) 从课程表中检索出全部数据的信息。select*fromcoursewherecnamelike'数据%'(3) 从学生信息表中检索出姓王的学生的信息。select*fromstudentwheresnamelike'

3、王_'(4) 从成绩表中找出成绩等丁60分的学生的性别。selectsexfromstudentwheresnoin(selectsnofromcswheregrade='60')(5) 找出不在成绩表中的学生的所有信息。select*fromstudentwheresnonotin(selectsnofromcs)(6) 在成绩表中找出成绩从70到85分的所有信息。select*fromcswheregradebetween70and85(7) 将学生表中的所有学生的年龄按升序排列。select*fromstudentorderbyage(8) 检索出没门课程的平均分

4、。selectcno,avg(grade)fromcsgroupbycno2、连接查询设计查询语句,分别用两种方式(where+连接条件和join-on)表示连接条件实现连接查询找出成绩大丁90分的姓名和他们所在的专业。(where+连接条件)selectdistinctsname,deptfromstudent,cswhere(grade>90)找出成绩大丁85分的姓名和他们所在的专业。(join-on)selectdistinctsname,deptfromstudentjoincson(student.sno=cs.sno)where(grade>85)3、嵌套查询具体要完成

5、的任务如下:1. 查询全体学生的学号与姓名selectsno,snamefromstudent查询全体学生的全部信息,并为学生表命名别名。2. select*fromstudentW,courseE,csBwhereW.sno=B.snoandE.cno=B.cno查全体学生的出生年份,并为年份加上标题select出生日期fromstudent查询选修了课程的学生学号,要求消除重复行selectsnofromstudentwheresnoin(selectsnofromcs)查询所有年龄在20岁以下的学生姓名及其年龄。3. selectsname,agefromstudentwhereage&

6、lt;20查询年龄在2023岁的学生信息(要求至少使用两种方式完成查询)select*fromstudentwhereagebetween20and23(第一种)select*fromstudentwhere(age>=20andage<=23)(第二种)使用IN关键字查询信息系(IS)、数学系(MA和计算机科学系(CS的学生select*fromstudentwheresnoin(selectsnofromstudentwheredept='IS')select*fromstudentwheresnoin(selectsnofromstudentwheredept

7、='MA')select*fromstudentwheresnoin(selectsnofromstudentwheredept='CS')查询既不是信息系、数学系,也不是计算机科学系的学生的姓名和性别。4. selectsname,sexfromstudentwheredept!='MA'anddept!='CS'anddept!='IS'查询所有姓刘学生的姓名、学号和性别。刘%'5. selectsname,sno,sexfromstudentwheresnamelike查询名字中第2个字为”阳”字的学

8、生的姓名和学号。6. selectsname,snofromstudentwheresnamelike'_阳'查询DB_Design课程的课程号和学分(先在Course表中插入“DB_Design课程信息)。7. selectcname,scorefromcoursewherecname='DB_Design'查询没有考试成绩的学生学号和课程号。8. selectsno,cnofromcswheregradeisnull查询计算机系年龄在20岁以下的学生姓名。9. selectsnamefromstudentwhereage<20anddept='

9、CS'查询全体学生情况,查询结果按所在系的系号升序排列,同一系中的学生按年龄降序排列。10. select*fromstudentorderbydept,agedesc查询学生总人数。11. selectcount(sno)fromstudent查询选修了课程的学生人数。12. selectcount(distinctsno)fromcs计算1号课程的学生平均成绩。13. selectavg(grade)fromcswherecno='C001'查询选修1号课程的学生最高分数。14. selectmax(grade)fromcswherecno='C001

10、9;求各个课程号及相应的选课人数。selectcno,count(sno)fromcsgroupbycno查询选修了3门以上课程的学生学号(提示用Having字句)selectsno,count(cno)fromcsgroupbysnohavingcount(cno)>=321、查询有3门以上课程是90分以上的学生的学号及(90分以上的)课程数。selectsno,count(cno)fromcswheregrade>=90groupbysnohavingcount(cno)>=322、查询全体学生与选课表的笛卡尔积。select*fromstudentcrossjoinco

11、urse23、查询每个学生及其选修课程的情况。selectdistinct*fromstudentcrossjoincswherestudent.sno=cs.sno24、查询每个学生及其选修课程的情况(去掉重复届性)selecta.sno,sname,sex,dept,age,o,cname,score,c.gradefromstudenta,courseb,cscwherea.sno=o=o25、查询某门课程考试成绩相同的学生学号和课程信息selecta.sno,o,ame,b.scorefromcsa,o=oand(selectcount(*)fromcswherecno=cnoandg

12、rade=grade)>=226、查询每个学生的选修课程包括没有选修课程的学生(外连接)select*fromstudenta,csbwherea.sno*=b.sno27、查询每个学生的选修课程包括没有被学生选修的课程(外连接)select*fromstudent,cswherestudent.sno=*cs.sno28、查询每个学生的选修课程即包括没有被学生选修的课程乂包括没有被学生选修的课程(全连接)select*fromstudentfulljoincsonstudent.sno=cs.sno29、查询选修2号课程且成绩在90分以上的所有学生的学号、姓名selectsno,sna

13、mefromstudentwheresnoin(selectsnofromcswheregrade>='90'andcno='C002')30、查询每个学生的学号、姓名、选修的课程名及成绩selectstudent.sno,sname,cname,gradefromstudent,course,cswhere(student.sno=cs.sno)and(o=o)31、查询与“张三”在一个系学习的学生(IN)select*fromstudentwheredeptin(selectdeptfromstudentwheresname='张三')32、查询选修了课程名为“信息系统”的学生学号和姓名。selectsno,snamefromstudentwheresnoin(selectsnofromcswherecnoin(selectcnofromcoursewherecname='信息系统')33、查询与“张三”在同一个系学习的学生select*fromstudentwheredeptin(selectdeptfromstudentwheresname='张三')34、查询选修了课程1或者选修了课程2的学生(要求消除重复组UNION(selectsnofromcswherecno=&

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论