sql server实验11 嵌套查询、集合查询
1、下面是实验五的数据表,若已存在,则无需创建,若无,则执行实验报告五的相应脚本创建表并导入相应的数据。请用嵌套查询完成下列问题。
1.查询“c001”课程比“c002”课程成绩高的所有学生的学号;
select sno from sc c
where c.cno='c001' and exists (
select * from sc a
where a.sno=c.sno and a.cno='c002' and c.score>a.score)

2、1.查询所有同学的学号、姓名、选课数、总成绩。
思路:
① 从SC表中查出选课学号、选课数,总成绩
② 将得到的结果集与student表连接,要求控制总分数的小数位数两位,没有选课的学生选课门数为0,总分数为0,而不是显示NULL)



3、1.查询没学过“谌燕”老师课的同学的学号、姓名;(分别用in或EXISTS子查询),参考查询结果如下。



4、1.查询没有选修全部课程的同学的学号、姓名。(思路:分别用EXISTS、EXCEPT完成)



5、1.快速复制表SC至SC1,并在SC1表中删除相应的记录,这些记录满足课程是由“谌燕”老师所讲授。
select * into sc1 from sc
delete from sc1
where sc1.cno in
(
select cno from course c
left join teacher t on c.tno=t.tno
where t.tname='谌燕'
)

6、1.快速复制表SC至SC2,向SC2 表中插入一些记录,这些记录要求符合以下条件:
① sno的取值为没有上过编号为“c002”课程的同学学号;
② cno的取值为“c002”;
③ score的取值为“c002”号课的平均成绩。
提示:insert插入的记录可以是由SELECT查询所得到的结果集。
select * into sc2 from sc
insert into sc2(sno,cno,score)
select distinct student.sno,sc2.cno,
(
select avg(score)
from sc2
where cno='c002'
)
from student,sc2
where not exists
(
select * from sc2
where cno='c002' and sc2.sno=student.sno
) and sc2.cno='c002'
select * from sc2
