一个班有40人每个学生要求记录其学号, 生名,性别,三门课程成绩。要求在子程序中完 指定课程的平均成绩,指定学生的平均成绩, 定性别的指定课程的平均成绩(可选)。

一个班有40人每个学生要求记录其学号,
生名,性别,三门课程成绩。要求在子程序中完
指定课程的平均成绩,指定学生的平均成绩,
定性别的指定课程的平均成绩(可选)。

温馨提示:若问题解决了,望给个采纳,谢谢!若有其他疑问随时咨询
1、效果如下

img

2、语句如下

--指定课程的平均成绩:查询语文的平均分
select avg(score) as '语文平均分' from score where course='语文'

--指定学生的平均成绩:查询语文的平均分
select avg(score) as '张三1平均分' from score where s_no=1

--指定性别和课程的平均成绩:查询女生英语平均分
select avg(score) '女生英语平均分' from score a
left join(
    select * from student where sex=0
) c on c.s_no=a.s_no
where a.course='英语' and c.sex is not null

3、创建表

--创建学生表
create table student(
    s_no int primary key,
    name varchar(50),
    sex tinyint
)

--创建成绩表
create table score(
    s_no int primary key,
    course varchar(50),
    score tinyint
)

4、模拟数据

--创建40个学生
declare @index int
set @index=0
while @index<40 begin
    set @index+=1

    insert into student(s_no,name,sex)
    values(@index,'张三'+convert(varchar,@index),round(rand()*1,0))
end

--创建学生分数 - 语文
declare @index2 int
set @index2=0
while @index2<40 begin
    set @index2+=1

    insert into score(s_no,course,score)
    values(@index2,'语文',60+round(rand()*40,0))
end

--创建学生分数 - 数学
declare @index3 int
set @index3=0
while @index3<40 begin
    set @index3+=1

    insert into score(s_no,course,score)
    values(@index3,'数学',60+round(rand()*40,0))
end

--创建学生分数 - 英语
declare @index4 int
set @index4=0
while @index4<40 begin
    set @index4+=1

    insert into score(s_no,course,score)
    values(@index4,'英语',60+round(rand()*40,0))
end

5、查询语句

select * from student
select * from score

select * from score where s_no=1
select * from student where sex=0


select a.*,c.sex from score a
left join(
    select * from student where sex=0
) c on c.s_no=a.s_no
where a.course='英语' and c.sex is not null