学生——成绩表2.1

一、表说明(MYSql)

二、导入测试数据

学生表

insert into Student values(‘01‘ , N‘赵雷‘ , ‘1990-01-01‘ , N‘男‘);
insert into Student values(‘02‘ , N‘钱电‘ , ‘1990-12-21‘ , N‘男‘);
insert into Student values(‘03‘ , N‘孙风‘ , ‘1990-05-20‘ , N‘男‘);
insert into Student values(‘04‘ , N‘李云‘ , ‘1990-08-06‘ , N‘男‘);
insert into Student values(‘05‘ , N‘周梅‘ , ‘1991-12-01‘ , N‘女‘);
insert into Student values(‘06‘ , N‘吴兰‘ , ‘1992-03-01‘ , N‘女‘);
insert into Student values(‘07‘ , N‘郑竹‘ , ‘1989-07-01‘ , N‘女‘);
insert into Student values(‘08‘ , N‘王菊‘ , ‘1990-01-20‘ , N‘女‘);

课程表

insert into Course values(‘01‘ , N‘语文‘ , ‘02‘);
insert into Course values(‘02‘ , N‘数学‘ , ‘01‘);
insert into Course values(‘03‘ , N‘英语‘ , ‘03‘);

教师表

insert into Teacher values(‘01‘ , N‘张三‘);
insert into Teacher values(‘02‘ , N‘李四‘);
insert into Teacher values(‘03‘ , N‘王五‘);

成绩表

insert into Score values(‘01‘ , ‘01‘ , 80);
insert into Score values(‘01‘ , ‘02‘ , 90);
insert into Score values(‘01‘ , ‘03‘ , 99);
insert into Score values(‘02‘ , ‘01‘ , 70);
insert into Score values(‘02‘ , ‘02‘ , 60);
insert into Score values(‘02‘ , ‘03‘ , 99.5);
insert into Score values(‘03‘ , ‘01‘ , 80);
insert into Score values(‘03‘ , ‘02‘ , 99);
insert into Score values(‘03‘ , ‘03‘ , 80);
insert into Score values(‘04‘ , ‘01‘ , 50);
insert into Score values(‘04‘ , ‘02‘ , 30);
insert into Score values(‘04‘ , ‘03‘ , 20);
insert into Score values(‘05‘ , ‘01‘ , 76);
insert into Score values(‘05‘ , ‘02‘ , 87);
insert into Score values(‘06‘ , ‘01‘ , 31);
insert into Score values(‘06‘ , ‘03‘ , 34);
insert into Score values(‘07‘ , ‘02‘ , 89);
insert into Score values(‘07‘ , ‘03‘ , 98);

三、查询

成绩情况

目录:

1.      查询各科成绩第一名的记录

2.      查询各科成绩前三名的记录(x,y)

3.      查询所有学生的课程及分数情况

4.      按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩

5.      查询学生平均成绩及其名次

6.      查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩

7.      查询平均成绩小于60的同学的学生编号和学生姓名和平均成绩

8.      查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列

9.      查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩

10.   查询不及格的课程

11.   查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩

12.   查询score表中不存在成绩的学生信息的SQL语句

13.   查询课程名称为"数学",且分数低于60的学生姓名和分数

14.   检索"01"课程分数小于60按分数降序排列的学生信息

15.   查询课程编号为01且课程成绩在80分以上的学生的学号和姓名

16.   查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩

17.   查询s_id=7的学生的平均分

18.   查询任何一门课程成绩在70分以上的学生姓名、课程名称和分数

19.   查询"01"课程比"02"课程成绩高的学生的信息及课程分数

20.   查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩

-----------------------------------------------------------------------------------------------

1.查询各科成绩第一名的记录

select student.s_id,sname,cname,m.score from student,course,(select t.* , (select count(distinct score) from SCore
where C_id = t.C_id and score >= t.score)px from score t) m
where px between 1 and 1 and student.s_id=m.s_id and course.c_id=m.c_id
order by m.C_id , m.px

结果:

2.查询各科成绩前三名的记录

2.1分数重复时保留名次空缺(x)

select m.* , n.C_id , n.score from Student m, SCore n where m.S_id = n.S_id and n.score in
(select  score from score where C_id = n.C_id order by score desc ) order by n.C_id , n.score desc

2.2分数重复时不保留名次空缺,合并名次

select student.s_id,sname,cname,m.score from student,course,(select t.* , (select count(distinct score) from SCore
where C_id = t.C_id and score >= t.score)px from score t) m
where px between 1 and 3 and student.s_id=m.s_id and course.c_id=m.c_id
order by m.C_id , m.px

结果:

3.查询所有学生的课程及分数情况

select Student.* , Course.Cname , SCore.C_id , SCore.score
from Student, SCore , Course
where Student.S_id = SCore.S_id and SCore.C_id = Course.C_id
group by Student.S_id , SCore.C_id
order by Student.S_id , SCore.C_id

4.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩

注:1.ifnull(cast(avg(b.score) asdecimal(18,2)),0) 平均分 可以去掉ifnull,写成:cast(avg(b.score) as decimal(18,2)) 平均分

2.目前在score表中,如果某位同学没有参加考试则没有成绩记录,这样会导致计算平均分错误,如郑竹同学的平均分只计算了其(数学+英语)/2。正确的做法应该是即使某位同学没有参加某科目的考试,那么该同学在socre表也应有记录,只不过对应的记录中的成绩为0,这样计算平均分就不会有错。

select a.S_id 学生编号 , a.Sname 学生姓名 ,
       max(case c.Cname when ‘语文‘ then b.score else null end) 语文,
       max(case c.Cname when ‘数学‘ then b.score else null end) 数学,
       max(case c.Cname when ‘英语‘ then b.score else null end) 英语,
       ifnull(cast(avg(b.score) as decimal(18,2)),0) 平均分
from Student a
left join SCore b on a.S_id = b.S_id
left join Course c on b.C_id = c.C_id
group by a.S_id , a.Sname
order by 平均分 desc

结果:

5.查询学生平均成绩及其名次

方法1

select t1.* ,  (select count(1) from(
  select m.S_id 学生编号 ,
         m.Sname 学生姓名 ,
         ifnull(cast(avg(score) as decimal(18,2)),0) 平均成绩
  from Student m left join score n on m.S_id = n.S_id
  group by m.S_id , m.Sname
) t2  where 平均成绩 > t1.平均成绩) + 1 as 名次 from(
  select m.S_id 学生编号,
         m.Sname 学生姓名 ,
         ifnull(cast(avg(score) as decimal(18,2)),0) 平均成绩
  from Student m left join score n on m.S_id = n.S_id
  group by m.S_id , m.Sname
) t1
order by 名次

方法2

select t1.* ,  (select count(distinct 平均成绩) from(
  select m.S_id 学生编号 ,
         m.Sname 学生姓名 ,
         ifnull(cast(avg(score) as decimal(18,2)),0) 平均成绩
  from Student m left join SCore n on m.S_id = n.S_id
  group by m.S_id , m.Sname
) t2 where 平均成绩 >= t1.平均成绩)  as 名次 from(
  select m.S_id 学生编号 ,
         m.Sname 学生姓名 ,
         ifnull(cast(avg(score) as decimal(18,2)),0) 平均成绩
  from Student m left join SCore n on m.S_id = n.S_id
  group by m.S_id , m.Sname
) t1
order by 名次

结果:

6.查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩

select a.S_id , a.Sname , cast(avg(b.score) as decimal(18,2)) 平均分
from Student a , score b
where a.S_id = b.S_id
group by a.S_id , a.Sname
having cast(avg(b.score) as decimal(18,2)) >= 85
order by 平均分 desc

结果:

7.查询平均成绩小于60的同学的学生编号和学生姓名和平均成绩

7.1查询在score表存在成绩的学生信息的SQL语句。

select a.S_id , a.Sname , cast(avg(b.score) as decimal(18,2)) avg_score
from Student a , score b
where a.S_id = b.S_id
group by a.S_id, a.Sname
having cast(avg(b.score) as decimal(18,2)) < 60
order by a.S_id

7.2查询在score表中不存在成绩的学生信息的SQL语句。

select a.S_id, a.Sname , ifnull(cast(avg(b.score) as decimal(18,2)),0) avg_score
from Student a left join score b
on a.S_id = b.S_id
group by a.S_id , a.Sname
having ifnull(cast(avg(b.score) as decimal(18,2)),0) < 60
order by a.S_id

8. 查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列

select m.C_id , m.Cname , cast(avg(n.score) as decimal(18,2)) 平均分
from Course m, SCore n
where m.C_id = n.C_id
group by m.C_id , m.Cname
order by 平均分 desc, m.C_id asc

9.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩

9.1 查询所有有成绩的SQL

select a.s_id ‘学生编号‘, a.Sname ‘学生姓名‘, count(b.c_id) 选课总数, sum(score) ‘所有课程的总成绩‘
from Student a , SCore b
where a.s_id = b.s_id
group by a.s_id,a.Sname
order by sum(score) desc

9.2查询所有(包括有成绩和无成绩)的SQL

select a.s_id as ‘学生编号‘, a.Sname as ‘学生姓名‘, count(b.c_id) as ‘选课总数‘, sum(score) as ‘所有课程的总成绩‘
from Student a left join SCore b
on a.s_id = b.s_id
group by a.s_id,a.Sname
order by sum(score) desc

10.查询不及格的课程

select Student.* , Course.Cname , SCore.C_id, SCore.score
from Student, SCore , Course
where Student.S_id = SCore.S_id and SCore.C_id = Course.C_id and SCore.score < 60
order by Student.S_id , SCore.C_id

11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩

11.1查询在score表存在成绩的学生信息的SQL语句。

select student.S_id, student.sname , cast(avg(score) as decimal(18,2)) 平均分
from student , SCore
where student.S_id = SCore.S_id and student.S_id in (select S_id from SCore where score < 60 group by S_id having count(1) >= 2)
group by student.S_id , student.sname
order by 平均分 desc

11.2查询在score表不存在成绩的学生信息的SQL语句。

select a.S_id, a.Sname , ifnull(cast(avg(b.score) as decimal(18,2)),0) 平均分
from Student a left join score b
on a.S_id = b.S_id
group by a.S_id , a.Sname
having ifnull(cast(avg(b.score) as decimal(18,2)),0) < 60
order by 平均分 desc

12. 查询score表中不存在成绩的学生信息的SQL语句

select a.s_id, a.Sname , ifnull(cast(avg(b.score) as decimal(18,2)),0) avg_score
from Student a left join score b
on a.s_id = b.s_id
group by a.s_id , a.Sname
having ifnull(cast(avg(b.score) as decimal(18,2)),0) = 0
order by a.s_id

13.查询课程名称为"数学",且分数低于60的学生姓名和分数

select sname , score
from Student , SCore , Course
where SCore.S_id = Student.S_id and SCore.C_id = Course.C_id and Course.Cname = N‘数学‘ and score < 60

14.检索"01"课程分数小于60按分数降序排列的学生信息

select student.* , score.C_id , score.score from student , score
where student.S_id = score.S_id and score.score < 60 and score.C_id = ‘01‘
order by score.score desc

15.查询课程编号为01且课程成绩在80分以上的学生的学号和姓名

select Student.* , Course.Cname , SCore.C_id , SCore.score
from Student, SCore , Course
where Student.S_id = SCore.S_id and SCore.C_id = Course.C_id and SCore.C_id = ‘01‘ and SCore.score >= 80
order by Student.S_id , SCore.C_id

16.查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩

为了说明以下两种情况,现在改变一下数据:“张三”老师所授的课程编号为2,现在将学号为2的学生的该课程成绩由60改为99,这样成绩表中就有2条记录。

16.1当最高分只有一个时

select  Student.* , Course.Cname , SCore.C_id , SCore.score
from Student, SCore , Course , Teacher
where Student.S_id = SCore.S_id and SCore.C_id = Course.C_id and Course.T_id = Teacher.T_id and Teacher.Tname = N‘张三‘
order by SCore.score desc limit 0,1

16.2当最高分出现多个时

select Student.* , Course.Cname , SCore.C_id , SCore.score
from Student, SCore , Course , Teacher
where Student.S_id = SCore.S_id and SCore.C_id = Course.C_id and Course.T_id = Teacher.T_id and Teacher.Tname = N‘张三‘ and
SCore.score = (select max(SCore.score) from SCore , Course , Teacher where SCore.C_id = Course.C_id and Course.T_id = Teacher.T_id and Teacher.Tname = N‘张三‘)

17.查询s_id=1的学生的平均分

select a.S_id , a.Sname , cast(avg(b.score) as decimal(18,2)) avg_score
from Student a , score b
where a.S_id = b.S_id and a.S_id=1
group by a.S_id, a.Sname

18. 查询任何一门课程成绩在70分以上的学生姓名、课程名称和分数

select Student.* , Course.Cname , SCore.C_id , SCore.score
from Student, SCore , Course
where Student.S_id = SCore.S_id and SCore.C_id = Course.C_id and SCore.score >= 70
order by Student.S_id , SCore.C_id

19.查询"01"课程比"02"课程成绩高的学生的信息及课程分数

19.1查询同时存在"01"课程和"02"课程的情况

select a.* , b.score ‘课程"01"的分数‘,c.score ‘课程"02"的分数‘
from Student a , SCore b , SCore c
where a.S_id = b.S_id and a.S_id = c.S_id and b.C_id = ‘01‘ and c.C_id = ‘02‘ and b.score > c.score

19.2查询同时存在"01"课程和"02"课程的情况和存在"01"课程但可能不存在"02"课程的情况(不存在时显示为null)

select a.* , b.score ‘课程"01"的分数‘,c.score ‘课程"02"的分数‘ from Student a
left join SCore b on a.S_id = b.S_id and b.C_id = ‘01‘
left join SCore c on a.S_id = c.S_id and c.C_id = ‘02‘
where b.score > ifnull(c.score,0)

20. 查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩

方法1

select m.* from SCore m ,(select C_id , score from SCore group by C_id , score having count(1) > 1) n
where m.C_id= n.C_id and m.score = n.score
order by m.C_id , m.score , m.S_id

方法2

select m.* from SCore m where exists (select 1 from (select C_id , score from SCore group by C_id , score having count(1) > 1) n
where m.C_id= n.C_id and m.score = n.score)
order by m.C_id , m.score , m.S_id

时间: 2024-10-14 12:49:45

学生——成绩表2.1的相关文章

学生——成绩表2.2

一.表说明同“学生——成绩表2.1” 二.目录 选课情况 1.       查询学过"张三"老师授课的同学的信息 2.       查询没学过"张三"老师授课的同学的信息 3.       查询选修了全部课程的学生信息 4.       查询没有学全所有课程的同学的信息 5.       查询出只选有两门课程的全部学生的学号和姓名 6.       检索至少选修两门课程的学生学号 7.       查询每门课程被选修的学生数 8.       查询学过编号为&quo

学生成绩表 SQL练习题

create table Student ( Sno char(3) primary key, Sname char(8) not null, Ssex char(2) not null, Sbirthday datetime, Class char(5) ); create table Course ( Cno varchar(10) primary key, Cname varchar(10) not null, Tno char(3) references Teacher(Tno) );

sql从学生成绩表中查出各科成绩分布最多的记录

题目:有一个数据表student,包含name(姓名),dept(科目),score(成绩)三个字段,现在要求查出每科成绩分数分布最多的项,如语文成绩80分最多,就输出语文80分的人数,数学90分的人最多,输出数学90分的人数: 测试数据: insert into student values ("deng","80","语文"); insert into student values ("zhang","80&qu

mysql 学生成绩表例题

#查询姓张的每个学生的最低分大于60的学号和姓名 SELECT r.studentno,studentname FROM result r INNER JOIN student s ON r.`studentno`=s.`studentno` WHERE s.`studentname` LIKE '张%' GROUP BY r.`studentno` HAVING MIN(r.`score`)>60; #查询生日在'1988-1-1'后的学生的姓名和专业名称 SELECT studentname

学生成绩表(输入成绩后自动算出最高、最低、平均分)

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <h3>千峰H5成绩计算表</h3> <table> <tr> <td>班级名称:</td> <td><input type="

合并Excel工作薄中成绩表的VBA代码,非常适合教育一线的朋友_python

这时候还需要把各个工作表合并到一起来形成一个汇总表.这时候比较麻烦也比较容易出错,因为各个表的学号不一定都是一致的.对齐的.因为可能会有人缺考,有人会考号涂错等等.特奉献以下代码,用于合并学生成绩表或者其它类似的表都可以.本代码特点在于不需要使用SQL或者Access等大头软件,只需要Excel就可以执行,非常方便,速度也不慢.转载请勿清除广告. 没有合适的局域网管理软件吗?你的网管工具够灵活够高效吗?看看这个network management software. ' ============

C#打印成绩表代码

题目:一个班6个人,从键盘输入每个学号语文,数学,外语成绩(不需输入学号). 输出:学生成绩表(包括每个人的总分),每科的平均分.附加1:试着,把不及格的用红字显示.附加2:试着按照总分排序,显示名次出来. static void Main(string[] args) { int[,] a = new int[6, 5]; for (int i = 0; i < 6; i++) { a[i, 0] = i + 1; Console.Write("请输入第" + (i + 1)

sql 统计 关于学生成绩

文章来源于:http://www.cnblogs.com/tenghoo/archive/2007/06/11/779240.html 学生成绩表(stuscore): 姓名:name 课程:subject 分数:score 学号:stuid 张三 数学 89 1 张三 语文 80 1 张三 英语 70 1 李四 数学 90 2 李四 语文 70 2 李四 英语 80 2 创建表 SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_

设有一数据库,包括四个表:学生表(Student)、课程表(Course)、成绩表(Score)以及教师信息表(Teacher)。四个表的结构分别如表1-1的表(一)~表(四)所示,数据如表1-2的表(一)~表(四)所示。用SQL语句创建四个表并完成相关题目。

表(一)Student (学生表) -- Create table create table STUDENT ( sno VARCHAR2(3) not null, sname VARCHAR2(8) not null, ssex VARCHAR2(2) not null, sbirthday DATE, class VARCHAR2(5) ) tablespace USERS pctfree 10 initrans 1 maxtrans 255; -- Add comments to the