0


MySQL 经典练习 50 题(完美解答版)

一、创建数据库和表

  1. **数据库**
  2. 学生表 student
  3. 课程表 course
  4. 教师表 teacher
  5. 成绩表 score
  6. **表关系**
创建数据库和表
创建数据库
  1. drop database if exists mysql_test cascade;
  2. create database mysql_test;
  3. use mysql_test;
创建学生表 student
  1. create table student (
  2. s_id int,
  3. s_name varchar(8),
  4. s_birth date,
  5. s_sex varchar(4)
  6. );
插入学生数据
  1. insert into student values
  2. (1,'赵雷','1990-01-01','男'),
  3. (2,'钱电','1990-12-21','男'),
  4. (3,'孙风','1990-05-20','男'),
  5. (4,'李云','1990-08-06','男'),
  6. (5,'周梅','1991-12-01','女'),
  7. (6,'吴兰','1992-03-01','女'),
  8. (7,'郑竹','1989-07-01','女'),
  9. (8,'王菊','1990-01-20','女');
创建课程表 course
  1. create table course (
  2. c_id int,
  3. c_name varchar(8),
  4. t_id int
  5. );
插入课程数据
  1. insert into course values
  2. (1,'语文',2),
  3. (2,'数学',1),
  4. (3,'英语',3);
创建教师表 teacher
  1. create table teacher (
  2. t_id int,
  3. t_name varchar(8)
  4. );
插入教师数据
  1. insert into teacher values
  2. (1,'张三'),
  3. (2,'李四'),
  4. (3,'王五');
创建成绩表 score
  1. create table score (
  2. s_id int,
  3. c_id int,
  4. s_score int
  5. );
插入成绩数据
  1. insert into score values
  2. (1,1,80),
  3. (1,2,90),
  4. (1,3,99),
  5. (2,1,70),
  6. (2,2,60),
  7. (2,3,65),
  8. (3,1,80),
  9. (3,2,80),
  10. (3,3,80),
  11. (4,1,50),
  12. (4,2,30),
  13. (4,3,40),
  14. (5,1,76),
  15. (5,2,87),
  16. (6,1,31),
  17. (6,3,34),
  18. (7,2,89),
  19. (7,3,98);

二、经典练习 50 题

1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
  1. select s.*,sc1.s_score as score_01,sc2.s_score as score_02
  2. from student s
  3. inner join (
  4. select * from score where c_id = 1
  5. ) sc1
  6. on s.s_id = sc1.s_id
  7. inner join (
  8. select * from score where c_id = 2
  9. ) sc2
  10. on s.s_id = sc2.s_id
  11. where sc1.s_score > sc2.s_score;

+------+--------+------------+-------+----------+----------+
| s_id | s_name | s_birth | s_sex | score_01 | score_02 |
+------+--------+------------+-------+----------+----------+
| 2 | 钱电 | 1990-12-21 | 男 | 70 | 60 |
| 4 | 李云 | 1990-08-06 | 男 | 50 | 30 |
+------+--------+------------+-------+----------+----------+

2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
  1. select s.*,sc1.s_score as score_01,sc2.s_score as score_02
  2. from student s
  3. inner join (
  4. select * from score where c_id = 1
  5. ) sc1
  6. on s.s_id = sc1.s_id
  7. inner join (
  8. select * from score where c_id = 2
  9. ) sc2
  10. on s.s_id = sc2.s_id
  11. where sc1.s_score < sc2.s_score;

+------+--------+------------+-------+----------+----------+
| s_id | s_name | s_birth | s_sex | score_01 | score_02 |
+------+--------+------------+-------+----------+----------+
| 1 | 赵雷 | 1990-01-01 | 男 | 80 | 90 |
| 5 | 周梅 | 1991-12-01 | 女 | 76 | 87 |
+------+--------+------------+-------+----------+----------+

3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
  1. select s.s_id,s.s_name,round(avg_score, 2) as avg_score
  2. from student s
  3. inner join (
  4. select s_id,avg(s_score) as avg_score
  5. from score
  6. group by s_id
  7. having avg_score >= 60
  8. ) t1
  9. on s.s_id = t1.s_id;

+------+--------+-----------+
| s_id | s_name | avg_score |
+------+--------+-----------+
| 1 | 赵雷 | 89.67 |
| 2 | 钱电 | 65.00 |
| 3 | 孙风 | 80.00 |
| 5 | 周梅 | 81.50 |
| 7 | 郑竹 | 93.50 |
+------+--------+-----------+

4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩(包括有成绩的和无成绩的)

  1. select s.s_id,s.s_name,ifnull(round(avg_score, 2), 0) as avg_score
  2. from student s
  3. left join (
  4. select s_id,avg(s_score) as avg_score
  5. from score
  6. group by s_id
  7. ) t1
  8. on s.s_id = t1.s_id
  9. where avg_score is null or avg_score < 60;

+------+--------+-----------+
| s_id | s_name | avg_score |
+------+--------+-----------+
| 4 | 李云 | 40.00 |
| 6 | 吴兰 | 32.50 |
| 8 | 王菊 | 0.00 |
+------+--------+-----------+

5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
  1. select s.s_id,s.s_name,ifnull(cnt_course, 0) as cnt_course,ifnull(sum_score, 0) as sum_score
  2. from student s
  3. left join (
  4. select s_id,count(*) as cnt_course,sum(s_score) as sum_score
  5. from score
  6. group by s_id
  7. ) t1
  8. on s.s_id = t1.s_id;

+------+--------+------------+-----------+
| s_id | s_name | cnt_course | sum_score |
+------+--------+------------+-----------+
| 1 | 赵雷 | 3 | 269 |
| 2 | 钱电 | 3 | 195 |
| 3 | 孙风 | 3 | 240 |
| 4 | 李云 | 3 | 120 |
| 5 | 周梅 | 2 | 163 |
| 6 | 吴兰 | 2 | 65 |
| 7 | 郑竹 | 2 | 187 |
| 8 | 王菊 | 0 | 0 |
+------+--------+------------+-----------+

6、查询"李"姓老师的数量
  1. select count(*) as cnt_name_li
  2. from teacher
  3. where t_name like '李%';

+-------------+
| cnt_name_li |
+-------------+
| 1 |
+-------------+

7、询学过"张三"老师授课的同学的信息
  1. select s.*
  2. from student s
  3. where s_id in (
  4. select distinct s_id
  5. from score sc
  6. inner join (
  7. select c_id
  8. from course c
  9. inner join teacher t on c.t_id = t.t_id
  10. where t_name = '张三'
  11. ) t1
  12. on sc.c_id = t1.c_id
  13. );

+------+--------+------------+-------+
| s_id | s_name | s_birth | s_sex |
+------+--------+------------+-------+
| 1 | 赵雷 | 1990-01-01 | 男 |
| 2 | 钱电 | 1990-12-21 | 男 |
| 3 | 孙风 | 1990-05-20 | 男 |
| 4 | 李云 | 1990-08-06 | 男 |
| 5 | 周梅 | 1991-12-01 | 女 |
| 7 | 郑竹 | 1989-07-01 | 女 |
+------+--------+------------+-------+

8、查询没学过"张三"老师授课的同学的信息
  1. select s.*
  2. from student s
  3. where s_id not in (
  4. select distinct s_id
  5. from score sc
  6. inner join (
  7. select c_id
  8. from course c
  9. inner join teacher t on c.t_id = t.t_id
  10. where t_name = '张三'
  11. ) t1
  12. on sc.c_id = t1.c_id
  13. );

+------+--------+------------+-------+
| s_id | s_name | s_birth | s_sex |
+------+--------+------------+-------+
| 6 | 吴兰 | 1992-03-01 | 女 |
| 8 | 王菊 | 1990-01-20 | 女 |
+------+--------+------------+-------+

9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
  1. select s.*
  2. from student s
  3. where s_id in (
  4. select s_id
  5. from score
  6. where c_id in (1,2)
  7. group by s_id
  8. having count(*) = 2
  9. );

+------+--------+------------+-------+
| s_id | s_name | s_birth | s_sex |
+------+--------+------------+-------+
| 1 | 赵雷 | 1990-01-01 | 男 |
| 2 | 钱电 | 1990-12-21 | 男 |
| 3 | 孙风 | 1990-05-20 | 男 |
| 4 | 李云 | 1990-08-06 | 男 |
| 5 | 周梅 | 1991-12-01 | 女 |
+------+--------+------------+-------+

10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
  1. select s.*
  2. from student s
  3. where s_id in (
  4. select s_id
  5. from score
  6. where c_id = 1
  7. and s_id not in (
  8. select s_id from score where c_id = 2
  9. )
  10. );

+------+--------+------------+-------+
| s_id | s_name | s_birth | s_sex |
+------+--------+------------+-------+
| 6 | 吴兰 | 1992-03-01 | 女 |
+------+--------+------------+-------+

11、查询没有学全所有课程的同学的信息
  1. select *
  2. from student
  3. where s_id not in (
  4. select s_id
  5. from score
  6. group by s_id
  7. having count(*) = (
  8. select count(*) from course
  9. )
  10. );

+------+--------+------------+-------+
| s_id | s_name | s_birth | s_sex |
+------+--------+------------+-------+
| 5 | 周梅 | 1991-12-01 | 女 |
| 6 | 吴兰 | 1992-03-01 | 女 |
| 7 | 郑竹 | 1989-07-01 | 女 |
| 8 | 王菊 | 1990-01-20 | 女 |
+------+--------+------------+-------+

12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
  1. select *
  2. from student
  3. where s_id in (
  4. select distinct s_id
  5. from score s
  6. inner join (
  7. select c_id from score where s_id = 1
  8. ) t1
  9. on s.c_id = t1.c_id
  10. );

+------+--------+------------+-------+
| s_id | s_name | s_birth | s_sex |
+------+--------+------------+-------+
| 1 | 赵雷 | 1990-01-01 | 男 |
| 2 | 钱电 | 1990-12-21 | 男 |
| 3 | 孙风 | 1990-05-20 | 男 |
| 4 | 李云 | 1990-08-06 | 男 |
| 5 | 周梅 | 1991-12-01 | 女 |
| 6 | 吴兰 | 1992-03-01 | 女 |
| 7 | 郑竹 | 1989-07-01 | 女 |
+------+--------+------------+-------+

13、查询和"01"号的同学学习的课程完全相同的其他同学的信息
  1. select *
  2. from student
  3. where s_id in (
  4. select s_id
  5. from score s
  6. inner join (
  7. select c_id from score where s_id = 1
  8. ) t1
  9. on s.c_id = t1.c_id
  10. where s_id != 1
  11. group by s_id having count(*) = (
  12. select count(*) from score where s_id = 1
  13. )
  14. );

+------+--------+------------+-------+
| s_id | s_name | s_birth | s_sex |
+------+--------+------------+-------+
| 2 | 钱电 | 1990-12-21 | 男 |
| 3 | 孙风 | 1990-05-20 | 男 |
| 4 | 李云 | 1990-08-06 | 男 |
+------+--------+------------+-------+

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

  1. select s_name
  2. from student
  3. where s_id not in (
  4. select s_id
  5. from score sc
  6. inner join (
  7. select c_id
  8. from course c
  9. inner join teacher t on c.t_id = t.t_id
  10. where t_name = '张三'
  11. ) t1
  12. on sc.c_id = t1.c_id
  13. );

+--------+
| s_name |
+--------+
| 吴兰 |
| 王菊 |
+--------+

15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
  1. select s.s_id,s.s_name,round(avg(s_score), 2) as avg_score
  2. from student s
  3. inner join (
  4. select s_id
  5. from score
  6. where s_score < 60
  7. group by s_id
  8. having count(*) >= 2
  9. ) t1
  10. on s.s_id = t1.s_id
  11. inner join score sc on s.s_id = sc.s_id
  12. group by s.s_id;

+------+--------+-----------+
| s_id | s_name | avg_score |
+------+--------+-----------+
| 4 | 李云 | 40.00 |
| 6 | 吴兰 | 32.50 |
+------+--------+-----------+

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

  1. select s.*,s_score as score_01
  2. from student s
  3. inner join (
  4. select s_id,s_score
  5. from score
  6. where c_id = 1
  7. and s_score < 60
  8. ) t1
  9. on s.s_id = t1.s_id
  10. order by score_01 desc;

+------+--------+------------+-------+----------+
| s_id | s_name | s_birth | s_sex | score_01 |
+------+--------+------------+-------+----------+
| 4 | 李云 | 1990-08-06 | 男 | 50 |
| 6 | 吴兰 | 1992-03-01 | 女 | 31 |
+------+--------+------------+-------+----------+

17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
  1. select
  2. s.s_id as 学号,s.s_name as 姓名,
  3. sum(case c_id when 1 then s_score else 0 end) as 语文,
  4. sum(case c_id when 2 then s_score else 0 end) as 数学,
  5. sum(case c_id when 3 then s_score else 0 end) as 英语,
  6. ifnull(round(avg(s_score), 2), 0) as 平均成绩
  7. from student s
  8. left join score sc on s.s_id = sc.s_id
  9. group by s.s_id
  10. order by 平均成绩 desc;

+------+------+------+------+------+----------+
| 学号 | 姓名 | 语文 | 数学 | 英语 | 平均成绩 |
+------+------+------+------+------+----------+
| 7 | 郑竹 | 0 | 89 | 98 | 93.50 |
| 1 | 赵雷 | 80 | 90 | 99 | 89.67 |
| 5 | 周梅 | 76 | 87 | 0 | 81.50 |
| 3 | 孙风 | 80 | 80 | 80 | 80.00 |
| 2 | 钱电 | 70 | 60 | 65 | 65.00 |
| 4 | 李云 | 50 | 30 | 40 | 40.00 |
| 6 | 吴兰 | 31 | 0 | 34 | 32.50 |
| 8 | 王菊 | 0 | 0 | 0 | 0.00 |
+------+------+------+------+------+----------+

18、查询各科成绩最高分、最低分和平均分,以如下形式显示:

课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
– 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90

  1. select
  2. c.c_id as 课程ID,c.c_name as 课程name,
  3. max(s_score) as 最高分,
  4. min(s_score) as 最低分,
  5. round(avg(s_score), 2) as 平均分,
  6. concat(round(sum(case when s_score >= 60 then 1 else 0 end) / count(*) * 100, 2), '%') as 及格率,
  7. concat(round(sum(case when s_score between 70 and 80 then 1 else 0 end) / count(*) * 100, 2), '%') as 中等率,
  8. concat(round(sum(case when s_score between 80 and 90 then 1 else 0 end) / count(*) * 100, 2), '%') as 优良率,
  9. concat(round(sum(case when s_score >= 90 then 1 else 0 end) / count(*) * 100, 2), '%') as 优秀率
  10. from course c
  11. inner join score s on c.c_id = s.c_id
  12. group by c.c_id;

+--------+----------+--------+--------+--------+--------+--------+--------+--------+
| 课程ID | 课程name | 最高分 | 最低分 | 平均分 | 及格率 | 中等率 | 优良率 | 优秀率 |
+--------+----------+--------+--------+--------+--------+--------+--------+--------+
| 1 | 语文 | 80 | 31 | 64.50 | 66.67% | 66.67% | 33.33% | 0.00% |
| 2 | 数学 | 90 | 30 | 72.67 | 83.33% | 16.67% | 66.67% | 16.67% |
| 3 | 英语 | 99 | 34 | 69.33 | 66.67% | 16.67% | 16.67% | 33.33% |
+--------+----------+--------+--------+--------+--------+--------+--------+--------+

19、按各科成绩进行排序,并显示排名
  1. select
  2. (@i := case when @pre_group_id = c_id then @i + 1 else 1 end) as rank,
  3. (@pre_group_id := c_id) as c_id,
  4. c_name,s_id,s_name,s_score
  5. from (select @i := 0, @pre_group_id := 1) var
  6. cross join (
  7. select c.c_id,c.c_name,s.s_id,s.s_name,s_score
  8. from score sc
  9. inner join student s on sc.s_id = s.s_id
  10. inner join course c on sc.c_id = c.c_id
  11. group by c.c_id,s.s_id
  12. order by c.c_id,s_score desc
  13. ) t1;

+------+------+--------+------+--------+---------+
| rank | c_id | c_name | s_id | s_name | s_score |
+------+------+--------+------+--------+---------+
| 1 | 1 | 语文 | 1 | 赵雷 | 80 |
| 2 | 1 | 语文 | 3 | 孙风 | 80 |
| 3 | 1 | 语文 | 5 | 周梅 | 76 |
| 4 | 1 | 语文 | 2 | 钱电 | 70 |
| 5 | 1 | 语文 | 4 | 李云 | 50 |
| 6 | 1 | 语文 | 6 | 吴兰 | 31 |
| 1 | 2 | 数学 | 1 | 赵雷 | 90 |
| 2 | 2 | 数学 | 7 | 郑竹 | 89 |
| 3 | 2 | 数学 | 5 | 周梅 | 87 |
| 4 | 2 | 数学 | 3 | 孙风 | 80 |
| 5 | 2 | 数学 | 2 | 钱电 | 60 |
| 6 | 2 | 数学 | 4 | 李云 | 30 |
| 1 | 3 | 英语 | 1 | 赵雷 | 99 |
| 2 | 3 | 英语 | 7 | 郑竹 | 98 |
| 3 | 3 | 英语 | 3 | 孙风 | 80 |
| 4 | 3 | 英语 | 2 | 钱电 | 65 |
| 5 | 3 | 英语 | 4 | 李云 | 40 |
| 6 | 3 | 英语 | 6 | 吴兰 | 34 |
+------+------+--------+------+--------+---------+

20、查询学生的总成绩并进行排名
  1. select (@i := @i + 1) as rank,t2.*
  2. from (select @i := 0) var
  3. cross join (
  4. select s.s_id,s.s_name,sum_score
  5. from student s
  6. inner join (
  7. select s_id,sum(s_score) as sum_score
  8. from score
  9. group by s_id
  10. ) t1
  11. on s.s_id = t1.s_id
  12. order by sum_score desc
  13. ) t2;

+------+------+--------+-----------+
| rank | s_id | s_name | sum_score |
+------+------+--------+-----------+
| 1 | 1 | 赵雷 | 269 |
| 2 | 3 | 孙风 | 240 |
| 3 | 2 | 钱电 | 195 |
| 4 | 7 | 郑竹 | 187 |
| 5 | 5 | 周梅 | 163 |
| 6 | 4 | 李云 | 120 |
| 7 | 6 | 吴兰 | 65 |
+------+------+--------+-----------+

21、查询不同老师所教不同课程平均分从高到低显示
  1. select t.t_id,t.t_name,c.c_id,c.c_name,round(avg(s_score), 2) as avg_score
  2. from score sc
  3. inner join course c on sc.c_id = c.c_id
  4. inner join teacher t on t.t_id = c.t_id
  5. group by t.t_id,c.c_id
  6. order by t.t_id,avg_score desc;

+------+--------+------+--------+-----------+
| t_id | t_name | c_id | c_name | avg_score |
+------+--------+------+--------+-----------+
| 1 | 张三 | 2 | 数学 | 72.67 |
| 2 | 李四 | 1 | 语文 | 64.50 |
| 3 | 王五 | 3 | 英语 | 69.33 |
+------+--------+------+--------+-----------+

22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
  1. select *
  2. from (
  3. select
  4. (@i := case when @pre_group_id = c_id then @i + 1 else 1 end) as rank,
  5. (@pre_group_id := c_id) as c_id,
  6. c_name,s_id,s_name,s_birth,s_sex,s_score
  7. from (select @i := 0, @pre_group_id := 1) var
  8. cross join (
  9. select c.c_id,c.c_name,s.*,s_score
  10. from score sc
  11. inner join student s on sc.s_id = s.s_id
  12. inner join course c on sc.c_id = c.c_id
  13. group by c.c_id,s.s_id
  14. order by c.c_id,s_score desc
  15. ) t1
  16. ) t2
  17. where rank in (2,3);

+------+------+--------+------+--------+------------+-------+---------+
| rank | c_id | c_name | s_id | s_name | s_birth | s_sex | s_score |
+------+------+--------+------+--------+------------+-------+---------+
| 2 | 1 | 语文 | 3 | 孙风 | 1990-05-20 | 男 | 80 |
| 3 | 1 | 语文 | 5 | 周梅 | 1991-12-01 | 女 | 76 |
| 2 | 2 | 数学 | 7 | 郑竹 | 1989-07-01 | 女 | 89 |
| 3 | 2 | 数学 | 5 | 周梅 | 1991-12-01 | 女 | 87 |
| 2 | 3 | 英语 | 7 | 郑竹 | 1989-07-01 | 女 | 98 |
| 3 | 3 | 英语 | 3 | 孙风 | 1990-05-20 | 男 | 80 |
+------+------+--------+------+--------+------------+-------+---------+

23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
  1. select
  2. c.c_id as 课程编号,c.c_name as 课程名称,
  3. sum(case when s_score between 85 and 100 then 1 else 0 end) as '[100-85]',
  4. concat(round(sum(case when s_score between 85 and 100 then 1 else 0 end) / count(*) * 100, 2), '%') as 百分比,
  5. sum(case when s_score between 70 and 85 then 1 else 0 end) as '[85-70]',
  6. concat(round(sum(case when s_score between 70 and 85 then 1 else 0 end) / count(*) * 100, 2), '%') as 百分比,
  7. sum(case when s_score between 60 and 70 then 1 else 0 end) as '[70-60]',
  8. concat(round(sum(case when s_score between 60 and 70 then 1 else 0 end) / count(*) * 100, 2), '%') as 百分比,
  9. sum(case when s_score between 0 and 60 then 1 else 0 end) as '[0-60]',
  10. concat(round(sum(case when s_score between 0 and 60 then 1 else 0 end) / count(*) * 100, 2), '%') as 百分比
  11. from course c
  12. inner join score sc
  13. on c.c_id = sc.c_id
  14. group by c.c_id;

+----------+----------+----------+--------+---------+--------+---------+--------+--------+--------+
| 课程编号 | 课程名称 | [100-85] | 百分比 | [85-70] | 百分比 | [70-60] | 百分比 | [0-60] | 百分比 |
+----------+----------+----------+--------+---------+--------+---------+--------+--------+--------+
| 1 | 语文 | 0 | 0.00% | 4 | 66.67% | 1 | 16.67% | 2 | 33.33% |
| 2 | 数学 | 3 | 50.00% | 1 | 16.67% | 1 | 16.67% | 2 | 33.33% |
| 3 | 英语 | 2 | 33.33% | 1 | 16.67% | 1 | 16.67% | 2 | 33.33% |
+----------+----------+----------+--------+---------+--------+---------+--------+--------+--------+

24、查询学生平均成绩及其名次
  1. select (@i := @i + 1) as rank,t2.*
  2. from (select @i := 0) var
  3. cross join (
  4. select s.s_id,s.s_name,avg_score
  5. from student s
  6. inner join (
  7. select s_id,round(avg(s_score), 2) as avg_score
  8. from score
  9. group by s_id
  10. ) t1
  11. on s.s_id = t1.s_id
  12. order by avg_score desc
  13. ) t2;

+------+------+--------+-----------+
| rank | s_id | s_name | avg_score |
+------+------+--------+-----------+
| 1 | 7 | 郑竹 | 93.50 |
| 2 | 1 | 赵雷 | 89.67 |
| 3 | 5 | 周梅 | 81.50 |
| 4 | 3 | 孙风 | 80.00 |
| 5 | 2 | 钱电 | 65.00 |
| 6 | 4 | 李云 | 40.00 |
| 7 | 6 | 吴兰 | 32.50 |
+------+------+--------+-----------+

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

方法 1:内外关联

  1. select c.c_id,c.c_name,s.s_id,s.s_name,s_score
  2. from (
  3. select *
  4. from score sc
  5. where (
  6. select count(*)
  7. from score sc1
  8. where sc.c_id = sc1.c_id
  9. and sc.s_score < sc1.s_score
  10. ) < 3
  11. )
  12. t1
  13. inner join student s on t1.s_id = s.s_id
  14. inner join course c on t1.c_id = c.c_id
  15. order by c.c_id,s_score desc;

+------+--------+------+--------+---------+
| c_id | c_name | s_id | s_name | s_score |
+------+--------+------+--------+---------+
| 1 | 语文 | 1 | 赵雷 | 80 |
| 1 | 语文 | 3 | 孙风 | 80 |
| 1 | 语文 | 5 | 周梅 | 76 |
| 2 | 数学 | 1 | 赵雷 | 90 |
| 2 | 数学 | 7 | 郑竹 | 89 |
| 2 | 数学 | 5 | 周梅 | 87 |
| 3 | 英语 | 1 | 赵雷 | 99 |
| 3 | 英语 | 7 | 郑竹 | 98 |
| 3 | 英语 | 3 | 孙风 | 80 |
+------+--------+------+--------+---------+

方法 2:序号筛选

  1. select *
  2. from (
  3. select
  4. (@i := case when @pre_group_id = c_id then @i + 1 else 1 end) as rank,
  5. (@pre_group_id := c_id) as c_id,
  6. c_name,s_id,s_name,s_score
  7. from (select @i := 0, @pre_group_id := 1) var
  8. cross join (
  9. select c.c_id,c.c_name,s.s_id,s.s_name,s_score
  10. from score sc
  11. inner join student s on sc.s_id = s.s_id
  12. inner join course c on sc.c_id = c.c_id
  13. group by c.c_id,s.s_id
  14. order by c.c_id,s_score desc
  15. ) t1
  16. ) t2
  17. where rank <= 3;

+------+------+--------+------+--------+---------+
| rank | c_id | c_name | s_id | s_name | s_score |
+------+------+--------+------+--------+---------+
| 1 | 1 | 语文 | 1 | 赵雷 | 80 |
| 2 | 1 | 语文 | 3 | 孙风 | 80 |
| 3 | 1 | 语文 | 5 | 周梅 | 76 |
| 1 | 2 | 数学 | 1 | 赵雷 | 90 |
| 2 | 2 | 数学 | 7 | 郑竹 | 89 |
| 3 | 2 | 数学 | 5 | 周梅 | 87 |
| 1 | 3 | 英语 | 1 | 赵雷 | 99 |
| 2 | 3 | 英语 | 7 | 郑竹 | 98 |
| 3 | 3 | 英语 | 3 | 孙风 | 80 |
+------+------+--------+------+--------+---------+

26、查询每门课程被选修的学生数
  1. select c.c_id,c.c_name,cnt_student
  2. from course c
  3. inner join (
  4. select c_id,count(*) as cnt_student
  5. from score
  6. group by c_id
  7. ) t1
  8. on c.c_id = t1.c_id;

+------+--------+-------------+
| c_id | c_name | cnt_student |
+------+--------+-------------+
| 1 | 语文 | 6 |
| 2 | 数学 | 6 |
| 3 | 英语 | 6 |
+------+--------+-------------+

27、查询出只有两门课程的全部学生的学号和姓名
  1. select s_id,s_name
  2. from student
  3. where s_id in (
  4. select s_id
  5. from score
  6. group by s_id
  7. having count(*) = 2
  8. );

+------+--------+
| s_id | s_name |
+------+--------+
| 5 | 周梅 |
| 6 | 吴兰 |
| 7 | 郑竹 |
+------+--------+

28、查询男生、女生人数
  1. select s_sex,count(*) as cnt_sex
  2. from student
  3. group by s_sex;

+-------+---------+
| s_sex | cnt_sex |
+-------+---------+
| 女 | 4 |
| 男 | 4 |
+-------+---------+

29、查询名字中含有"风"字的学生信息
  1. select *
  2. from student
  3. where s_name like '%风%';

+------+--------+------------+-------+
| s_id | s_name | s_birth | s_sex |
+------+--------+------------+-------+
| 3 | 孙风 | 1990-05-20 | 男 |
+------+--------+------------+-------+

30、查询同名同性学生名单,并统计同名人数
  1. select *,count(*) as cnt_student
  2. from student
  3. group by s_name,s_sex
  4. having cnt_student > 1;

+-----------+
| Empty set |
+-----------+

31、查询1990年出生的学生名单
  1. select *
  2. from student
  3. where year(s_birth) = '1990';

+------+--------+------------+-------+
| s_id | s_name | s_birth | s_sex |
+------+--------+------------+-------+
| 1 | 赵雷 | 1990-01-01 | 男 |
| 2 | 钱电 | 1990-12-21 | 男 |
| 3 | 孙风 | 1990-05-20 | 男 |
| 4 | 李云 | 1990-08-06 | 男 |
| 8 | 王菊 | 1990-01-20 | 女 |
+------+--------+------------+-------+

32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
  1. select c.c_id,c.c_name,avg_score
  2. from course c
  3. inner join (
  4. select c_id,round(avg(s_score), 2) as avg_score
  5. from score
  6. group by c_id
  7. ) t1
  8. on c.c_id = t1.c_id
  9. order by avg_score desc,c.c_id;

+------+--------+-----------+
| c_id | c_name | avg_score |
+------+--------+-----------+
| 2 | 数学 | 72.67 |
| 3 | 英语 | 69.33 |
| 1 | 语文 | 64.50 |
+------+--------+-----------+

33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
  1. select s.s_id,s.s_name,avg_score
  2. from student s
  3. inner join (
  4. select s_id,round(avg(s_score), 2) as avg_score
  5. from score
  6. group by s_id
  7. having avg_score >= 85
  8. ) t1
  9. on s.s_id = t1.s_id;

+------+--------+-----------+
| s_id | s_name | avg_score |
+------+--------+-----------+
| 1 | 赵雷 | 89.67 |
| 7 | 郑竹 | 93.50 |
+------+--------+-----------+

34、查询课程名称为"数学",且分数低于60的学生姓名和分数
  1. select s_name,s_score
  2. from student s
  3. inner join (
  4. select s_id,s_score
  5. from score sc
  6. inner join course c on sc.c_id = c.c_id
  7. where c_name = '数学'
  8. and s_score < 60
  9. ) t1
  10. on s.s_id = t1.s_id;

+--------+---------+
| s_name | s_score |
+--------+---------+
| 李云 | 30 |
+--------+---------+

35、查询所有学生的课程及分数情况
  1. select
  2. s.s_id,s.s_name,
  3. sum(case c_id when 1 then s_score else 0 end) as 语文,
  4. sum(case c_id when 2 then s_score else 0 end) as 数学,
  5. sum(case c_id when 3 then s_score else 0 end) as 英语
  6. from student s
  7. left join score sc on s.s_id = sc.s_id
  8. group by s.s_id;

+------+--------+------+------+------+
| s_id | s_name | 语文 | 数学 | 英语 |
+------+--------+------+------+------+
| 1 | 赵雷 | 80 | 90 | 99 |
| 2 | 钱电 | 70 | 60 | 65 |
| 3 | 孙风 | 80 | 80 | 80 |
| 4 | 李云 | 50 | 30 | 40 |
| 5 | 周梅 | 76 | 87 | 0 |
| 6 | 吴兰 | 31 | 0 | 34 |
| 7 | 郑竹 | 0 | 89 | 98 |
| 8 | 王菊 | 0 | 0 | 0 |
+------+--------+------+------+------+

36、查询任何一门课程成绩在70分以上的学生姓名、课程名称和分数
  1. select s_name,c_name,s_score
  2. from score sc
  3. inner join student s on sc.s_id = s.s_id
  4. inner join course c on sc.c_id = c.c_id
  5. where s_score > 70
  6. group by s.s_id;

+--------+--------+---------+
| s_name | c_name | s_score |
+--------+--------+---------+
| 赵雷 | 语文 | 80 |
| 孙风 | 语文 | 80 |
| 周梅 | 语文 | 76 |
| 郑竹 | 数学 | 89 |
+--------+--------+---------+

37、查询课程不及格的学生
  1. select s.s_id,s.s_name,c.c_id,c.c_name,s_score
  2. from score sc
  3. inner join student s on sc.s_id = s.s_id
  4. inner join course c on sc.c_id = c.c_id
  5. where s_score < 60;

+------+--------+------+--------+---------+
| s_id | s_name | c_id | c_name | s_score |
+------+--------+------+--------+---------+
| 4 | 李云 | 1 | 语文 | 50 |
| 4 | 李云 | 2 | 数学 | 30 |
| 4 | 李云 | 3 | 英语 | 40 |
| 6 | 吴兰 | 1 | 语文 | 31 |
| 6 | 吴兰 | 3 | 英语 | 34 |
+------+--------+------+--------+---------+

38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名
  1. select s.s_id,s.s_name
  2. from student s
  3. where s_id in (
  4. select s_id
  5. from score
  6. where c_id = 1
  7. and s_score >= 80
  8. );

+------+--------+
| s_id | s_name |
+------+--------+
| 1 | 赵雷 |
| 3 | 孙风 |
+------+--------+

39、求每门课程的学生人数
  1. select c.c_id,c.c_name,cnt_student
  2. from course c
  3. inner join (
  4. select c_id,count(*) as cnt_student
  5. from score
  6. group by c_id
  7. ) t1
  8. on c.c_id = t1.c_id;

+------+--------+-------------+
| c_id | c_name | cnt_student |
+------+--------+-------------+
| 1 | 语文 | 6 |
| 2 | 数学 | 6 |
| 3 | 英语 | 6 |
+------+--------+-------------+

40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
  1. select s.*,s_score as max_score
  2. from student s
  3. inner join (
  4. select s_id,s_score
  5. from score sc
  6. inner join (
  7. select c_id
  8. from course c
  9. inner join teacher t on c.t_id = t.t_id
  10. where t_name = '张三'
  11. ) t1
  12. on sc.c_id = t1.c_id
  13. order by s_score desc
  14. limit 1
  15. ) t2
  16. on s.s_id = t2.s_id;

+------+--------+------------+-------+-----------+
| s_id | s_name | s_birth | s_sex | max_score |
+------+--------+------------+-------+-----------+
| 1 | 赵雷 | 1990-01-01 | 男 | 90 |
+------+--------+------------+-------+-----------+

41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
  1. select *
  2. from score
  3. where s_score in (
  4. select s_score
  5. from score
  6. group by s_score
  7. having count(*) > 1
  8. );

+------+------+---------+
| s_id | c_id | s_score |
+------+------+---------+
| 1 | 1 | 80 |
| 3 | 1 | 80 |
| 3 | 2 | 80 |
| 3 | 3 | 80 |
+------+------+---------+

42、查询每门课程成绩最好的前三名

方法 1:内外关联

  1. select c.c_id,c.c_name,s.s_id,s.s_name,s_score
  2. from (
  3. select *
  4. from score sc
  5. where (
  6. select count(*)
  7. from score sc1
  8. where sc.c_id = sc1.c_id
  9. and sc.s_score < sc1.s_score
  10. ) < 3
  11. ) t1
  12. inner join student s on t1.s_id = s.s_id
  13. inner join course c on t1.c_id = c.c_id
  14. order by c.c_id,s_score desc;

+------+--------+------+--------+---------+
| c_id | c_name | s_id | s_name | s_score |
+------+--------+------+--------+---------+
| 1 | 语文 | 1 | 赵雷 | 80 |
| 1 | 语文 | 3 | 孙风 | 80 |
| 1 | 语文 | 5 | 周梅 | 76 |
| 2 | 数学 | 1 | 赵雷 | 90 |
| 2 | 数学 | 7 | 郑竹 | 89 |
| 2 | 数学 | 5 | 周梅 | 87 |
| 3 | 英语 | 1 | 赵雷 | 99 |
| 3 | 英语 | 7 | 郑竹 | 98 |
| 3 | 英语 | 3 | 孙风 | 80 |
+------+--------+------+--------+---------+

方法 2:序号筛选

  1. select *
  2. from (
  3. select
  4. (@i := case when @pre_group_id = c_id then @i + 1 else 1 end) as rank,
  5. (@pre_group_id := c_id) as c_id,
  6. c_name,s_id,s_name,s_score
  7. from (select @i := 0, @pre_group_id := 1) var
  8. cross join (
  9. select c.c_id,c.c_name,s.s_id,s.s_name,s_score
  10. from score sc
  11. inner join student s on sc.s_id = s.s_id
  12. inner join course c on sc.c_id = c.c_id
  13. group by c.c_id,s.s_id
  14. order by c.c_id,s_score desc
  15. ) t1
  16. ) t2
  17. where rank <= 3;

+------+------+--------+------+--------+---------+
| rank | c_id | c_name | s_id | s_name | s_score |
+------+------+--------+------+--------+---------+
| 1 | 1 | 语文 | 1 | 赵雷 | 80 |
| 2 | 1 | 语文 | 3 | 孙风 | 80 |
| 3 | 1 | 语文 | 5 | 周梅 | 76 |
| 1 | 2 | 数学 | 1 | 赵雷 | 90 |
| 2 | 2 | 数学 | 7 | 郑竹 | 89 |
| 3 | 2 | 数学 | 5 | 周梅 | 87 |
| 1 | 3 | 英语 | 1 | 赵雷 | 99 |
| 2 | 3 | 英语 | 7 | 郑竹 | 98 |
| 3 | 3 | 英语 | 3 | 孙风 | 80 |
+------+------+--------+------+--------+---------+

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

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

  1. select c.c_id,cnt_student
  2. from course c
  3. inner join (
  4. select c_id,count(*) as cnt_student
  5. from score
  6. group by c_id
  7. having cnt_student > 5
  8. ) t1
  9. on c.c_id = t1.c_id
  10. order by cnt_student desc,c.c_id;

+------+-------------+
| c_id | cnt_student |
+------+-------------+
| 1 | 6 |
| 2 | 6 |
| 3 | 6 |
+------+-------------+

44、检索至少选修两门课程的学生学号
  1. select s_id
  2. from score
  3. group by s_id
  4. having count(*) >= 2;

+------+
| s_id |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
+------+

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

  1. select *
  2. from student
  3. where s_id in (
  4. select s_id
  5. from score
  6. group by s_id
  7. having count(*) = (
  8. select count(*) from course
  9. )
  10. );

+------+--------+------------+-------+
| s_id | s_name | s_birth | s_sex |
+------+--------+------------+-------+
| 1 | 赵雷 | 1990-01-01 | 男 |
| 2 | 钱电 | 1990-12-21 | 男 |
| 3 | 孙风 | 1990-05-20 | 男 |
| 4 | 李云 | 1990-08-06 | 男 |
+------+--------+------------+-------+

46、查询各学生的年龄(周岁)

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

  1. select
  2. s_id,s_name,s_birth,
  3. if (
  4. month(current_date()) < month(s_birth) or (month(current_date()) = month(s_birth) and day(current_date()) < day(s_birth)),
  5. year(current_date()) - year(s_birth) -1,
  6. year(current_date()) - year(s_birth)
  7. )
  8. as s_age
  9. from student;
47、查询本周过生日的学生
  1. select *
  2. from student
  3. where
  4. datediff(concat(year(current_date()), date_format(s_birth, '-%m-%d')), current_date())
  5. between 0 and 7
  6. or
  7. datediff(concat(year(current_date()) + 1, date_format(s_birth, '-%m-%d')), current_date())
  8. between 0 and 7;
48、查询下周过生日的学生
  1. select *
  2. from student
  3. where
  4. datediff(concat(year(current_date()), date_format(s_birth, '-%m-%d')), current_date())
  5. between 7 and 14
  6. or
  7. datediff(concat(year(current_date()) + 1, date_format(s_birth, '-%m-%d')), current_date())
  8. between 7 and 14;

49、查询本月过生日的学生

  1. select *
  2. from student
  3. where month(s_birth) = month(current_date());

50、查询12月份过生日的学生

  1. select *
  2. from student
  3. where month(s_birth) = 12;

+------+--------+------------+-------+
| s_id | s_name | s_birth | s_sex |
+------+--------+------------+-------+
| 2 | 钱电 | 1990-12-21 | 男 |
| 5 | 周梅 | 1991-12-01 | 女 |
+------+--------+------------+-------+

标签: 数据库

本文转载自: https://blog.csdn.net/AL_K_ZN/article/details/131549291
版权归原作者 小羊不会敲 所有, 如有侵权,请联系我们删除。

“MySQL 经典练习 50 题(完美解答版)”的评论:

还没有评论