一、数据表介绍

1.学生表
Student(SId,Sname,Sage,Ssex)
SId 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别

2.课程表
Course(CId,Cname,TId)
CId 课程编号,Cname 课程名称,TId 教师编号

3.教师表
Teacher(TId,Tname)
TId 教师编号,Tname 教师姓名

4.成绩表
SC(SId,CId,score)
SId 学生编号,CId 课程编号,score 分数

二、创建表的素材

学生表 Student

create table Student(SId varchar(10),Sname varchar(10),Sage datetime,Ssex varchar(10));

insert into Student values(’01’ , ‘赵雷’ , ‘1990-01-01’ , ‘男’);

insert into Student values(’02’ , ‘钱电’ , ‘1990-12-21’ , ‘男’);

insert into Student values(’03’ , ‘孙风’ , ‘1990-12-20’ , ‘男’);

insert into Student values(’04’ , ‘李云’ , ‘1990-12-06’ , ‘男’);

insert into Student values(’05’ , ‘周梅’ , ‘1991-12-01’ , ‘女’);

insert into Student values(’06’ , ‘吴兰’ , ‘1992-01-01’ , ‘女’);

insert into Student values(’07’ , ‘郑竹’ , ‘1989-01-01’ , ‘女’);

insert into Student values(’09’ , ‘张三’ , ‘2017-12-20’ , ‘女’);

insert into Student values(’10’ , ‘李四’ , ‘2017-12-25’ , ‘女’);

insert into Student values(’11’ , ‘李四’ , ‘2012-06-06’ , ‘女’);

insert into Student values(’12’ , ‘赵六’ , ‘2013-06-13’ , ‘女’);

insert into Student values(’13’ , ‘孙七’ , ‘2014-06-01’ , ‘女’);

科目表 Course

create table Course(CId varchar(10),Cname nvarchar(10),TId varchar(10));

insert into Course values(’01’ , ‘语文’ , ’02’);

insert into Course values(’02’ , ‘数学’ , ’01’);

insert into Course values(’03’ , ‘英语’ , ’03’);

教师表 Teacher

create table Teacher(TId varchar(10),Tname varchar(10));

insert into Teacher values(’01’ , ‘张三’);

insert into Teacher values(’02’ , ‘李四’);

insert into Teacher values(’03’ , ‘王五’);

成绩表 SC

create table SC(SId varchar(10),CId varchar(10),score decimal(18,1));

insert into SC values(’01’ , ’01’ , 80);

insert into SC values(’01’ , ’02’ , 90);

insert into SC values(’01’ , ’03’ , 99);

insert into SC values(’02’ , ’01’ , 70);

insert into SC values(’02’ , ’02’ , 60);

insert into SC values(’02’ , ’03’ , 80);

insert into SC values(’03’ , ’01’ , 80);

insert into SC values(’03’ , ’02’ , 80);

insert into SC values(’03’ , ’03’ , 80);

insert into SC values(’04’ , ’01’ , 50);

insert into SC values(’04’ , ’02’ , 30);

insert into SC values(’04’ , ’03’ , 20);

insert into SC values(’05’ , ’01’ , 76);

insert into SC values(’05’ , ’02’ , 87);

insert into SC values(’06’ , ’01’ , 31);

insert into SC values(’06’ , ’03’ , 34);

insert into SC values(’07’ , ’02’ , 89);

insert into SC values(’07’ , ’03’ , 98);

三、题目要求

1.查询” 01 “课程比” 02 “课程成绩高的学生的信息及课程分数

1.1查询同时存在” 01 “课程和” 02 “课程的情况

1.2查询存在” 01 “课程但可能不存在” 02 “课程的情况(不存在时显示为null )

1.3查询不存在” 01 “课程但存在” 02 “课程的情况

2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩

3.查询在 SC 表存在成绩的学生信息

4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为null )

4.1.查有成绩的学生信息

5.查询「李」姓老师的数量

6.查询学过「张三」老师授课的同学的信息

7.查询没有学全所有课程的同学的信息

8.查询至少有一门课与学号为” 01 “的同学所学相同的同学的信息

9.查询和” 01 “号的同学学习的课程完全相同的其他同学的信息

10.查询没学过”张三”老师讲授的任一门课程的学生姓名

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

12.检索” 01 “课程分数小于60,按分数降序排列的学生信息

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

14.查询各科成绩最高分、最低分和平均分:

以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率

及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90

要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列

15.按各科成绩进行排序,并显示排名,Score重复时保留名次空缺

15.1按各科成绩进行排序,并显示排名,Score重复时合并名次

16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺

16.1查询学生的总成绩,并进行排名,总分重复时不保留名次空缺

17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比

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

19.查询每门课程被选修的学生数

20.查询出只选修两门课程的学生学号和姓名

21.查询男生、女生人数

22.查询名字中含有「风」字的学生信息

23.查询同名同性学生名单,并统计同名人数

24.查询 1990 年出生的学生名单

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

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

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

28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况)

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

30.查询不及格的课程

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

32.求每门课程的学生人数

33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩

34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩

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

36.查询每门功成绩最好的前两名

37.统计每门课程的学生选修人数(超过 5 人的课程才统计)

38.检索至少选修两门课程的学生学号

39.查询选修了全部课程的学生信息

40.查询各学生的年龄,只按年份来算

41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一

42.查询本周过生日的学生

43.查询下周过生日的学生

44.查询本月过生日的学生

45.查询下月过生日的学生

四、完成步骤

4.1 登录MySQL并创建表

①登录MySQL并切换数据库

②分别创建学生表、课程表、教师表、成绩表

4.2 插入数据

①为Student表增加数据

②为Course表增加数据

③为Teacher表增加数据

④为SC表增加数据

4.3查询” 01 “课程比” 02 “课程成绩高的学生的信息及课程分数

①命令

#方法一mysql> select s.* , b.score `课程01的分数`,c.score `课程02的分数` from Student s , SC b , SC c-> where s.SId = b.SId and s.SId = c.SId and b.CId = '01' and c.CId = '02'and b.score > c.score;#方法二因为需要全部的学生信息,则需要在SC表中得到符合条件的SId后与Student表进行join,可以left join也可以 right joinmysql> select * from Student RIGHT JOIN (-> select t1.SId, class1, class2 from-> (select SId, score as class1 from SC where SC.CId = '01')as t1,-> (select SId, score as class2 from SC where SC.CId = '02')as t2-> where t1.SId = t2.SId AND t1.class1 > t2.class2-> )r-> on Student.SId = r.SId;mysql> select * from(-> select t1.SId, class1, class2-> from-> (SELECT SId, score as class1 FROM SC WHERE SC.CId = '01') AS t1,-> (SELECT SId, score as class2 FROM SC WHERE SC.CId = '02') AS t2-> where t1.SId = t2.SId and t1.class1 > t2.class2-> )r-> LEFT JOIN Student-> ON Student.SId = r.SId;#方法三mysql> select * from-> (select SId,CId,score from SC where CId = '01') t1 inner join (select SId,CId,score from SC where CId ='02') t2-> on t1.SId=t2.SId and t1.score>t2.score;

②结果

4.3.1查询同时存在” 01 “课程和” 02 “课程的情况

①命令

#方法一mysql> select * from (select SId ,score from SC where SC.CId='01')as t1 , (select SId ,score from SC where SC.CId='02') as t2-> where t1.SId=t2.SId;#方法二mysql> select * from (select SId ,score from SC where SC.CId='01')as t1 inner join (select SId ,score from SC where SC.CId='02') as t2-> on t1.SId=t2.SId;#方法三mysql> select * from (select SId ,score from SC where SC.CId='01')as t1 left join (select SId ,score from SC where SC.CId='02') as t2-> on t1.SId=t2.SId where t2.score is not null;

②结果

4.3.2 查询存在” 01 “课程但可能不存在” 02 “课程的情况(不存在时显示为null )

①命令

#这一道就是明显需要使用join的情况了,02可能不存在,即为left join的右侧或right join的左侧即可.#方法一mysql> select * from-> (select * from SC where SC.CId = '01') as t1-> left join-> (select * from SC where SC.CId = '02') as t2-> on t1.SId = t2.SId;#方法二mysql> select * from-> (select * from SC where SC.CId = '02') as t2-> right join-> (select * from SC where SC.CId = '01') as t1-> on t1.SId = t2.SId;

②结果

4.3.3查询不存在” 01 “课程但存在” 02 “课程的情况

①命令

mysql> select * from SC-> where SC.SId not in (select SId from SC where SC.CId = '01')-> AND SC.CId= '02';

②结果

4.4查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩

①命令

#方法一mysql> select SC.SId,s.Sname,avg(score)-> from SC,Student as s-> where SC.SId=s.SId-> group by SC.SId, s.Sname-> having avg(score)>=60;#方法二(思路:这里只用根据学生ID把成绩分组,对分组中的score求平均值,最后在选取结果中AVG大于60的即可。注意:这里必须要给计算得到的AVG结果一个alias(AS ss)得到学生信息的时候既可以用join也可以用一般的联合搜索)mysql> select Student.SId,Sname,ss from Student,(-> select SId, avg(Score) as ss from SC-> group by SId-> having avg(score)> 60-> )r-> where Student.SId = r.SId;mysql> select s.SId,ss,Sname from(-> select SId, avg(score) as ss from SC-> group by SId-> having avg(score)> 60-> )r left join-> (select Student.SId, Student.Sname from-> Student)s on s.SId = r.SId;

②结果

4.5查询在 SC 表存在成绩的学生信息

①命令

mysql> select distinct s.* from Student s,SC where s.SId=SC.SId;

②结果

4.6查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为null )

①命令

mysql> select s.SId, s.Sname,r.coursenumber,r.scoresum-> from (-> (select Student.SId,Student.Sname-> from Student-> )s-> left join-> (select-> SC.SId, sum(SC.score) as scoresum, count(SC.CId) as coursenumber-> from SC-> group by SC.SId-> )r->on s.SId = r.SId-> );

②结果

4.6.1查有成绩的学生信息

①命令

注意:IN()适合B表比A表数据小的情况;EXISTS()适合B表比A表数据大的情况#方法一mysql> select * from Student-> where Student.SId in (select SC.SId from SC);#方法二mysql> select * from Student-> where exists (select SC.SId from SC where Student.SId = SC.SId);

②结果

4.7查询「李」姓老师的数量

①命令

mysql> select count(Tname) from Teacher where Tname like'李%';

②结果

4.8查询学过「张三」老师授课的同学的信息

①命令

mysql> select Student.* from Student,Teacher,Course,SC-> where-> Student.SId = SC.SId-> and Course.CId=SC.CId-> and Course.TId = Teacher.TId-> and Tname = '张三';

②结果

4.9查询没有学全所有课程的同学的信息

①命令

#利用逆向思维mysql> select * from Student-> where Student.SId not in (-> select SC.SId from SC-> group by SC.SId-> having count(SC.CId)= (select count(CId) from Course)-> );

②结果

4.10查询至少有一门课与学号为” 01 “的同学所学相同的同学的信息

①命令

mysql> select * from Student-> where Student.SId in (-> select SC.SId from SC-> where SC.CId in(-> select SC.CId from SC-> where SC.SId = '01'-> )-> );

②结果

4.11查询和” 01 “号的同学学习的课程完全相同的其他同学的信息

①命令

mysql> select s1.* from Student s1 inner join-> (select SC.SId from SC where CId in-> (select CId from SC where SId ='01') and SId !='01'-> group by SC.SId having count(distinct CId) =-> (select count(distinct CId) from SC where SId='01')) s2 on s1.SId= s2.SId;

②结果

4.12查询没学过”张三”老师讲授的任一门课程的学生姓名

①命令

#方法一mysql> select * from Student-> where Student.SId not in(-> select SC.SId from SC where SC.CId in(-> select Course.CId from Course where Course.TId in(-> select Teacher.TId from Teacher where Tname = "张三"-> )-> )-> );#方法二mysql> select * from Student-> where Student.SId not in(-> select SC.SId from SC,Course,Teacher-> where-> SC.CId = Course.CId-> and Course.TId = Teacher.TId-> and Teacher.Tname= "张三"-> );

②结果

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

①命令

mysql> select Student.SId, Student.Sname,b.avg-> from Student right join-> (select SId, avg(score) as avg from SC-> where SId in (-> select SId from SC-> where score group by SId-> having count(score)>1)-> group by SId) b on Student.SId=b.SId;

②结果

4.14检索” 01 “课程分数小于60,按分数降序排列的学生信息

①命令

mysql> select Student.*, SC.score from Student, SC-> where Student.SId = SC.SId-> and SC.score  and CId = "01"-> order by SC.score desc;

②结果

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

①命令

mysql> select *from SC-> left join (-> select SId,avg(score) as avscore from SC-> group by SId-> )r-> on SC.SId = r.SId-> order by avscore desc;

②结果

4.16查询各科成绩最高分、最低分和平均分

以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率

及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90

要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列

①命令

mysql> select-> SC.CId ,-> max(SC.score) as 最高分,-> min(SC.score) as 最低分,-> avg(SC.score) as 平均分,-> count(*) as 选修人数,-> sum(case when SC.score >= 60 then 1 else 0 end ) / count(*) as 及格率,-> sum(case when SC.score >= 70 and SC.score  sum(case when SC.score >= 80 and SC.score  sum(case when SC.score >= 90 then 1 else 0 end ) / count(*) as 优秀率-> from SC-> group by SC.CId-> order by count(*) desc, SC.CId asc;

②结果

4.17按各科成绩进行排序,并显示排名,Score重复时保留名次空缺

①命令

 mysql> select *,->row_number() over (partition by CId order by score desc) as 排名-> from SC;

②结果

4.17.1按各科成绩进行排序,并显示排名,Score重复时合并名次

①命令

 mysql> select *,->rank() over (partition by CId order by score desc) as 排名-> from SC;

②结果

4.18查询学生的总成绩,并进行排名,总分重复时保留名次空缺

①命令

 mysql> select *,-> rank() over (order by sum_score desc) as 排名-> from (select SId,sum(score) as sum_score-> from SC-> group by SId) as a;

②结果

4.18.1查询学生的总成绩,并进行排名,总分重复时不保留名次空缺

①命令

mysql> select *,-> dense_rank() over (order by sum_score desc) as 排名-> from (select SId,sum(score) as sum_score-> from SC-> group by SId) as a;

②结果

4.19 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比

①命令

mysql> select Course.Cname, Course.CId,-> sum(case when SC.score85 then 1 else 0 end) as "[100-85]",-> sum(case when SC.score70 then 1 else 0 end) as "[85-70]",-> sum(case when SC.score60 then 1 else 0 end) as "[70-60]",-> sum(case when SC.score0 then 1 else 0 end) as "[60-0]"-> from SC left join Course-> on SC.CId = Course.CId-> group by Course.Cname,SC.CId;

②结果

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

①命令

mysql> select a.SId,a.CId,a.score from SC a-> left join SC b on a.CId = b.CId and a.score group by a.CId, a.SId,a.score-> having count(b.CId) order by a.CId;

②结果

4.21查询每门课程被选修的学生数

①命令

mysql> select CId, count(SId) from SC-> group by CId;

②结果

4.22查询出只选修两门课程的学生学号和姓名

①命令

mysql> select Student.SId, Student.Sname from Student-> where Student.SId in-> (select SC.SId from SC-> group by SC.SId-> having count(SC.CId)=2-> );

②结果

4.23查询男生、女生人数

①命令

mysql> select Ssex, count(*) from Student-> group by Ssex;

②结果

4.24查询名字中含有「风」字的学生信息

①命令

mysql> select *-> from Student-> where Student.Sname like '%风%';

②结果

4.25查询同名同性学生名单,并统计同名人数

①命令

mysql> select Sname, count(*) from Student-> group by Sname-> having count(*)>1;

②结果

4.26 查询 1990 年出生的学生名单

①命令

mysql> select *-> from Student-> where year(Student.Sage)=1990;

②结果

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

①命令

mysql> select SC.CId, Course.Cname, avg(SC.score) as average from SC,Course-> where SC.CId = Course.CId-> group by SC.CId,Course.Cname-> order by average desc,CId asc;

②结果

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

①命令

mysql> select Student.SId, Student.Sname, avg(SC.score) as aver from Student, SC-> where Student.SId = SC.SId-> group by SC.SId, Student.Sname-> having aver > 85;

②结果

4.29 查询课程名称为「数学」,且分数低于 60 的学生姓名和分数

①命令

mysql> select Student.Sname, SC.score from Student, SC, Course-> where Student.SId = SC.SId-> and Course.CId = SC.CId-> and Course.Cname = "数学"-> and SC.score < 60;

②结果

4.30 查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况)

①命令

mysql> select Student.Sname, CId, score from Student-> left join SC-> on Student.SId = SC.SId;

②结果

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

①命令

mysql> select Student.Sname, Course.Cname,SC.score from Student,Course,SC-> where SC.score>70-> and Student.SId = SC.SId-> and SC.CId = Course.CId;

②结果

4.32 查询不及格的课程

①命令

mysql> select CId from SC-> where score group by CId;

②结果

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

①命令

mysql> select Student.SId,Student.Sname-> from Student,SC-> where CId="01"-> and score>=80-> and Student.SId = SC.SId;

②结果

4.34 求每门课程的学生人数

①命令

mysql> select SC.CId,count(*) as 学生人数-> from SC-> group by SC.CId;

②结果

4.35 成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩

①命令

mysql> select Student.*, SC.score, SC.CId from Student, Teacher, Course,SC-> where Teacher.TId = Course.TId-> and SC.SId = Student.SId-> and SC.CId = Course.CId-> and Teacher.Tname = "张三"-> order by score desc-> limit 1;

②结果

4.36 成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩

①命令

mysql> select Student.*, SC.score, SC.CId from Student, Teacher, Course,SC-> where Teacher.TId = Course.TId-> and SC.SId = Student.SId-> and SC.CId = Course.CId-> and Teacher.Tname = "张三"-> and SC.score = (-> select Max(SC.score)-> from SC,Student, Teacher, Course-> where Teacher.TId = Course.TId-> and SC.SId = Student.SId-> and SC.CId = Course.CId-> and Teacher.Tname = "张三"-> );

②结果

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

①命令

mysql> selecta.CId, a.SId,a.score from SC as a-> inner join-> SC as b-> on a.SId = b.SId-> and a.CId != b.CId-> and a.score = b.score-> group by CId, SId, score;

②结果

4.38 查询每门功成绩最好的前两名

①命令

mysql> select a.SId,a.CId,a.score from SC as a-> left join SC as b-> on a.CId = b.CId and a.score group by a.CId, a.SId, score-> having count(b.CId) order by a.CId;

②结果

4.39 统计每门课程的学生选修人数(超过 5 人的课程才统计)

①命令

mysql> select SC.CId, count(SId) as cc from SC-> group by CId-> having cc >5;

②结果

4.40 检索至少选修两门课程的学生学号

①命令

mysql> select SId, count(CId) as cc from SC-> group by SId-> having cc>=2;

②结果

4.41 查询选修了全部课程的学生信息

①命令

mysql> select Student.*-> from SC ,Student-> where SC.SId=Student.SId-> group by SC.SId,Student.Sname,Sage,Ssex-> having count(*) = (select distinct count(*) from Course );

②结果

4.42 查询各学生的年龄,只按年份来算

①命令

mysql> select Student.SId as 学生编号,Student.Sname as 学生姓名,timestampdiff(year,Student.Sage,curdate()) as 学生年龄-> from Student;

②结果

4.43 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一

①命令

mysql> select-> case-> when curdate()  else timestampdiff(year,Student.Sage,curdate())-> end as 学生年龄-> from Student;

②结果

4.44 查询本周过生日的学生

①命令

mysql> select *-> from Student-> where weekofyear(Student.Sage)=weekofyear(curdate());

②结果

4.45 查询下周过生日的学生

①命令

mysql> select *-> from Student-> where weekofyear(Student.Sage)=weekofyear(curdate())+1;

②结果

4.46 查询本月过生日的学生

①命令

mysql> select *-> from Student-> where month(Student.Sage)=month(curdate());

②结果

4.47 查询下月过生日的学生

①命令

mysql> select *-> from Student-> where month(Student.Sage)=month(curdate()) + 1;

②结果