(1)查询软件学院的学生的选课及考试成绩情况,查询结果中包括:学号、姓名、课程名,成绩。
select student.sno,sname,cname,grade
from student,course,sc
where student.sno=sc.sno and sc.cno=course.cno and sdept='软件学院';
(2)查询每个同学考试的平均成绩,查询结果中包括:学号,姓名及平均成绩。
select student.sno,sname,avg(grade)
from student,sc
where student.sno=sc.sno
group by student.sno
(3)查询所有男生中选课门数在2门及以上的学生学号、姓名、及其选课门数。
select student.sno,sname,count(*)
from student,sc
where student.sno=sc.sno and ssex='男'
group by student.sno
having count(cno)>=2;
(4)针对每个学生所选修课程,找出该生所选修的课程中的最高成绩,查询结果中包括:学号,姓名及该生的最高成绩的分数,并且只显示最高成绩大于等于90分的相关信息(比如学生001,选了3门课程,找出该生在这3门课程考试中的最高分,如果最高分>=90,则出现在查询结果中)。
select student.sno,sname,max(grade)
from student,sc
where student.sno=sc.sno
group by student.sno
having max(grade)>=90;
(5)查询既选修了“2号课程”又选修了“3号课程”的学生学号与姓名。
select sc.sno,sname
from student,sc
where student.sno=sc.sno and cno ='2' and student.sno in
(select sno
from sc
where cno='3');
(6)查询学生所选修的课程超过其所选课程平均成绩的信息,查询结果包括:学号,课程号。(提示:用相关子查询)
select sno,cno
from sc x
where grade>=(select avg(grade)
from sc y
where x.sno=y.sno);
(7)查询学生所选修的课程超过其所选课程平均成绩的信息,查询结果包括:学号,姓名,课程号,课程名,成绩。(提示:用连接查询+相关子查询)
select student.sno,sname,x.cno,cname,grade
from student,sc x,course
where student.sno=x.sno and x.cno=course.cno
and grade>(select avg(grade)
from sc y
where y.sno=x.sno);
(8)查询被“孟文”所选修的课程号及课程名(要求用嵌套查询实现)
select cno,cname
from course
where cno in
(select cno
from sc
where sno in
(select sno
from student
where sname='孟文'));
(9) 查询至少选修了学生201215121所选修的全部课程的学生学号。(如果没有查询结果,则应该调整相应数据表中的数据,使得至少有1个查询结果)。(选做)
select distinct sno
from sc scx
where not EXISTS
(select *
from sc scy
where scy.sno='201215121' and not EXISTS
(select *
from sc scz
where scx.sno=scz.sno and scz.cno=scy.cno));
(10) 查询至少选修了学生201215121所选修的全部课程的学生学号及姓名。(如果没有查询结果,则应该调整相应数据表中的数据,使得至少有1个查询结果)。(选做)
select sname,sno
from student
where not EXISTS
(select *
from sc scx
where scx.sno='201215121' and not EXISTS
(select *
from sc scy
where scy.sno=student.sno and scy.cno=scx.cno));