学生各门课程成绩统计MySQL语句大全(面试题)

 题目参照:

https://www.cnblogs.com/lsgcoder101/p/6011059.html

启动数据库服务(windows)

net start mysql

 

登录数据库

mysql -uroot -p

 

# 创建和选择数据库

create database dbo;

use dbo;

# 创建数据表

create table stuscore
(
name char(50) NULL,
subject char(50) NULL ,
score int NULL,
stuid int NULL,    
record_id int not null auto_increment, # 主键不能为null(否则此语句出错)
PRIMARY KEY (record_id)
) engine=InnoDB; #存储引擎是InnoDB

#插入数据

insert into dbo.stuscore values ('张三','数学',89,1,null);    # 自增主键可以给null
insert into dbo.stuscore values ('张三','语文',80,1,null);
insert into dbo.stuscore values ('张三','英语',70,1, null);
insert into dbo.stuscore values ('李四','数学',90,2,null);
insert into dbo.stuscore values ('李四','语文',70,2,null);
insert into dbo.stuscore values ('李四','英语',80,2,null);

结果:

学生各门课程成绩统计MySQL语句大全(面试题)_第1张图片

#问题:

#1. 计算每个人的总成绩并排名(要求显示字段:姓名,总成绩)

select name, sum(score) as allscore
from stuscore
group by name
order by allscore;

结果:

学生各门课程成绩统计MySQL语句大全(面试题)_第2张图片

 

2.计算每个人的总成绩并排名(要求显示字段: 学号,姓名,总成绩)

select stuid,name,SUM(score) as allscore from dbo.stuscore
group by name,stuid
order by allscore;

 

3.计算每个人单科的最高成绩(要求显示字段: 学号,姓名,课程,最高成绩)

select t1.stuid, t1.name, t1.subject, t1.score from
stuscore t1, (select stuid, max(score) as maxscore from stuscore group by stuid) t2
where t1.stuid=t2.stuid and t1.score=t2.maxscore;

 

4.计算每个人的平均成绩(要求显示字段: 学号,姓名,平均成绩)

select stuid, name, AVG(score) avgscore
from dbo.stuscore
group by stuid, name;

 

5.列出各门课程成绩最好的学生(要求显示字段: 学号,姓名,科目,成绩)

select t1.stuid, t1.name, t1.subject, t1.score
from (
    select subject, max(score) as maxscore
    from stuscore
    group by subject
)as t2 left outer join stuscore as t1 on t1.subject = t2.subject and t1.score = t2.maxscore;

 

6.列出各门课程成绩最好的两位学生(要求显示字段: 学号,姓名,科目,成绩)

SELECT a.name, a.subject, a.score
FROM stuscore AS a
WHERE
(
    SELECT COUNT(*)
    FROM stuscore AS b
    WHERE b.subject = a.subject AND b.score >= a.score
) <= 2    # 比 a.score 成绩更好的score的数量 <= 2
ORDER BY a.subject ASC, a.score DESC;

 

7.统计如下表格:

学号    姓名    语文    数学    英语    总分    平均分

# 统计每个同学的总分和平均分
select t1.stuid, t1.name, sum(score) as allscore, avg(score) as avgscore
from stuscore as t1
group by t1.stuid, t1.name;

# 统计每个同学分数以及总分和平均分
select stuid, name,
sum(case when subject='语文' then score else 0 end )as 语文,
sum(case when subject='数学' then score else 0 end )as 数学,
sum(case when subject='英语' then score else 0 end )as 英语,
sum(score) as 总分, avg(score) as 平均分
from stuscore
group by stuid, name;

 

8.列出各门课程的平均成绩(要求显示字段:课程,平均成绩)

select subject as 课程, avg(score) as 平均分
from stuscore
group by subject;

 

9.列出数学成绩的排名(要求显示字段:学号,姓名,成绩,排名)

select stuid, name, score,
(
    select count(*)
    from stuscore t2
    where subject = '数学' and t2.score > t1.score
) + 1 as rank
from stuscore t1
where subject = '数学'
order by score desc;

 

10.列出数学成绩在2-3名的学生(要求显示字段:学号,姓名,科目,成绩)

select stuid, name, subject as 数学, score
from stuscore
where subject = '数学'
order by score desc
limit 2 offset 1;

 

11.求出李四的数学成绩的排名

select name, (select count(*) from stuscore as t2 where t2.score > t1.score) + 1 as rank
from stuscore as t1
where name = '李四' and subject = '数学';

 

12.统计如下:

课程    不及格(0-59)?个    良(60-80)?个    优(81-100)?个

select t1.subject,
(select count(*) from stuscore t2 where t2.subject = t1.subject and t2.score < 60) as 不及格,
(select count(*) from stuscore t2 where t2.subject = t1.subject and t2.score >= 60 and t2.score < 80) as 良,
(select count(*) from stuscore t2 where t2.subject = t1.subject and t2.score >= 80) as 优
from stuscore t1
group by t1.subject;

 

13.统计如下:

数学: 张三(50分),李四(90分),王五(90分),赵六(76分)

需要用到游标因此不会

你可能感兴趣的:(其他学习笔记,笔试题)