有个SQL查询语句请教大家: 选出 “语文”和“数学”都选修并且语文成绩比数学成绩高的所有学生的姓名

2024-12-01 11:24:21
推荐回答(2个)
回答1:

select aa.sid,s.sname from
(select sc.sid,sc.cid,sc.score,c.cname
from sc inner join cource c on sc.cid = c.cid
where c.cname in('语文','数学')) aa
inner join student s on aa.sid = s.sid
group by aa.sid,s.sname
having count(aa.*) > 1 --此处选出同时选择了“语文”和“嫌岩模数学”两门课的学生
and sum(case aa.cname when '语文' then aa.score else 0 end) >芹缓 sum(case aa.cname when '数学' then aa.score else 0 end) -- 次数选出“语文”成绩大于“数学”成绩的学枣大生

回答2:

select db.sname
from
(select a.sname,c.cname,b.score
from student as a
inner join sc as b
on a.sid=b.sid
inner join course as c
on b.cid=c.cid and c.cname='语文') as db --姓名租中、科目(语文)、成绩
inner join (select a.sname,c.cname,b.score
from student as a
inner join sc as b
on a.sid=b.sid
inner join course as c
on b.cid=c.cid and c.cname='数学') as db2 --姓名、科目(数瞎型帆学)、成磨雹绩
on db.sname=db2.sname
where db.score>db2.score --语文成绩大于数学成绩