4实验四 复杂查询

更新时间:2023-10-16 10:00:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

实验四 复杂查询

一、实验目的

掌握两个表以上的连接查询的应用,包括嵌套查询。

二、实验内容

(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 SC.cno=c.cno

1

(3)查询已选课学生的学号、姓名、课程名、成绩。

select SC.Sno,Sname,Cname,Grade from Student s,course c,SC

where s.sno=SC.sno and c.cno=SC.cno

(4)查询选修了“C语言程序设计”的学生的学号和姓名。

select sc.Sno,Sname

from Student s,course c,sc

where c.Cname='C语言程序设计' and s.Sno=sc.Sno and sc.Cno=c.Cno

2

(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”班所有学生年龄大的学生的学号、姓名。

3

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=Course.cno))

(8)(选作)查询至少选修了学生“20110002”选修的全部课程的学生的学号,姓名。

select Sno,Sname from Student

4

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

SC3.cno=SC2.cno))

)

(9)检索学生的学号、姓名、学习课程名及课程成绩。

select s.Sno,Sname,Cname,Grade from Student s,Course c,SC

where s.Sno=sc.Sno and sc.Cno=c.Cno

5

本文来源:https://www.bwwdw.com/article/vudf.html

Top