題目: 成績表(Grade),包含字段:GradeID(Int,自增), SNO(int, 學號), CNO(int, 課程號), Score(float,分數(shù)) 查詢每門課程的平均(最高/最低)分及課程號; 查詢每門課程第1名的學生的學號; 查詢每門課程中超過平均分的所有學生的學號等等; 解答: 創(chuàng)建表:
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)新聞熱點
疑難解答