我个人觉得这道题还是值得研究一下的,题目如下:
有一张表,数据如下:
现有以下几个问题:
1、查询出科目成绩都大于80分的学生的名字?
2、查询出每个科目的第一名的学生的名字[依次是语文、数学]?
3、查询出总成绩排名,降序展示?
注意,以上查询结果只要名字,不要其他的值!
下面是我自己的解决方案,不知道是否有更好的解决方案额。
第一个问题,我有两个解决方案:
select a.name from grade a left join grade b on b.name = a.name where a.score > 80 and b.score > 80 and a.subject != b.subject group by a.name;
或者
select name
from grade
group by name
having sum(score > 80) > 1;
第二个问题,我的解决方案是:
select name from grade a left join (select max(score) as num, subject from grade group by subject order by subject) as b on a.score = b.num where a.subject = b.subject;
第三个问题,我的解决方案是:
select name from (select sum(score) as num, name from grade group by name order by num desc) as a
问题描述:已知关系模式: S (SNO,SNAME,SAGE, SSEX, SDEPT) 学生关系。SNO 为学号,SNAME 为姓名,SAGE为年龄, SSEX为性别, SDEPT为系别 C (CNO,CNAME,CTEACHER) 课程关系。CNO 为课程号,CNAME 为课程名,CTEACHER 为任课教师 SC(SNO,CNO,SCGRADE) 选课关系。SCGRADE 为成绩
SELECT COUNT(*) FROM S WHERE EXISTS( SELECT * FROM SC WHERE SC.SNO = S.SNO )
SELECT COUNT(DISTINCT SNO) FROM SC;
S表:s#(学号),sn(学生姓名) C表:c#(课程号),cn(课程名称) SC表:s#(学号),c#(课程号),gr(成绩) T表:t#(教师号),tn(教师姓名),c#(所讲课程号) 检索没有选修c1课程且选修课程数为两门的学生的姓名和平均成绩并按平均成绩降序排列。 Select sn as 姓名,avg(gr) as 平均成绩 From s,sc where sn not in (select sn from s,sc where c#!='c1' and s.s#=sc.s# group by sn having count(*)=2) group by sn order by avg(gr) desc上面是我自己写的查询语句,但有些问题,想不通要怎么修改,求助~~~~~~~
Select a.sn as 姓名,avg(c.gr) as 平均成绩 From s a,c b,sc c where a.s#=c.s# and b.c#=c.c# and b.cn!='c1' group by a.sn having count(*)=2 order by avg(c.gr) desc