您的当前位置:首页正文

SQL数据库综合实训3参考解答

2024-06-08 来源:客趣旅游网
SQL数据库综合实训3参考解答

实训名称:查询语句及应用 一、实训目的

通过完成以下实践与操作,牢固掌握SQL Server 中SELECT语句及相关子句的运用;

二、实训环境

PC机+Windows XP操作系统+SQL Server 2000; 三、实训内容与要求

现有一数据库GradeManager(成绩管理),包括四个表:学生表(Student)、课程表(Course)、班级表(Class)以及成绩表(Grade)。四个表的结构如下:

各表中的数据如下:

利用如上的成绩管理数据库与表、数据,实现以下的查询。 1.找出所有被学生选修了的课程号; use GradeManager

select distinct Cno from Grade go

2.找出01311班女学生的个人信息;

use GradeManager

select * from Student where (Ssex='女' and Clno='01311') go 3.找出01311班、01312班的学生姓名、性别; use GradeManager

select * from Student where (Clno='01311' or Clno='01312') go

4.找出所有姓李的学生的个人信息; use GradeManager

select * from Student where Sname like '李%' go

5.找出学生李勇所在班级的学生人数; use GradeManager

select Clno from Student where Sname='李勇' go

查出李通所在班级是'00311',再用如下语句查询: use GradeManager

select Clno,count(*) as '班级人数' from Student where Clno='00311' group by Clno

go

或者合并一下,用如下语句(查询结果是一样的): use GradeManager

select Clno,count(*) as '班级人数' from Student where Clno=(select Clno from Student where Sname='李勇') group by Clno

go

6.找出课程名为操作系统的平均成绩、最高分、最低分; --查平均分

use GradeManager

select Cno,avg(Gmark) as '最高分' from Grade where Cno=(select Cno from Course where Cname='操作系统') group by

Cno

go --查最高分

use GradeManager

select Cno,max(Gmark) as '最高分' from Grade where Cno=(select Cno from Course where Cname='操作系统') group by Cno

go --查最低分

use GradeManager

select Cno,min(Gmark) as '最高分' from Grade where Cno=(select Cno from Course where Cname='操作系统') group by Cno

go

7.找出选修了课程的学生人数; use GradeManager

select Sno,count(*) as '选修的课程数量' from Grade group by Sno

go

8.找出选修了课程操作系统的学生人数; use GradeManager

select Cno,count(*) as '选修人数' from Grade where Cno=(select Cno from Course where Cname='操作系统') group by Cno

go

9.找出2000级计算机软件班的成绩为空的学生姓名。

--先看简单一点的,请找出2000级计算机软件班的学生姓名,用如下查询:use GradeManager

select Sname from Student where Student.Clno in (select Class.Clno from Class where Inyear='2000' and Speciality='计算

机软件')

go

--找出2000级计算机软件班的成绩为空(即Gmark is NULL,不是为0)学生姓名,先增加二条记录以便看出查询的效果:

use GradeManager

insert into Grade values('2000101','5',NULL) insert into Grade values('2000102','1',NULL) go

--再用如下查询: use GradeManager

select Sname from Student where (Student.Clno in (select Class.Clno from Class where Inyear='2000' and Speciality='计算机软件')) and (Student.Sno in (select Grade.Sno from Grade where Gmark is NULL))

go

10.找出与李勇在同一个班级的学生信息; use GradeManager

select * from Student where Clno=(select Clno from Student where Sname='李勇')

go

11.找出所有与学生李勇有相同选修课程的学生信息; use GradeManager

select * from Student where Student.Sno in (select Grade.Sno from Grade where Cno in (select Cno from Grade where Grade.Sno=(select Student.Sno from Student where Sname='李勇')))

go

12.找出年龄介于学生李勇和25岁之间的学生信息; use GradeManager

select * from Student where (Sage>=(select Sage from

student where Sname='李勇')) and (Sage<=25)

go

13.找出选修了课程操作系统的学生学号和姓名; use GradeManager

select Student.Sno,Sname from Student where Student.Sno in (select Grade.Sno from Grade where Grade.Cno=(select Course.Cno from Course where Cname='操作系统'))

go

14.找出所有没有选修1号课程的学生学号、姓名; use GradeManager

select Student.Sno,Sname from Student where Student.Sno not in (select Grade.Sno from Grade where Grade.Cno='1')

go

15.查询选修了3号课程的学生学号及其成绩,并按成绩的降序排列;

use GradeManager

select Sno,Gmark from Grade where Cno='3' order by Gmark DESC

go

16.查询全体学生信息,要求查询结果按班级号升序排列,同一班级学生按年龄降序排列;

use GradeManager

select * from Student order by Clno,Sage DESC go

17.求每个课程号及相应的选课人数; use GradeManager

select Cno,count(*) as '选课人数' from Grade group by Cno go

18.查询选修了2门以上课程的学生学号。 use GradeManager

select Sno,count(*) as '此学生选课数' from Grade group by Sno having count(*)>2

go

四、实训分析与体会

(说明以上实践与操作是否已经成功,若遇到问题如何解决)(谈谈自己这次实训操作的体会)

因篇幅问题不能全部显示,请点此查看更多更全内容