题目: 成绩表(Grade),包含字段:GradeID(Int,自增), SNO(int, 学号), CNO(int, 课程号), Score(float,分数) 查询每门课程的平均(最高/最低)分及课程号; 查询每门课程第1名的学生的学号; 查询每门课程中超过平均分的所有学生的学号等等; 解答: 创建表:
1.查询每门课程的平均(最高/最低)分及课程号:
Select AVG(Score) as AvgScore, CNO from Grade group by CNOSelect MAX(Score) as MaxScore, CNO From grade group by cno2.查询每门课程第1名的学生的学号:
select * from Grade a where not exists( select 1 from grade b where b.cno = a.cno and (b.score < a.score or (b.score = a.score and gradeid < a.gradeid)))3.查询每门课程中超过平均分的所有学生的学号:
select * from grade a where not exists( select * from(select AVG(score) avgScore, cno from grade group by cno) b where a.cno = b.cno and a.score < b.avgScore)新闻热点
疑难解答