实验报告第四次.docx

上传人:b****4 文档编号:4308098 上传时间:2022-11-29 格式:DOCX 页数:14 大小:979.47KB
下载 相关 举报
实验报告第四次.docx_第1页
第1页 / 共14页
实验报告第四次.docx_第2页
第2页 / 共14页
实验报告第四次.docx_第3页
第3页 / 共14页
实验报告第四次.docx_第4页
第4页 / 共14页
实验报告第四次.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

实验报告第四次.docx

《实验报告第四次.docx》由会员分享,可在线阅读,更多相关《实验报告第四次.docx(14页珍藏版)》请在冰豆网上搜索。

实验报告第四次.docx

实验报告第四次

实验名称:

SQL数据查询

一、实验目的:

数据查询语句是SQL语句的重要组成部分,合理使用数据查询语句,可以极大的简化应用程序编制、快速的定位分析数据库系统的故障,查询语句是编程人员与数据库管理人员必不可少的工具,通过实验达到以下目的:

(1)加深学生对查询语句基本概念的理解与掌握,最终达到灵活应用。

(2)掌握SELECT语句的基本语法。

(3)掌握简单单表查询、连接查询、嵌套查询。

(4)学会使用聚函数和进行分组查询。

二、实验内容:

1、单表查询:

2、连接查询

3、嵌套查询

三、实验环境

Windowsxp系统SQLServer2000服务器

四、程序源码与运行结果

1、单表查询:

设计查询语句完成对*、distinct、like(%和_)、in、notin、betweenand、orderby、groupby等的应用。

(1)检索出学生信息表中所有女生的记录。

Select*fromstudentwheresex=’女’

(2)从选课成绩表中检索出所有学生的成绩,并去除重复值。

selectdistinctgradefromcs

(3)从课程表中检索出全部数据的信息。

select*fromcoursewherecnamelike'数据%'

(3)从学生信息表中检索出姓王的学生的信息。

select*fromstudentwheresnamelike'王_'

(4)从成绩表中找出成绩等于60分的学生的性别。

selectsexfromstudentwheresnoin(selectsnofromcswheregrade='60')

(5)找出不在成绩表中的学生的所有信息。

select*fromstudentwheresnonotin(selectsnofromcs)

(6)在成绩表中找出成绩从70到85分的所有信息。

select*fromcswheregradebetween70and85

(7)将学生表中的所有学生的年龄按升序排列。

select*fromstudentorderbyage

(8)检索出没门课程的平均分。

selectcno,avg(grade)fromcsgroupbycno

2、连接查询

设计查询语句,分别用两种方式(where+连接条件和join…on)表示连接条件实现连接查询

(1)找出成绩大于90分的姓名和他们所在的专业。

(where+连接条件)

selectdistinctsname,deptfromstudent,cswhere(grade>90)

(2)找出成绩大于85分的姓名和他们所在的专业。

(join…on)

selectdistinctsname,dept

fromstudentjoincson(student.sno=cs.sno)where(grade>85)

3、嵌套查询

具体要完成的任务如下:

1.查询全体学生的学号与姓名

selectsno,snamefromstudent

2.查询全体学生的全部信息,并为学生表命名别名。

select*fromstudentW,courseE,csBwhereW.sno=B.snoandE.cno=B.cno

3.查全体学生的出生年份,并为年份加上标题

select出生日期fromstudent

4.查询选修了课程的学生学号,要求消除重复行

selectsnofromstudentwheresnoin(selectsnofromcs)

5.查询所有年龄在20岁以下的学生姓名及其年龄。

selectsname,agefromstudentwhereage<20

6.查询年龄在20~23岁的学生信息(要求至少使用两种方式完成查询)

select*fromstudentwhereagebetween20and23(第一种)

select*fromstudentwhere(age>=20andage<=23)(第二种)

7.使用IN关键字查询信息系(IS)、数学系(MA)和计算机科学系(CS)的学生

select*fromstudent

wheresnoin(selectsnofromstudentwheredept='IS')

select*fromstudent

wheresnoin(selectsnofromstudentwheredept='MA')

select*fromstudent

wheresnoin(selectsnofromstudentwheredept='CS')

8.查询既不是信息系、数学系,也不是计算机科学系的学生的姓名和性别。

selectsname,sexfromstudent

wheredept!

='MA'anddept!

='CS'anddept!

='IS'

9.查询所有姓刘学生的姓名、学号和性别。

selectsname,sno,sexfromstudentwheresnamelike'刘%'

10.查询名字中第2个字为"阳"字的学生的姓名和学号。

selectsname,snofromstudentwheresnamelike'_阳'

11.查询DB_Design课程的课程号和学分(先在Course表中插入“DB_Design”课程信息)。

selectcname,scorefromcoursewherecname='DB_Design'

12.查询没有考试成绩的学生学号和课程号。

selectsno,cnofromcswheregradeisnull

13.查询计算机系年龄在20岁以下的学生姓名。

selectsnamefromstudentwhereage<20anddept='CS'

14.查询全体学生情况,查询结果按所在系的系号升序排列,同一系中的学生按年龄降序排列。

select*fromstudentorderbydept,agedesc

15.查询学生总人数。

selectcount(sno)fromstudent

16.查询选修了课程的学生人数。

selectcount(distinctsno)fromcs

17.计算1号课程的学生平均成绩。

selectavg(grade)fromcswherecno='C001'

18.查询选修1号课程的学生最高分数。

selectmax(grade)fromcswherecno='C001'

19.求各个课程号及相应的选课人数。

selectcno,count(sno)fromcsgroupbycno

20.查询选修了3门以上课程的学生学号(提示用Having字句)

selectsno,count(cno)fromcsgroupbysnohavingcount(cno)>=3

21、查询有3门以上课程是90分以上的学生的学号及(90分以上的)课程数。

selectsno,count(cno)fromcswheregrade>=90

groupbysnohavingcount(cno)>=3

22、查询全体学生与选课表的笛卡尔积。

select*fromstudentcrossjoincourse

23、查询每个学生及其选修课程的情况。

selectdistinct*fromstudentcrossjoincswherestudent.sno=cs.sno

24、查询每个学生及其选修课程的情况(去掉重复属性)

selecta.sno,sname,sex,dept,age,o,cname,score,c.grade

fromstudenta,courseb,cscwherea.sno=c.snoando=o

25、查询某门课程考试成绩相同的学生学号和课程信息

selecta.sno,o,ame,b.scorefromcsa,courseb

whereo=oand(selectcount(*)fromcswherecno=cnoandgrade=grade)>=2

26、查询每个学生的选修课程包括没有选修课程的学生(外连接)

select*fromstudenta,csbwherea.sno*=b.sno

27、查询每个学生的选修课程包括没有被学生选修的课程(外连接)

select*fromstudent,cswherestudent.sno=*cs.sno

28、查询每个学生的选修课程即包括没有被学生选修的课程又包括没有被学生选修的课程(全连接)

select*fromstudentfulljoincsonstudent.sno=cs.sno

29、查询选修2号课程且成绩在90分以上的所有学生的学号、姓名

selectsno,snamefromstudent

wheresnoin(selectsnofromcswheregrade>='90'andcno='C002')

30、查询每个学生的学号、姓名、选修的课程名及成绩

selectstudent.sno,sname,cname,gradefromstudent,course,cs

where(student.sno=cs.sno)and(o=o)

31、查询与“张三”在一个系学习的学生(IN)

select*fromstudent

wheredeptin(selectdeptfromstudentwheresname='张三')

32、查询选修了课程名为“信息系统”的学生学号和姓名。

selectsno,snamefromstudent

wheresnoin(selectsnofromcswherecnoin(selectcnofromcoursewherecname='信息系统'))

33、查询与“张三”在同一个系学习的学生

select*fromstudent

wheredeptin(selectdeptfromstudentwheresname='张三')

34、查询选修了课程1或者选修了课程2的学生(要求消除重复组UNION)

(selectsnofromcswherecno='C001')UNION

(selectsnofromcswherecno='C002')

35、查询选修了课程1或者选修了课程2的学生(要求不消除重复组UNIONALL)

(selectsnofromcswherecno='C001')UNIONall

(selectsnofromcswherecno='C002')

五、实验总结

通过本次试验,掌握了使用SQL语句查询的技巧。

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 解决方案 > 学习计划

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1