标签:
一、表说明(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
标签:
原文地址:http://www.cnblogs.com/wql025/p/4957313.html