内容发布更新时间 : 2025/3/4 15:43:19星期一 下面是文章的全部内容请认真阅读。
实验四 简单查询和连接查询
1. 简单查询实验
用Transact-SQL语句表示下列操作,在“学生选课“数据库中实现其数据查询操作: (1) 查询数学系学生的学号和姓名。
select sno,sname
from student
where dept='数学系';
(2) 查询选修了课程的学生学号。 select distinct(sno)
from sc;
(3) 查询选修课程号为0101的学生学号和成绩,并要求对查询结果按成绩降序排列,如果成绩相同则按学号升序排列。
select distinct(sno),grade
from sc
where cno='0101'
order by grade desc,sno asc;
(4) 查询选修课程号为0101的成绩在80-90 分之间的学生学号和成绩,并将成绩乘以系数0.8 输出。
select distinct(sno),grade*0.8 as 'sore'
from sc
where cno='0101' and grade between 80 and 90;
(5) 查询数学系或计算机系姓张的学生的信息。 select *
from student
where dept in ('数学系','计算机系')and sname like '张%' ;
(6) 查询缺少了成绩的学生的学号和课程号。 select sno,cno
from sc
where grade is null;
2. 连接查询实验
用Transact-SQL语句表示,并在“学生选课”数据库中实现下列数据连接查询操作: (1) 查询每个学生的情况以及他(她)所选修的课程。 select student.*,course.cname
from student,sc,course
where student.sno=sc.sno and sc.cno=course.cno;
(2) 查询学生的学号、姓名、选修的课程名及成绩。 select student.sno,sname,cname,grade
from student,sc,course
where student.sno=sc.sno and sc.cno=course.cno;
(3) 查询选修离散数学 课程且成绩为90 分以上的学生学号、姓名及成绩。
- 1 -
select student.sno,sname,grade
from student,sc,course
where student.sno=sc.sno and sc.cno=course.cno
and cname='离散数学' and grade>=90;
(4) 查询每一门课的间接先行课(即先行课的先行课)。
select first.cno,second.pcno
from course as first,course as second where first.pcno=second.cno;
实验五 嵌套查询
用TransacTransact-SQL语句表示,在学生选课库中实现其数据嵌套查询操作: (l) 查询选修了离散数学的学生学号和姓名。
select sno,sname
from student where sno in (select sno from sc where cno= (select cno from course
where cname='离散数学'));
(2) 查询0101课程的成绩高于张林的学生学号和成绩。
select sno,grade
from sc
where cno='0101' and grade>
(select grade
from sc
where cno='0101' and sno=(select sno From student
Where sname='张林'));
(3) 查询其他系中年龄小于计算机系年龄最大者的学生。
select *
from student
where dept<>'计算机系' and age<(select max(age) from student
where dept='计算机系');
(4) 查询其他系中比计算机系学生年龄都小的学生。
(3)中的max换成min即可。
(5) 查询同牟万里数据库原理课程分数相同的学生的学号。
- 2 -
select sno
from sc
where grade=(select grade
from student,sc,course where student.sno=sc.sno and
sc.cno=course.cno and course.cname='数据库原理' and sname='牟万里');
(6) 查询选修了0206 课程的学生姓名。
select sname
from student
where sno in (select sno from sc
where cno='0206');
(7) 查询没有选修0206 课程的学生姓名。
在(5)的in前加not即可。
(8) 查询选修了全部课程的学生的姓名。 SELECT SNAME FROM STUDENT WHERE SNO IN (
SELECT SNO FROM SC
GROUP BY SNO
HAVING COUNT(*)=
( SELECT COUNT(*) FROM COURSE));
select sname
from student
where not exists (select *
from course
where not exists (select * from sc
where cno=course.cno));
sno=student.sno
and
(9) 查询与学号为“09001103”的学生所选修的全部课程相同的学生学号和姓名。
select sno,sname
From student
Where sno<>'09001103' and not exists( select * From sc as x
- 3 -