用SQL语句查询各门课程的平均成绩,结果按平均成绩排序·
创建表:
SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOSET ANSI_PADDING ONGOCREATE TABLE [dbo].[stuscore]
( [name] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[subject] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[score] [int] NULL,
[stuid] [int] NULL)
ON [PRIMARY]
GO
SET ANSI_PADDING OFF
插入数据:
insert into dbo.stuscore values ('张三','数学',89,1);
insert into dbo.stuscore values ('张三','语文',80,1);
insert into dbo.stuscore values ('张三','英语',70,1);
insert into dbo.stuscore values ('李四','数学',90,2);
insert into dbo.stuscore values ('李四','语文',70,2);
insert into dbo.stuscore values ('李四','英语',80,2);
查询结果如下:
列出各门课程的平均成绩:
select subject,AVG(score)平均成绩 from stuscore
group by subject;