MySQL练习题50道

我们可以在自己电脑上安装MySQL,然后可以更方便的练习。推荐安装8.0以上版本,可以用窗口函数。

练习数据

数据表

  1. 学生表—Student(SId,Sname,Sage,Ssex)

    • 其中SId 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
+------+--------+---------------------+------+
| SId | Sname | Sage | Ssex |
+------+--------+---------------------+------+
| 01 | 赵雷 | 1990-01-01 00:00:00 | 男 |
| 02 | 钱电 | 1990-12-21 00:00:00 | 男 |
| 03 | 孙风 | 1990-05-20 00:00:00 | 男 |
| 04 | 李云 | 1990-08-06 00:00:00 | 男 |
| 05 | 周梅 | 1991-12-01 00:00:00 | 女 |
| 06 | 吴兰 | 1992-03-01 00:00:00 | 女 |
| 07 | 郑竹 | 1989-07-01 00:00:00 | 女 |
| 09 | 张三 | 2017-12-20 00:00:00 | 女 |
| 10 | 李四 | 2017-12-25 00:00:00 | 女 |
| 11 | 李四 | 2017-12-30 00:00:00 | 女 |
| 12 | 赵六 | 2017-01-01 00:00:00 | 女 |
| 13 | 孙七 | 2018-01-01 00:00:00 | 女 |
+------+--------+---------------------+------+
  1. 课程表—Course(CId,Cname,TId)
    • CId –课程编号,Cname 课程名称,TId 教师编号
1
2
3
4
5
6
7
+------+--------+------+
| CId | Cname | TId |
+------+--------+------+
| 01 | 语文 | 02 |
| 02 | 数学 | 01 |
| 03 | 英语 | 03 |
+------+--------+------+
  1. 教师表—Teacher(TId,Tname)
    • TId 教师编号,Tname 教师姓名
1
2
3
4
5
6
7
+------+--------+
| TId | Tname |
+------+--------+
| 01 | 张三 |
| 02 | 李四 |
| 03 | 王五 |
+------+--------+
  1. 成绩表—SC(SId,CId,score)
    • SId 学生编号,CId 课程编号,score 分数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
+------+------+-------+
| SId | CId | score |
+------+------+-------+
| 01 | 01 | 80.0 |
| 01 | 02 | 90.0 |
| 01 | 03 | 99.0 |
| 02 | 01 | 70.0 |
| 02 | 02 | 60.0 |
| 02 | 03 | 80.0 |
| 03 | 01 | 80.0 |
| 03 | 02 | 80.0 |
| 03 | 03 | 80.0 |
| 04 | 01 | 50.0 |
| 04 | 02 | 30.0 |
| 04 | 03 | 20.0 |
| 05 | 01 | 76.0 |
| 05 | 02 | 87.0 |
| 06 | 01 | 31.0 |
| 06 | 03 | 34.0 |
| 07 | 02 | 89.0 |
| 07 | 03 | 98.0 |
+------+------+-------+

创建测试数据

  1. 学生表 Student
1
2
3
4
5
6
7
8
9
10
11
12
13
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-05-20' , '男');
insert into Student values('04' , '李云' , '1990-08-06' , '男');
insert into Student values('05' , '周梅' , '1991-12-01' , '女');
insert into Student values('06' , '吴兰' , '1992-03-01' , '女');
insert into Student values('07' , '郑竹' , '1989-07-01' , '女');
insert into Student values('09' , '张三' , '2017-12-20' , '女');
insert into Student values('10' , '李四' , '2017-12-25' , '女');
insert into Student values('11' , '李四' , '2017-12-30' , '女');
insert into Student values('12' , '赵六' , '2017-01-01' , '女');
insert into Student values('13' , '孙七' , '2018-01-01' , '女');
  1. 科目表 Course
1
2
3
4
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')
  1. 教师表 Teacher
1
2
3
4
create table Teacher(TId varchar(10),Tname varchar(10))
insert into Teacher values('01' , '张三')
insert into Teacher values('02' , '李四')
insert into Teacher values('03' , '王五')
  1. 成绩表 SC
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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. 查询下月过生日的学生

个人解答

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

1
2
3
select *
from (select SId, CId, score from sc where sc.CId='01')as t1 , (select SId, CId, score from sc where sc.CId='02') as t2
where t1.SId=t2.SId and t1.score>t2.score;
1
2
3
4
5
6
+------+------+-------+------+------+-------+
| sid | cid | score | sid | cid | score |
+------+------+-------+------+------+-------+
| 02 | 01 | 70.0 | 02 | 02 | 60.0 |
| 04 | 01 | 50.0 | 04 | 02 | 30.0 |
+------+------+-------+------+------+-------+

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

1
2
3
select *
from (select SId, CId, score from sc where sc.CId='01')as t1 , (select SId, CId, score from sc where sc.CId='02') as t2
where t1.SId=t2.SId;
1
2
3
4
5
6
7
8
9
+------+------+-------+------+------+-------+
| sid | cid | score | sid | cid | score |
+------+------+-------+------+------+-------+
| 01 | 01 | 80.0 | 01 | 02 | 90.0 |
| 02 | 01 | 70.0 | 02 | 02 | 60.0 |
| 03 | 01 | 80.0 | 03 | 02 | 80.0 |
| 04 | 01 | 50.0 | 04 | 02 | 30.0 |
| 05 | 01 | 76.0 | 05 | 02 | 87.0 |
+------+------+-------+------+------+-------+

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

1
2
3
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;
1
2
3
4
5
6
7
8
9
10
+------+-------+------+-------+
| sid | score | sid | score |
+------+-------+------+-------+
| 01 | 80.0 | 01 | 90.0 |
| 02 | 70.0 | 02 | 60.0 |
| 03 | 80.0 | 03 | 80.0 |
| 04 | 50.0 | 04 | 30.0 |
| 05 | 76.0 | 05 | 87.0 |
| 06 | 31.0 | NULL | NULL |
+------+-------+------+-------+

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

1
2
3
4
select *
from sc
where sc.SId not in (select SId from sc where sc.CId='01')
and sc.CId='02';
1
2
3
4
5
+------+------+-------+
| SId | CId | score |
+------+------+-------+
| 07 | 02 | 89.0 |
+------+------+-------+

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

1
2
3
select sc.SId,AVG(sc.score) as avgscore
from sc
group by sc.SId;
1
2
3
4
5
6
7
8
9
10
11
+------+----------+
| sid | avgscore |
+------+----------+
| 01 | 89.66667 |
| 02 | 70.00000 |
| 03 | 80.00000 |
| 04 | 33.33333 |
| 05 | 81.50000 |
| 06 | 32.50000 |
| 07 | 93.50000 |
+------+----------+
1
2
3
4
5
6
select student.*,t1.avgscore
from student inner JOIN(
select sc.SId ,AVG(sc.score)as avgscore
from sc
GROUP BY sc.SId
HAVING AVG(sc.score)>=60)as t1 on student.SId=t1.SId
1
2
3
4
5
6
7
8
9
+------+--------+----------+
| sid | sname | avgscore |
+------+--------+----------+
| 01 | 赵雷 | 89.66667 |
| 02 | 钱电 | 70.00000 |
| 03 | 孙风 | 80.00000 |
| 05 | 周梅 | 81.50000 |
| 07 | 郑竹 | 93.50000 |
+------+--------+----------+

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

1
2
3
select DISTINCT student.*
from student, sc
where student.SId=sc.SId
1
2
3
4
5
6
7
8
9
10
11
+------+--------+---------------------+------+
| SId | Sname | Sage | Ssex |
+------+--------+---------------------+------+
| 01 | 赵雷 | 1990-01-01 00:00:00 | 男 |
| 02 | 钱电 | 1990-12-21 00:00:00 | 男 |
| 03 | 孙风 | 1990-05-20 00:00:00 | 男 |
| 04 | 李云 | 1990-08-06 00:00:00 | 男 |
| 05 | 周梅 | 1991-12-01 00:00:00 | 女 |
| 06 | 吴兰 | 1992-03-01 00:00:00 | 女 |
| 07 | 郑竹 | 1989-07-01 00:00:00 | 女 |
+------+--------+---------------------+------+

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

1
2
3
select sid,sum(score) as score_sum,count(cid) as course_count
from sc
group by sid;
1
2
3
4
5
6
7
8
9
10
11
+------+-----------+--------------+
| sid | score_sum | course_count |
+------+-----------+--------------+
| 01 | 269.0 | 3 |
| 02 | 210.0 | 3 |
| 03 | 240.0 | 3 |
| 04 | 100.0 | 3 |
| 05 | 163.0 | 2 |
| 06 | 65.0 | 2 |
| 07 | 187.0 | 2 |
+------+-----------+--------------+
1
2
3
4
5
select student.SId,student.Sname,t1.score_sum,t1.course_count
from student,(select SId,sum(score) as score_sum,count(CId) as course_count
from sc
GROUP BY SId) as t1
where student.SId = t1.SId
1
2
3
4
5
6
7
8
9
10
11
+------+--------+-----------+--------------+
| sid | sname | score_sum | course_count |
+------+--------+-----------+--------------+
| 01 | 赵雷 | 269.0 | 3 |
| 02 | 钱电 | 210.0 | 3 |
| 03 | 孙风 | 240.0 | 3 |
| 04 | 李云 | 100.0 | 3 |
| 05 | 周梅 | 163.0 | 2 |
| 06 | 吴兰 | 65.0 | 2 |
| 07 | 郑竹 | 187.0 | 2 |
+------+--------+-----------+--------------+

4.1 查有成绩的学生信息

这道题跟第3题一样?

1
2
3
select DISTINCT student.*
from student, sc
where student.SId=sc.SId

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

1
2
3
SELECT COUNT(*) AS T_count
FROM teacher
WHERE Tname LIKE '李%'
1
2
3
4
5
+---------+
| T_count |
+---------+
| 1 |
+---------+

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

首先teacher表查找得到「张三」老师的TId,然后根据这个TIdcourse表查询得到课程编号CId,然后在sc表查找得到选课的学生SId,最后在student表查找得到学生信息。

  • 课程编号CId查询:
1
2
3
SELECT course.CId
FROM teacher,course
WHERE teacher.Tname = '张三' AND course.TId = teacher.TId;
1
2
3
4
5
+------+
| CId |
+------+
| 02 |
+------+
  • 学生编号SId查询:
1
2
3
SELECT sc.SId
FROM teacher,course,sc
WHERE teacher.Tname = '张三' AND course.TId = teacher.TId AND sc.CId = course.CId;
1
2
3
4
5
6
7
8
9
10
+------+
| SId |
+------+
| 01 |
| 02 |
| 03 |
| 04 |
| 05 |
| 07 |
+------+
  • 全部的SQL代码:
1
2
3
SELECT student.*
FROM teacher,course,sc,student
WHERE teacher.Tname = '张三' AND course.TId = teacher.TId AND sc.CId = course.CId AND student.SId = sc.SId;
1
2
3
4
5
6
7
8
9
10
+------+--------+---------------------+------+
| SId | Sname | Sage | Ssex |
+------+--------+---------------------+------+
| 01 | 赵雷 | 1990-01-01 00:00:00 | 男 |
| 02 | 钱电 | 1990-12-21 00:00:00 | 男 |
| 03 | 孙风 | 1990-05-20 00:00:00 | 男 |
| 04 | 李云 | 1990-08-06 00:00:00 | 男 |
| 05 | 周梅 | 1991-12-01 00:00:00 | 女 |
| 07 | 郑竹 | 1989-07-01 00:00:00 | 女 |
+------+--------+---------------------+------+

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

  • 分组得到每位学生的课程数。
1
2
3
SELECT SId,COUNT(*) AS course_count
FROM sc
GROUP BY SId;
1
2
3
4
5
6
7
8
9
10
11
+------+--------------+
| SId | course_count |
+------+--------------+
| 01 | 3 |
| 02 | 3 |
| 03 | 3 |
| 04 | 3 |
| 05 | 2 |
| 06 | 2 |
| 07 | 2 |
+------+--------------+
  • 全部的SQL代码:选出课程数=总课程数的学生的信息
1
2
3
SELECT student.*
FROM student,(SELECT SId,COUNT(*) AS course_count FROM sc GROUP BY SId) AS t1
WHERE student.SId = t1.SId AND t1.course_count = (SELECT COUNT(*) FROM course);
1
2
3
4
5
6
7
8
+------+--------+---------------------+------+
| SId | Sname | Sage | Ssex |
+------+--------+---------------------+------+
| 01 | 赵雷 | 1990-01-01 00:00:00 | 男 |
| 02 | 钱电 | 1990-12-21 00:00:00 | 男 |
| 03 | 孙风 | 1990-05-20 00:00:00 | 男 |
| 04 | 李云 | 1990-08-06 00:00:00 | 男 |
+------+--------+---------------------+------+

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

首先查找得到SId = '01'的学生的课程信息CId,然后找到所有选这些课程的学生SId,最后根据SId找到学生信息。

  • 前两步:得到学生SId
1
2
3
SELECT DISTINCT SId
FROM sc
WHERE CId IN (SELECT CId FROM sc WHERE SId = '01');
1
2
3
4
5
6
7
8
9
10
11
+------+
| SId |
+------+
| 01 |
| 02 |
| 03 |
| 04 |
| 05 |
| 06 |
| 07 |
+------+
  • 全部SQL代码
1
2
3
SELECT DISTINCT student.*
FROM sc,student
WHERE CId IN (SELECT CId FROM sc WHERE SId = '01') AND sc.SId = student.SId AND sc.SId != '01';
1
2
3
4
5
6
7
8
9
10
+------+--------+---------------------+------+
| SId | Sname | Sage | Ssex |
+------+--------+---------------------+------+
| 02 | 钱电 | 1990-12-21 00:00:00 | 男 |
| 03 | 孙风 | 1990-05-20 00:00:00 | 男 |
| 04 | 李云 | 1990-08-06 00:00:00 | 男 |
| 05 | 周梅 | 1991-12-01 00:00:00 | 女 |
| 06 | 吴兰 | 1992-03-01 00:00:00 | 女 |
| 07 | 郑竹 | 1989-07-01 00:00:00 | 女 |
+------+--------+---------------------+------+

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

1
2
3
4
5
6
select DISTINCT student.*
from (
select student.SId,t.CId
from student ,(select sc.CId from sc where sc.SId='01') as t) as t1 LEFT JOIN sc on t1.SId=sc.SId and t1.CId=sc.CId,student
where sc.SId is null
and t1.SId=student.SId

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

这道题目,首先需要找到张三老师讲授的课程,然后查找得到选这门课的学生,最后也是最关键的,就是选中除这部分学生以外的学生

  • 第一步:找到张三老师讲授的课程,然后查找得到选这门课的学生
1
2
3
SELECT sc.SId
FROM sc,teacher,course
WHERE teacher.Tname = '张三' AND teacher.TId = course.TId AND course.CId = sc.CId;
1
2
3
4
5
6
7
8
9
10
+------+
| SId |
+------+
| 01 |
| 02 |
| 03 |
| 04 |
| 05 |
| 07 |
+------+
  • 全部SQL代码:
1
2
3
SELECT student.*
FROM student
WHERE SId NOT IN (SELECT sc.SId FROM sc,teacher,course WHERE teacher.Tname = '张三' AND teacher.TId = course.TId AND course.CId = sc.CId);
1
2
3
4
5
6
7
8
9
10
+------+--------+---------------------+------+
| SId | Sname | Sage | Ssex |
+------+--------+---------------------+------+
| 06 | 吴兰 | 1992-03-01 00:00:00 | 女 |
| 09 | 张三 | 2017-12-20 00:00:00 | 女 |
| 10 | 李四 | 2017-12-25 00:00:00 | 女 |
| 11 | 李四 | 2017-12-30 00:00:00 | 女 |
| 12 | 赵六 | 2017-01-01 00:00:00 | 女 |
| 13 | 孙七 | 2018-01-01 00:00:00 | 女 |
+------+--------+---------------------+------+

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

首先查找得到不及格的课程,然后按照学生SId分组统计不及格课程数目,选出其中大于等于两门的学生,得到他们的信息以及平均成绩。

  • 查找得到不及格的课程,然后按照学生SId分组统计不及格课程数目,选出其中大于等于两门的学生
1
2
3
4
5
SELECT SId,COUNT(SId) AS count_course
FROM sc
WHERE sc.score < 60
GROUP BY SId
HAVING COUNT(SId) >= 2;
1
2
3
4
5
6
+------+--------------+
| SId | count_course |
+------+--------------+
| 04 | 3 |
| 06 | 2 |
+------+--------------+
  • 由于题目中要求得到平均成绩,因此将上面的这个分组查询修改为:
1
2
3
4
5
SELECT SId,AVG(score) AS score_avg
FROM sc
WHERE score < 60
GROUP BY SId
HAVING COUNT(*) >= 2;
1
2
3
4
5
6
+------+-----------+
| SId | score_avg |
+------+-----------+
| 04 | 33.33333 |
| 06 | 32.50000 |
+------+-----------+
  • 全部的SQL代码:将上面查询出来的结果作为t1表和student表进行联结,得到这部分学生的信息
1
2
3
SELECT student.*,t1.score_avg
FROM student,(SELECT SId,AVG(score) AS score_avg FROM sc WHERE score < 60 GROUP BY SId HAVING COUNT(*) >= 2) AS t1
WHERE student.SId = t1.SId;
1
2
3
4
5
6
+------+--------+---------------------+------+-----------+
| SId | Sname | Sage | Ssex | score_avg |
+------+--------+---------------------+------+-----------+
| 04 | 李云 | 1990-08-06 00:00:00 | 男 | 33.33333 |
| 06 | 吴兰 | 1992-03-01 00:00:00 | 女 | 32.50000 |
+------+--------+---------------------+------+-----------+

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

1
2
3
4
SELECT student.*,sc.score
FROM student,sc
WHERE sc.CId = '01' AND sc.score < 60 AND sc.SId = student.SId
ORDER BY sc.score DESC;
1
2
3
4
5
6
+------+--------+---------------------+------+-------+
| SId | Sname | Sage | Ssex | score |
+------+--------+---------------------+------+-------+
| 04 | 李云 | 1990-08-06 00:00:00 | 男 | 50.0 |
| 06 | 吴兰 | 1992-03-01 00:00:00 | 女 | 31.0 |
+------+--------+---------------------+------+-------+

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

  • 首先通过按照学生SId分组得到平均成绩:
1
2
3
SELECT SId,AVG(score) AS score_avg
FROM sc
GROUP BY SId;
1
2
3
4
5
6
7
8
9
10
11
+------+-----------+
| SId | score_avg |
+------+-----------+
| 01 | 89.66667 |
| 02 | 70.00000 |
| 03 | 80.00000 |
| 04 | 33.33333 |
| 05 | 81.50000 |
| 06 | 32.50000 |
| 07 | 93.50000 |
+------+-----------+
  • 然后将上面这个查询结果作为表t1和成绩表sc联结,得到各科成绩以及平均成绩:
1
2
3
4
SELECT sc.*,t1.score_avg
FROM sc,(SELECT SId,AVG(score) AS score_avg FROM sc GROUP BY SId) AS t1
WHERE sc.SId = t1.SId
ORDER BY score_avg DESC;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
+------+------+-------+-----------+
| SId | CId | score | score_avg |
+------+------+-------+-----------+
| 07 | 02 | 89.0 | 93.50000 |
| 07 | 03 | 98.0 | 93.50000 |
| 01 | 01 | 80.0 | 89.66667 |
| 01 | 02 | 90.0 | 89.66667 |
| 01 | 03 | 99.0 | 89.66667 |
| 05 | 01 | 76.0 | 81.50000 |
| 05 | 02 | 87.0 | 81.50000 |
| 03 | 03 | 80.0 | 80.00000 |
| 03 | 01 | 80.0 | 80.00000 |
| 03 | 02 | 80.0 | 80.00000 |
| 02 | 01 | 70.0 | 70.00000 |
| 02 | 02 | 60.0 | 70.00000 |
| 02 | 03 | 80.0 | 70.00000 |
| 04 | 01 | 50.0 | 33.33333 |
| 04 | 02 | 30.0 | 33.33333 |
| 04 | 03 | 20.0 | 33.33333 |
| 06 | 01 | 31.0 | 32.50000 |
| 06 | 03 | 34.0 | 32.50000 |
+------+------+-------+-----------+

14.查询各科成绩最高分、最低分和平均分。1)以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率(及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90)。2)要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列

  • 优秀率的计算公式:
1
SUM(CASE WHEN sc.score >= 90 THEN 1 ELSE 0 END)/COUNT(sc.score)

优良率、中等率、合作率的计算类似优秀率。

  • 全部的SQL代码:
1
2
3
4
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<80 then 1 else 0 end )/COUNT(*) AS 中等率,SUM(case when sc.score>=80 and sc.score<90 and sc.score<80 then 1 else 0 end )/COUNT(*) AS 优良率,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;
1
2
3
4
5
6
7
+------+-----------+-----------+-----------+--------------+-----------+-----------+-----------+-----------+
| CId | 最高分 | 最低分 | 平均分 | 选修人数 | 及格率 | 中等率 | 优良率 | 优秀率 |
+------+-----------+-----------+-----------+--------------+-----------+-----------+-----------+-----------+
| 01 | 80.0 | 31.0 | 64.50000 | 6 | 0.6667 | 0.3333 | 0.0000 | 0.0000 |
| 02 | 90.0 | 30.0 | 72.66667 | 6 | 0.8333 | 0.0000 | 0.0000 | 0.1667 |
| 03 | 99.0 | 20.0 | 68.50000 | 6 | 0.6667 | 0.0000 | 0.0000 | 0.3333 |
+------+-----------+-----------+-----------+--------------+-----------+-----------+-----------+-----------+

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

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

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

1
2
3
4
5
SELECT t1.*,COUNT(t2.total) AS ranks
FROM (SELECT SId,SUM(score) AS total FROM sc GROUP BY SId) t1,(SELECT SId,SUM(score) AS total FROM sc GROUP BY SId) t2
WHERE t2.total >= t1.total
GROUP BY t1.SId
ORDER BY ranks;
1
2
3
4
5
6
7
8
9
10
11
+------+-------+-------+
| SId | total | ranks |
+------+-------+-------+
| 01 | 269.0 | 1 |
| 03 | 240.0 | 2 |
| 02 | 210.0 | 3 |
| 07 | 187.0 | 4 |
| 05 | 163.0 | 5 |
| 04 | 100.0 | 6 |
| 06 | 65.0 | 7 |
+------+-------+-------+

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

1
2
3
4
5
SELECT t1.*,COUNT(DISTINCT t2.total) AS ranks
FROM (SELECT SId,SUM(score) AS total FROM sc GROUP BY SId) t1,(SELECT SId,SUM(score) AS total FROM sc GROUP BY SId) t2
WHERE t2.total >= t1.total
GROUP BY t1.SId
ORDER BY ranks;
1
2
3
4
5
6
7
8
9
10
11
+------+-------+-------+
| SId | total | ranks |
+------+-------+-------+
| 01 | 269.0 | 1 |
| 03 | 240.0 | 2 |
| 02 | 210.0 | 3 |
| 07 | 187.0 | 4 |
| 05 | 163.0 | 5 |
| 04 | 100.0 | 6 |
| 06 | 65.0 | 7 |
+------+-------+-------+

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

  • [100-85]分数段人数:
1
2
SELECT COUNT(CASE WHEN sc.score>=85 THEN 1 ELSE 0 end) AS '[100,85]人数'
FROM sc;
1
2
3
4
5
+----------------+
| [100,85]人数 |
+----------------+
| 18 |
+----------------+

[85-70][70-60][60-0]分数段人数类似。

百分比的计算则再除以一个总人数。

1
2
SELECT CONCAT(SUM(CASE WHEN sc.score>=85 AND sc.score<=100 THEN 1 ELSE 0 END)/COUNT(*)*100,'%') AS '[85-100]占比'
FROM sc;
1
2
3
4
5
+----------------+
| [85-100]占比 |
+----------------+
| 27.7778% |
+----------------+
  • 全部的SQL代码:
1
2
3
4
5
6
7
8
9
SELECT course.Cname,t1.*
FROM course LEFT JOIN (SELECT sc.CId,
CONCAT(sum(case when sc.score>=85 and sc.score<=100 then 1 else 0 end )/count(*)*100,'%') as '[85-100]',
CONCAT(sum(case when sc.score>=70 and sc.score<85 then 1 else 0 end )/count(*)*100,'%') as '[70-85)',
CONCAT(sum(case when sc.score>=60 and sc.score<70 then 1 else 0 end )/count(*)*100,'%') as '[60-70)',
CONCAT(sum(case when sc.score>=0 and sc.score<60 then 1 else 0 end )/count(*)*100,'%') as '[0-60)'
FROM sc
GROUP BY sc.CId) AS t1
ON course.CId=t1.CId;
1
2
3
4
5
6
7
+--------+------+----------+----------+----------+----------+
| Cname | CId | [85-100] | [70-85) | [60-70) | [0-60) |
+--------+------+----------+----------+----------+----------+
| 语文 | 01 | 0.0000% | 66.6667% | 0.0000% | 33.3333% |
| 数学 | 02 | 50.0000% | 16.6667% | 16.6667% | 16.6667% |
| 英语 | 03 | 33.3333% | 33.3333% | 0.0000% | 33.3333% |
+--------+------+----------+----------+----------+----------+

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

思路:前三名转化为若大于此成绩的数量少于3即为前三名。

1
2
3
4
SELECT *
FROM sc t1
WHERE (SELECT COUNT(*) FROM sc t2 WHERE t2.score>t1.score AND t2.CId = t1.CId) < 3
ORDER BY t1.CId ASC,t1.score DESC;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
+------+------+-------+
| SId | CId | score |
+------+------+-------+
| 01 | 01 | 80.0 |
| 03 | 01 | 80.0 |
| 05 | 01 | 76.0 |
| 01 | 02 | 90.0 |
| 07 | 02 | 89.0 |
| 05 | 02 | 87.0 |
| 01 | 03 | 99.0 |
| 07 | 03 | 98.0 |
| 02 | 03 | 80.0 |
| 03 | 03 | 80.0 |
+------+------+-------+

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

1
2
3
SELECT CId,COUNT(*)
FROM sc
GROUP BY CId;
1
2
3
4
5
6
7
+------+----------+
| CId | COUNT(*) |
+------+----------+
| 01 | 6 |
| 02 | 6 |
| 03 | 6 |
+------+----------+

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

  • 查询得到只选修两门课程的学生学号
1
2
3
4
SELECT SId
FROM sc
GROUP BY SId
HAVING COUNT(*) = 2;
1
2
3
4
5
6
7
+------+
| SId |
+------+
| 05 |
| 06 |
| 07 |
+------+
  • 全部的SQL代码:
1
2
3
SELECT t1.SId,student.Sname
FROM student,(SELECT SId FROM sc GROUP BY SId HAVING COUNT(*) = 2) AS t1
WHERE student.SId = t1.SId;
1
2
3
4
5
6
7
+------+--------+
| SId | Sname |
+------+--------+
| 05 | 周梅 |
| 06 | 吴兰 |
| 07 | 郑竹 |
+------+--------+

21.查询男生、女生人数

1
2
3
SELECT Ssex,COUNT(*) AS nums
FROM student
GROUP BY Ssex;
1
2
3
4
5
6
+------+------+
| Ssex | nums |
+------+------+
| 男 | 4 |
| 女 | 8 |
+------+------+

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

1
2
3
SELECT *
FROM student
WHERE Sname LIKE '%风%';
1
2
3
4
5
+------+--------+---------------------+------+
| SId | Sname | Sage | Ssex |
+------+--------+---------------------+------+
| 03 | 孙风 | 1990-05-20 00:00:00 | 男 |
+------+--------+---------------------+------+

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

1
2
3
4
SELECT Sname,Ssex,COUNT(*) AS nums 
FROM Student
GROUP BY Sname,Ssex
HAVING COUNT(*) > 1;
1
2
3
4
5
+--------+------+------+
| Sname | Ssex | nums |
+--------+------+------+
| 李四 | 女 | 2 |
+--------+------+------+

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

1
2
3
SELECT *
FROM student
WHERE Year(Sage) = 1990;
1
2
3
4
5
6
7
8
+------+--------+---------------------+------+
| SId | Sname | Sage | Ssex |
+------+--------+---------------------+------+
| 01 | 赵雷 | 1990-01-01 00:00:00 | 男 |
| 02 | 钱电 | 1990-12-21 00:00:00 | 男 |
| 03 | 孙风 | 1990-05-20 00:00:00 | 男 |
| 04 | 李云 | 1990-08-06 00:00:00 | 男 |
+------+--------+---------------------+------+

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

1
2
3
4
SELECT CId,AVG(score) AS score_avg
FROM sc
GROUP BY CId
ORDER BY AVG(score) DESC,CId ASC;
1
2
3
4
5
6
7
+------+-----------+
| CId | score_avg |
+------+-----------+
| 02 | 72.66667 |
| 03 | 68.50000 |
| 01 | 64.50000 |
+------+-----------+

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

此题跟第2题是类似的!

1
2
3
SELECT student.SId,student.Sname,t1.score_avg
FROM student,(SELECT SId,AVG(score) AS score_avg FROM sc GROUP BY SId HAVING AVG(score) >= 85) AS t1
WHERE student.SId = t1.SId;
1
2
3
4
5
6
+------+--------+-----------+
| SId | Sname | score_avg |
+------+--------+-----------+
| 01 | 赵雷 | 89.66667 |
| 07 | 郑竹 | 93.50000 |
+------+--------+-----------+

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

1
2
3
SELECT student.Sname,sc.score
FROM course,sc,student
WHERE course.Cname = '数学' AND course.CId = sc.CId AND sc.score < 60 AND sc.SId = student.SId;
1
2
3
4
5
+--------+-------+
| Sname | score |
+--------+-------+
| 李云 | 30.0 |
+--------+-------+

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

  • 由于这里需要得到所有学生所有课程的情况,所以不添加联结条件得到笛卡儿积
1
2
SELECT DISTINCT student.SId,course.CID
FROM student JOIN course;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
+------+------+
| SId | CID |
+------+------+
| 01 | 01 |
| 01 | 02 |
| 01 | 03 |
| 02 | 01 |
| 02 | 02 |
| 02 | 03 |
| 03 | 01 |
| 03 | 02 |
| 03 | 03 |
| 04 | 01 |
| 04 | 02 |
| 04 | 03 |
| 05 | 01 |
| 05 | 02 |
| 05 | 03 |
| 06 | 01 |
| 06 | 02 |
| 06 | 03 |
| 07 | 01 |
| 07 | 02 |
| 07 | 03 |
| 09 | 01 |
| 09 | 02 |
| 09 | 03 |
| 10 | 01 |
| 10 | 02 |
| 10 | 03 |
| 11 | 01 |
| 11 | 02 |
| 11 | 03 |
| 12 | 01 |
| 12 | 02 |
| 12 | 03 |
| 13 | 01 |
| 13 | 02 |
| 13 | 03 |
+------+------+
  • 全部SQL代码:这里将前面得到的笛卡尔积与sc表左联结得到所有学生所有课程的成绩,然后进行正确的排序。
1
2
3
4
SELECT t1.SId,t1.CId,sc.score
FROM (SELECT DISTINCT student.SId,course.CId FROM student JOIN course) AS t1 LEFT JOIN sc
ON t1.SId = sc.SId AND t1.CId = sc.CId
ORDER BY t1.SId ASC,t1.CId ASC;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
+------+------+-------+
| SId | CId | score |
+------+------+-------+
| 01 | 01 | 80.0 |
| 01 | 02 | 90.0 |
| 01 | 03 | 99.0 |
| 02 | 01 | 70.0 |
| 02 | 02 | 60.0 |
| 02 | 03 | 80.0 |
| 03 | 01 | 80.0 |
| 03 | 02 | 80.0 |
| 03 | 03 | 80.0 |
| 04 | 01 | 50.0 |
| 04 | 02 | 30.0 |
| 04 | 03 | 20.0 |
| 05 | 01 | 76.0 |
| 05 | 02 | 87.0 |
| 05 | 03 | NULL |
| 06 | 01 | 31.0 |
| 06 | 02 | NULL |
| 06 | 03 | 34.0 |
| 07 | 01 | NULL |
| 07 | 02 | 89.0 |
| 07 | 03 | 98.0 |
| 09 | 01 | NULL |
| 09 | 02 | NULL |
| 09 | 03 | NULL |
| 10 | 01 | NULL |
| 10 | 02 | NULL |
| 10 | 03 | NULL |
| 11 | 01 | NULL |
| 11 | 02 | NULL |
| 11 | 03 | NULL |
| 12 | 01 | NULL |
| 12 | 02 | NULL |
| 12 | 03 | NULL |
| 13 | 01 | NULL |
| 13 | 02 | NULL |
| 13 | 03 | NULL |
+------+------+-------+

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

这里需要注意,需要查询的是学生姓名(student表中)、课程名称(course表中),因此需要联结三个表。

1
2
3
SELECT student.Sname,course.CId,sc.score
FROM student,sc,course
WHERE student.SId = sc.SId AND sc.CId = course.CId AND sc.score >= 70;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
+--------+------+-------+
| Sname | CId | score |
+--------+------+-------+
| 赵雷 | 01 | 80.0 |
| 赵雷 | 02 | 90.0 |
| 赵雷 | 03 | 99.0 |
| 钱电 | 01 | 70.0 |
| 钱电 | 03 | 80.0 |
| 孙风 | 01 | 80.0 |
| 孙风 | 02 | 80.0 |
| 孙风 | 03 | 80.0 |
| 周梅 | 01 | 76.0 |
| 周梅 | 02 | 87.0 |
| 郑竹 | 02 | 89.0 |
| 郑竹 | 03 | 98.0 |
+--------+------+-------+

30.查询存在不及格的课程

1
2
3
SELECT *
FROM sc
WHERE sc.score < 60;
1
2
3
4
5
6
7
8
9
+------+------+-------+
| SId | CId | score |
+------+------+-------+
| 04 | 01 | 50.0 |
| 04 | 02 | 30.0 |
| 04 | 03 | 20.0 |
| 06 | 01 | 31.0 |
| 06 | 03 | 34.0 |
+------+------+-------+

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

这个题目跟12题是类似的。

1
2
3
SELECT student.SId,student.Sname
FROM student,sc
WHERE student.SId = sc.SId AND sc.CId = '01' AND sc.score >= 80;
1
2
3
4
5
6
+------+--------+
| SId | Sname |
+------+--------+
| 01 | 赵雷 |
| 03 | 孙风 |
+------+--------+

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

1
2
3
SELECT CId,COUNT(*) AS course_num
FROM sc
GROUP BY CId;
1
2
3
4
5
6
7
+------+------------+
| CId | course_num |
+------+------------+
| 01 | 6 |
| 02 | 6 |
| 03 | 6 |
+------+------------+

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

1)如果有多门课程,然后每门课程选出成绩最高的学生(成绩重复)的话,是如下的解法:

  • 按照课程CId分组得到每门课程的最高分
1
2
3
SELECT CId,MAX(score) AS max_score
FROM sc
GROUP BY CId;
1
2
3
4
5
6
7
+------+-----------+
| CId | max_score |
+------+-----------+
| 01 | 80.0 |
| 02 | 90.0 |
| 03 | 99.0 |
+------+-----------+
  • 全部SQL代码:查询得到「张三」老师教的课程和前面得到的最高分表t1联结(course.CId = t1.CId AND t1.max_score = sc.score),最后得到学生信息
1
2
3
SELECT student.*,t1.CId,t1.max_score
FROM teacher,course,student,sc,(SELECT CId,MAX(score) AS max_score FROM sc GROUP BY CId) AS t1
WHERE teacher.Tname = '张三' AND teacher.TId = course.TId AND course.CId = t1.CId AND t1.max_score = sc.score AND student.SId = sc.SId;
1
2
3
4
5
+------+--------+---------------------+------+------+-----------+
| SId | Sname | Sage | Ssex | CId | max_score |
+------+--------+---------------------+------+------+-----------+
| 01 | 赵雷 | 1990-01-01 00:00:00 | 男 | 02 | 90.0 |
+------+--------+---------------------+------+------+-----------+

2)如果有多门课程,然后只选出成绩最高的学生的话(成绩不重复),是如下的解法:

1
2
3
4
5
SELECT student.*,sc.CId,sc.score
FROM teacher,course,sc,student
WHERE teacher.Tname = '张三' AND teacher.TId = course.TId AND course.CId = sc.CId AND sc.SId = student.SId
ORDER BY score DESC
LIMIT 1;
1
2
3
4
5
+------+--------+---------------------+------+------+-------+
| SId | Sname | Sage | Ssex | CId | score |
+------+--------+---------------------+------+------+-------+
| 01 | 赵雷 | 1990-01-01 00:00:00 | 男 | 02 | 90.0 |
+------+--------+---------------------+------+------+-------+

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

这道题的思路继续上一题(2),我们已经查询到了符合限定条件的最高分了,这个时候只用比较这张表,找到全部score等于这个最高分的记录就可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 = "张三"
);
1
2
3
4
5
+------+--------+---------------------+------+-------+------+
| SId | Sname | Sage | Ssex | score | cid |
+------+--------+---------------------+------+-------+------+
| 01 | 赵雷 | 1990-01-01 00:00:00 | 男 | 90.0 | 02 |
+------+--------+---------------------+------+-------+------+

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

  • 按照学生学号SId、成绩score进行分组计数:
1
2
3
SELECT SId,score,COUNT(*)
FROM sc
GROUP BY SId,score;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
+------+-------+----------+
| SId | score | COUNT(*) |
+------+-------+----------+
| 01 | 80.0 | 1 |
| 01 | 90.0 | 1 |
| 01 | 99.0 | 1 |
| 02 | 70.0 | 1 |
| 02 | 60.0 | 1 |
| 02 | 80.0 | 1 |
| 03 | 80.0 | 3 |
| 04 | 50.0 | 1 |
| 04 | 30.0 | 1 |
| 04 | 20.0 | 1 |
| 05 | 76.0 | 1 |
| 05 | 87.0 | 1 |
| 06 | 31.0 | 1 |
| 06 | 34.0 | 1 |
| 07 | 89.0 | 1 |
| 07 | 98.0 | 1 |
+------+-------+----------+
  • 全部的SQL代码:从上面分组结果中选出计数值大于2的学生学号SId,学生成绩score,然后和sc表联结,过滤出想要的信息。
1
2
3
SELECT sc.SId,sc.CId,sc.score
FROM sc,(SELECT SId,score FROM sc GROUP BY SId,score HAVING COUNT(*) > 1) AS t1
WHERE sc.SId = t1.SId AND sc.score = t1.score;
1
2
3
4
5
6
7
+------+------+-------+
| SId | CId | score |
+------+------+-------+
| 03 | 01 | 80.0 |
| 03 | 02 | 80.0 |
| 03 | 03 | 80.0 |
+------+------+-------+

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

这题和18题类似。

思路:前两名转化为若大于此成绩的数量少于2即为前两名。

1
2
3
4
SELECT *
FROM sc t1
WHERE (SELECT COUNT(*) FROM sc t2 WHERE t1.score < t2.score AND t1.CId = t2.CId) < 2
ORDER BY t1.CId ASC,t1.score DESC;
1
2
3
4
5
6
7
8
9
10
+------+------+-------+
| SId | CId | score |
+------+------+-------+
| 01 | 01 | 80.0 |
| 03 | 01 | 80.0 |
| 01 | 02 | 90.0 |
| 07 | 02 | 89.0 |
| 01 | 03 | 99.0 |
| 07 | 03 | 98.0 |
+------+------+-------+

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

1
2
3
4
SELECT CId,COUNT(*) AS stu_num
FROM sc
GROUP BY CId
HAVING COUNT(*) > 5;
1
2
3
4
5
6
7
+------+------------+
| CId | stu_num |
+------+------------+
| 01 | 6 |
| 02 | 6 |
| 03 | 6 |
+------+------------+

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

1
2
3
4
SELECT SId,COUNT(*) AS course_num
FROM sc
GROUP BY SId
HAVING COUNT(*) >= 2;
1
2
3
4
5
6
7
8
9
10
11
+------+------------+
| SId | course_num |
+------+------------+
| 01 | 3 |
| 02 | 3 |
| 03 | 3 |
| 04 | 3 |
| 05 | 2 |
| 06 | 2 |
| 07 | 2 |
+------+------------+

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

  • 查询得到选课数=全部课程数的学生学号SId
1
2
3
4
SELECT SId 
FROM sc
GROUP BY SId
HAVING COUNT(*) = (SELECT COUNT(*) FROM course);
1
2
3
4
5
6
7
8
+------+
| SId |
+------+
| 01 |
| 02 |
| 03 |
| 04 |
+------+
  • 全部的SQL代码:
1
2
3
SELECT student.*
FROM student,(SELECT SId FROM sc GROUP BY SId HAVING COUNT(*) = (SELECT COUNT(*) FROM course)) AS t1
WHERE student.SId = t1.SId;
1
2
3
4
5
6
7
8
+------+--------+---------------------+------+
| SId | Sname | Sage | Ssex |
+------+--------+---------------------+------+
| 01 | 赵雷 | 1990-01-01 00:00:00 | 男 |
| 02 | 钱电 | 1990-12-21 00:00:00 | 男 |
| 03 | 孙风 | 1990-05-20 00:00:00 | 男 |
| 04 | 李云 | 1990-08-06 00:00:00 | 男 |
+------+--------+---------------------+------+

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

1
2
SELECT SId,Sname,(YEAR(CURDATE()) - YEAR(Sage)) AS age
FROM student;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
+------+--------+------+
| SId | Sname | age |
+------+--------+------+
| 01 | 赵雷 | 29 |
| 02 | 钱电 | 29 |
| 03 | 孙风 | 29 |
| 04 | 李云 | 29 |
| 05 | 周梅 | 28 |
| 06 | 吴兰 | 27 |
| 07 | 郑竹 | 30 |
| 09 | 张三 | 2 |
| 10 | 李四 | 2 |
| 11 | 李四 | 2 |
| 12 | 赵六 | 2 |
| 13 | 孙七 | 1 |
+------+--------+------+

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

1
2
SELECT SId,Sname,TIMESTAMPDIFF(YEAR,Sage,CURDATE()) AS age
FROM student;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
+------+--------+------+
| SId | Sname | age |
+------+--------+------+
| 01 | 赵雷 | 29 |
| 02 | 钱电 | 28 |
| 03 | 孙风 | 28 |
| 04 | 李云 | 28 |
| 05 | 周梅 | 27 |
| 06 | 吴兰 | 27 |
| 07 | 郑竹 | 29 |
| 09 | 张三 | 1 |
| 10 | 李四 | 1 |
| 11 | 李四 | 1 |
| 12 | 赵六 | 2 |
| 13 | 孙七 | 1 |
+------+--------+------+

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

  • WEEK函数使用说明:
1
2
SELECT Sage,WEEK(Sage,1) 
FROM student;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
+---------------------+--------------+
| Sage | WEEK(Sage,1) |
+---------------------+--------------+
| 1990-01-01 00:00:00 | 1 |
| 1990-12-21 00:00:00 | 51 |
| 1990-05-20 00:00:00 | 20 |
| 1990-08-06 00:00:00 | 32 |
| 1991-12-01 00:00:00 | 48 |
| 1992-03-01 00:00:00 | 9 |
| 1989-07-01 00:00:00 | 26 |
| 2017-12-20 00:00:00 | 51 |
| 2017-12-25 00:00:00 | 52 |
| 2017-12-30 00:00:00 | 52 |
| 2017-01-01 00:00:00 | 0 |
| 2018-01-01 00:00:00 | 1 |
+---------------------+--------------+
1
2
3
SELECT *
FROM student
WHERE WEEK(Sage,1) = WEEK(CURDATE(),1);
1
Empty set (0.03 sec)

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

1
2
3
SELECT *
FROM student
WHERE WEEK(Sage,1) = WEEK(CURDATE(),1) + 1;
1
Empty set (0.03 sec)

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

1
2
3
SELECT *
FROM student
WHERE MONTH(Sage) = MONTH(CURDATE());
1
2
3
4
5
+------+--------+---------------------+------+
| SId | Sname | Sage | Ssex |
+------+--------+---------------------+------+
| 06 | 吴兰 | 1992-03-01 00:00:00 | 女 |
+------+--------+---------------------+------+

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

1
2
3
SELECT *
FROM student
WHERE MONTH(Sage) = MONTH(CURDATE()) + 1;
1
Empty set (0.00 sec)

----------- 本文结束啦感谢您阅读 -----------

赞赏一杯咖啡

欢迎关注我的其它发布渠道