一道sql 的面试题

表结构:  

 • 成绩表(Grade),包含字段:GradeID(Int,自增), SNO(int, 学号), CNO(int, 课程号), Score(float,分数)  

查询每门课程的平均(最高/最低)分及课程号;  

 select avg(score) ,cno from [DBTest].[dbo].[grade] group by cno

 select max(score) ,cno from [DBTest].[dbo].[grade] group by cno

 select min(score) ,cno from [DBTest].[dbo].[grade] group by cno  

查询每门课程第1名的学生的学号;  

select sno from [DBTest].[dbo].[grade] as A   

where sno in

    (   

select top 1 sno from [DBTest].[dbo].[grade] as B where B.cno=A.cno order by score desc   

 ) 

查询每门课程中超过平均分的所有学生的学号等等。

 select * from [DBTest].[dbo].[grade] as A   

 where score >   

 (    

select avg(score)  from [DBTest].[dbo].[grade]as C where c.cno=a.cno

   )

posted @ 2011-11-30 15:19  xiaoshiyilang  阅读(129)  评论(0编辑  收藏  举报