回答MagicianLiu的一道SQL题

原题:http://blog.csdn.net/MagicianLiu/archive/2009/04/27/4128073.aspx

 

scores有四个字段,学生stu、班级class、学院institute、分数score,

要求返回:班级考试人数大于10、班级最低分在50分以上、计算机学院、班级平均分从高到低前10名。

 

我的回答:

select top 5 count(stu), min(score), avg(score), class, institute from scores group by class, institute having count(stu) > 10 and min(score) >= 50 and institute = 'institue_computer' order by avg(score) desc

创建表和准备数据的SQL:

CREATE TABLE [dbo].[Scores]( [Stu] [nvarchar](50) NOT NULL, [Class] [nvarchar](50) NOT NULL, [Institute] [nvarchar](50) NOT NULL, [Score] [int] NOT NULL ) ON [PRIMARY] declare @i int, @j int select @i =1 while (@i <= 20) begin select @j=1 while (@j <=@i) begin insert Scores(stu, class, institute, score) Values('stu' + cast(@j as varchar(10)), 'class' + cast(@i as varchar(10)), 'institue_computer', @j*10+50) select @j=@j+1 end select @i=@i+1 end

你可能感兴趣的:(回答MagicianLiu的一道SQL题)