当前位置:文档之家› 4实验四复杂查询

4实验四复杂查询

实验四复杂查询
一、实验目的
掌握两个表以上的连接查询的应用,包括嵌套查询。

二、实验内容
(1)查询比“林红”年纪大的男学生信息。

select*from Student
where Sex='男'and
YEAR(Birth)-(select YEAR(Birth)
from Student where Sname='林红')<0
(2)检索所有学生的选课信息,包括学号、姓名、课号、课程名、成绩。

select SC.Sno,Sname,Sex,Classno,Cname,Grade
from Student s,SC,Course c
where s.Sno=SC.Sno and o=o
(3)查询已选课学生的学号、姓名、课程名、成绩。

select SC.Sno,Sname,Cname,Grade
from Student s,course c,SC
where s.sno=SC.sno and o=o
(4)查询选修了“C语言程序设计”的学生的学号和姓名。

select sc.Sno,Sname
from Student s,course c,sc
where ame='C语言程序设计'and s.Sno=sc.Sno and o=o
(5)查询与“张虹”在同一个班级的学生学号、姓名、家庭住址。

a.用子查询
select Sno,Sname,Home_addr
from Student
where Classno='051' and Sname!='张虹'
b.用连接查询
select Sno,Sname,Home_addr
from Student
where Classno=(select Classno from Student where Sname='张虹') and Sname!='张虹'
(6)查询其他班级中比“051”班所有学生年龄大的学生的学号、姓名。

select Sno,Sname
from Student
where Classno<>'051'
and Birth<all(select Birth from Student
where Classno='051')
(7)(选作)查询选修了全部课程的学生姓名。

本题使用除运算的方法。

由题意可得另一种语言,没有一个选了课的学生没有选course表里的课程。

那么,我们需要两个NOT EXISTS表示双重否定;另一种思路可详见书例4.52
select Sname from Student
where not exists(
select*from Course
where not exists(
select*from SC
where Sno=Student.sno
and cno=o))
(8)(选作)查询至少选修了学生“20110002”选修的全部课程的学生的学号,姓名。

select Sno,Sname from Student
where Sno in(
select distinct Sno
from SC as SC1
where not exists(
select*from SC as SC2
where SC2.Sno='20110002'
and not exists(
select*from SC as SC3
where SC3.Sno=SC1.Sno and
o=o)) )
(9)检索学生的学号、姓名、学习课程名及课程成绩。

select s.Sno,Sname,Cname,Grade
from Student s,Course c,SC
where s.Sno=sc.Sno and o=o
(10)检索选修了“高数”课且成绩至少高于选修课程号为“002”课程的学生的学号、课程号、成绩,并按成绩从高到低次序排列。

由题意得,选修了高数课的学生的成绩要高于选修002课号课程的学生的成绩
select distinct Sno,Cno,Grade
from SC
where Cno in(
select Cno from Course
where Cname='高数')
and Grade>(select MAX(Grade)
from SC where cno='002')
order by Grade desc
(11)检索选修3门以上课程的学生的学号、总成绩(不统计不及格的课程),并要求按总成绩的降序排列出来。

select Sno,sum(grade)as总成绩
from SC
where Sno in(
select Sno from SC
group by Sno
having count(*)>3)
and Grade>=60
group by Sno
order by总成绩desc
(12)检索多于3名学生选修的并以3结尾的课程号的平均成绩。

select avg(Grade)as平均成绩
from SC
where Cno like'%3'
group by Cno
having count(Cno)>3
(13)检索最高分与最低分之差大于5分的学生的学号、姓名、最高分、最底分。

select distinct SC.Sno学号,Sname姓名,
max(grade)as最高分,min(grade)as最低分
from Student,SC
where SC.Sno=Student.Sno
group by SC.Sno,Sname
having max(grade)-min(grade)>5
(14)外连接
对实验二中的表6和表7做一个外连接查询,显示每门课程的课号、课名、选修该门课的学号、成绩,没有同学选修的课程(如Visual_Basic)也要在查询结果中。

select o课号,Cname课名,Sno学号,Grade成绩
from Course c left outer join SC
on (o=o)
(15)创建一个表Student_other,结构同Student,输入若干记录,部分记录和Student表中的相同。

创建过程:
create table Student_other(
Sno char(8)primary key,
Sname varchar(8)not null,
Sex char(2)not null,
Birth smalldatetime not null,
Classno char(3)not null,
Entrance_date smalldatetime not null,
Home_addr varchar(40),
Sdept char(2)not null,
Postcode char(6)
)
随意输入几条Student表中没有的信息,完成创建
a.查询同时出现在Student表和Student_other表中的记录select*from student_other so,Student s where so.Sno=s.Sno
b. 查询Student表和Student_other表中的全部记录
select*from student
union
select*from student_other
(16)(选作)创建一个数据库Student_info_other,参数自定。

创建过程:
新建数据库
百度文库- 让每个人平等地提升自我
名称确定,参数自定义,然后“确定”即可
a.当前数据库为Student_info,将Student_info数据库中的Student_other复制到Student_info_other中。

select*into Student_info_other.dbo.Student_other
from Student_info.dbo.Student_other
b.查询同时出现在Student表和Student_info_other数据库Student_other表中的记录。

select*from Student_info_other.dbo.student_other so,
Student_info.dbo.Student s
where
so.sno=s.sno
11。

相关主题