查询存在两门(包含两门)以上课程不及格的学生姓名

2024-12-25 05:33:46
推荐回答(4个)
回答1:

回答2:

刚正好在做这题,我这边是ok的。你自己可以去试试。(因为是测试,所以起名有些随意)
-- 查询所有有两门/或以上成绩小于60的学生姓名和平均成绩
SELECT s.sname,sss.mm from
(SELECT sc.sno,avg(sc.score) mm FROM sc WHERE sc.sno IN
(SELECT sc.sno FROM sc WHERE sc.score <60 GROUP BY sc.sno HAVING COUNT(sno)>1 )
GROUP BY sc.sno) sss
LEFT JOIN s on sss.sno = s.sno
解释:(SELECT sc.sno FROM sc WHERE sc.score <60 GROUP BY sc.sno HAVING COUNT(sno)>1 ) ——————是用来锁定包含两门不及格的学生的学号,并作为下面查询的约束条件
(SELECT sc.sno,avg(sc.score) mm FROM sc WHERE sc.sno IN ——————结合上面筛选出来的学号,计算出平均成绩,因为使用了avg函数所以需要排序GROUP BY sc.sno) sss,将中间表取名sss
LEFT JOIN s on sss.sno = s.sno——————用外链接关联学生信息表

回答3:

select * from student where sno in(
select sno from sc where score<60 group by sno having count(cno)>=2)

回答4:

select student.sname from student,sc
where score <60