MySQL 经典30题,拿走不谢!!!

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
云数据库 RDS PostgreSQL,集群系列 2核4GB
简介: 还在等什么,快来一起讨论关注吧,公众号【八点半技术站】,欢迎加入社群

 Hello,大家好。我是公众号“八点半技术站”的小编-Bruce.D。

新的一周又来了,今天是周一(2020-03-30),分享一句谚语 “胸有凌云志,无高不可攀分享给大家的是 「MySQL 模块」- 经典sql 30题。在这里我希望通过此篇 “经典 sql 30题” ,能对大家在 sql 方面,有一定的帮助与理解。

01

表结构/表数据

第一小节:表结构

【 学生表 】
CREATE TABLE Student(
    s_id VARCHAR(20) COMMENT '学生编号',
    s_name VARCHAR(20) NOT NULL DEFAULT '' COMMENT '学生姓名',
    s_birth VARCHAR(20) NOT NULL DEFAULT '' COMMENT '出生年月',
    s_sex VARCHAR(10) NOT NULL DEFAULT '' COMMENT '学生性别',
    PRIMARY KEY(s_id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT '学生表';

image.gif

【 课程表 】
CREATE TABLE Course(
    c_id  VARCHAR(20) COMMENT '课程编号',
    c_name VARCHAR(20) NOT NULL DEFAULT '' COMMENT '课程名称',
    t_id VARCHAR(20) NOT NULL COMMENT '教师编号',
    PRIMARY KEY(c_id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT '课程表';

image.gif

【 教师表 】
CREATE TABLE Teacher(
    t_id VARCHAR(20) COMMENT '教师编号',
    t_name VARCHAR(20) NOT NULL DEFAULT '' COMMENT '教师姓名',
    PRIMARY KEY(t_id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT '教师表';

image.gif

【 成绩表 】
CREATE TABLE Score(
    s_id VARCHAR(20) COMMENT '学生编号',
    c_id  VARCHAR(20) COMMENT '课程编号',
    s_score INT(3) COMMENT '分数',
    PRIMARY KEY(s_id,c_id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT '成绩表';

image.gif

第二小节:表数据(方便大家操作后续模块sql语句)

-- 插入学生表测试数据
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('08' , '王菊' , '1990-01-20' , '女');
-- 课程表测试数据
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');
-- 教师表测试数据
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');
-- 成绩表测试数据
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' , 80);
insert into Score values('03' , '01' , 80);
insert into Score values('03' , '02' , 80);

image.gif

02

经典30题 sql

这里将开始我们的 sql 之旅,在这里希望对 sql 能力稍弱的同学,有一定的帮助。 如果大家在以下 sql 学习中,发现更具有优化性的建议,可以留言给小编或者加技术群交流,让我们一起成长。(底部有WeChat方式)

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

SELECT
    a.*, b.s_score AS score1,
    c.s_score AS score2
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' OR c.c_id =NULL)
WHERE
    b.s_score > c.s_score ;

image.gif

2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数

SELECT
    a.*, b.s_score AS score1,
    c.s_score AS score2
FROM
    student a
LEFT JOIN score b ON a.s_id = b.s_id
AND (b.c_id = '01' OR b.c_id =NULL)
LEFT JOIN score c ON a.s_id = c.s_id
AND c.c_id = '02' 
WHERE
    b.s_score < c.s_score ;

image.gif

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

SELECT
    a.s_id,
    a.s_name,
    ROUND(AVG(b.s_score), 1) AS 平均成绩
FROM
    student a
LEFT JOIN score b ON a.s_id = b.s_id
GROUP BY
    a.s_id
HAVING
    AVG(b.s_score) >= 60;

image.gif

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

SELECT
    a.s_id,a.s_name,
    ROUND(AVG(b.s_score),2) AS avg_score
FROM
    student a
JOIN score b ON a.s_id = b.s_id
GROUP BY
    a.s_id
HAVING AVG(b.s_score) < 60
UNION
SELECT a.s_id,a.s_name,0 AS avg_score FROM 
student a
WHERE a.s_id NOT IN (SELECT DISTINCT s_id FROM score);

image.gif

5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩,并从高到低排序

SELECT
    a.s_id,
    a.s_name,
    COUNT(b.s_id) AS 选课总数 ,
    SUM(b.s_score) AS 总成绩
FROM
    student a
LEFT JOIN score b
ON
    a.s_id = b.s_id
GROUP BY a.s_id
ORDER BY SUM(b.s_score) DESC;

image.gif

6、查询各学生的年龄

SELECT s_id,s_birth,(DATE_FORMAT(NOW(),'%Y')-DATE_FORMAT(s_birth,'%Y'))-
(CASE WHEN DATE_FORMAT(NOW(),'%m%d')< DATE_FORMAT(s_birth,'%m%d') THEN 1 ELSE 0 END) AS age
FROM student

image.gif

7、查询学过"张三"老师授课的同学的信息

SELECT *FROM student WHERE 
s_id IN (SELECT s_id FROM score WHERE 
c_id = (SELECT c_id FROM course WHERE
t_id = (SELECT t_id FROM teacher WHERE t_name = '张三'))
);

image.gif

8、查询每门功课成绩最好的前两名

SELECT *FROM
(SELECT a.s_id,a.c_id,a.s_score, @i:=@i+1 as 排名 FROM score a,(SELECT @i:=0)b WHERE a.c_id='01' ORDER BY a.s_score DESC
) c
WHERE 排名 BETWEEN 1 AND 2
UNION
SELECT *FROM
(SELECT a.s_id,a.c_id,a.s_score, @j:=@j+1 as 排名 FROM score a,(SELECT @j:=0)b WHERE a.c_id='02' ORDER BY a.s_score DESC
) c
WHERE 排名 BETWEEN 1 AND 2
UNION
SELECT *FROM
(SELECT a.s_id,a.c_id,a.s_score, @k:=@k+1 as 排名 FROM score a,(SELECT @k:=0)b WHERE a.c_id='03' ORDER BY a.s_score DESC
) c
WHERE 排名 BETWEEN 1 AND 2

image.gif

9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息,及两门课程成绩

SELECT
    a.*, b.s_score,
    c.s_score 
FROM
    student a
JOIN score b ON a.s_id = b.s_id
AND b.c_id = '01'
JOIN score c ON a.s_id = c.s_id
AND c.c_id = '02';

image.gif

10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息

SELECT *FROM student a
WHERE
a.s_id IN (SELECT s_id FROM score  WHERE c_id = '01')
AND a.s_id NOT IN (SELECT s_id FROM score  WHERE c_id = '02');

image.gif

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

SELECT *FROM student d WHERE d.s_id IN(
    SELECT e.s_id FROM score e WHERE e.s_id NOT IN(
        SELECT a.s_id FROM score a 
            JOIN score b ON a.s_id=b.s_id AND b.c_id='02'
            JOIN score c ON a.s_id=c.s_id AND c.c_id='03'
            WHERE a.c_id='01')
);

image.gif

12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息

SELECT * FROM student a
WHERE
a.s_id IN (SELECT DISTINCT b.s_id FROM score b WHERE 
b.c_id IN (SELECT c.c_id FROM score c WHERE c.s_id ='01')
);

image.gif

13、查询和"01"号的同学学习的课程完全相同的其他同学的信息

SELECT * FROM student WHERE s_id IN(
    SELECT DISTINCT s_id FROM score WHERE s_id!='01' AND c_id IN (SELECT c_id FROM score WHERE s_id ='01') 
    GROUP BY s_id
    HAVING COUNT(1)=(SELECT COUNT(1) FROM score WHERE s_id='01')
)

image.gif

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

SELECT
    a.s_id,
    a.s_name,
    ROUND(AVG(b.s_score), 1) AS 平均成绩 
FROM
    student a
LEFT JOIN score b ON a.s_id = b.s_id
GROUP BY a.s_id
HAVING
a.s_id IN(SELECT s_id FROM score WHERE s_score<60 GROUP BY s_id HAVING COUNT(1) >= 2)

image.gif

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

SELECT
    a.*, b.s_score
FROM
    student a
LEFT JOIN score b ON a.s_id = b.s_id
WHERE
    b.c_id = '01'
AND b.s_score < 60
ORDER BY
    b.s_score DESC;

image.gif

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

SELECT 
 a.s_id,
 b.s_score,
 c.s_score,
 d.s_score,
 ROUND(avg(a.s_score), 2) AS 平均分
FROM
    score 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'
LEFT JOIN score d ON a.s_id = d.s_id AND d.c_id='03'
GROUP BY a.s_id
ORDER BY 平均分 DESC;

image.gif

17、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率

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

image.gif

SELECT
    a.c_id AS 课程ID,
    b.c_name AS 课程name,
    MAX(a.s_score) AS 最高分,
    MIN(a.s_score) AS 最低分,
    ROUND(AVG(a.s_score),2) AS 平均分,
  ROUND(100*(SUM(CASE WHEN a.s_score >= 60 THEN 1 ELSE 0 END) / COUNT(1)) , 2)  AS '及格率',
    ROUND(100*(SUM(CASE WHEN a.s_score >= 70 AND  a.s_score <80 THEN 1 ELSE 0 END) / COUNT(1)) , 2)  AS '中等率',
  ROUND(100*(SUM(CASE WHEN a.s_score >= 80 AND  a.s_score <90 THEN 1 ELSE 0 END) / COUNT(1)) , 2)  AS '优良率',
  ROUND(100*(SUM(CASE WHEN a.s_score >= 90 THEN 1 ELSE 0 END) / COUNT(1)) , 2)  AS '优秀率'
FROM
    score a
LEFT JOIN course b ON a.c_id = b.c_id
GROUP BY
    b.c_id;

image.gif

18、按各科成绩进行排序,并显示排名(mysql没有rank顺序函数)

select a.s_id,a.c_id,
        @i:=@i +1 as i保留排名,
        @k:=(case when @score=a.s_score then @k else @i end) as rank不保留排名,
        @score:=a.s_score as score
    from (
        select s_id,c_id,s_score from score WHERE c_id='01' GROUP BY s_id,c_id,s_score ORDER BY s_score DESC
)a,(select @k:=0,@i:=0,@score:=0)s
    union
    select a.s_id,a.c_id,
        @i:=@i +1 as i,
        @k:=(case when @score=a.s_score then @k else @i end) as rank,
        @score:=a.s_score as score
    from (
        select s_id,c_id,s_score from score WHERE c_id='02' GROUP BY s_id,c_id,s_score ORDER BY s_score DESC
)a,(select @k:=0,@i:=0,@score:=0)s
    union
    select a.s_id,a.c_id,
        @i:=@i +1 as i,
        @k:=(case when @score=a.s_score then @k else @i end) as rank,
        @score:=a.s_score as score
    from (
        select s_id,c_id,s_score from score WHERE c_id='03' GROUP BY s_id,c_id,s_score ORDER BY s_score DESC
)a,(select @k:=0,@i:=0,@score:=0)s

image.gif

19、查询学生的总成绩并进行排名

SELECT a.s_id,
    @i:=@i+1 AS i,
    @k:=(CASE WHEN @score=a.sum_score THEN @k ELSE @i END) AS rank,
    @score:=a.sum_score AS score
FROM (SELECT s_id,SUM(s_score) AS sum_score FROM score GROUP BY s_id ORDER BY sum_score DESC) AS a,
(SELECT @i:=0,@score:=0) AS b

image.gif

20、查询不同老师所教不同课程平均分从高到低显示

SELECT
    a.t_name,
    b.c_id,
    b.c_name,
    ROUND(AVG(c.s_score) ,2) AS 平均分
FROM
    teacher a
LEFT JOIN course b ON a.t_id = b.t_id
LEFT JOIN score c ON b.c_id=c.c_id
GROUP BY c.c_id
ORDER BY AVG(c.s_score) DESC;

image.gif

21、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩

SELECT c.*,d.s_name,d.s_birth,d.s_sex FROM 
(SELECT a.s_id,a.s_score,a.c_id,@i:=@i+1 AS 排名 FROM score a,(SELECT @i:=0)b WHERE a.c_id='01') c 
LEFT JOIN student d ON c.s_id=d.s_id
WHERE 排名 BETWEEN 2 AND 3
UNION
SELECT c.*,d.s_name,d.s_birth,d.s_sex FROM 
(SELECT a.s_id,a.s_score,a.c_id,@j:=@j+1 AS 排名 FROM score a,(SELECT @j:=0)b WHERE a.c_id='02') c 
LEFT JOIN student d ON c.s_id=d.s_id
WHERE 排名 BETWEEN 2 AND 3
UNION
SELECT c.*,d.s_name,d.s_birth,d.s_sex FROM 
(SELECT a.s_id,a.s_score,a.c_id,@k:=@k+1 AS 排名 FROM score a,(SELECT @k:=0)b WHERE a.c_id='03') c 
LEFT JOIN student d ON c.s_id=d.s_id
WHERE 排名 BETWEEN 2 AND 3;

image.gif

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

SELECT
    a.c_id AS 课程编号, a.c_name AS 课程名称,
  c.`[100-85]的人数`, c.`[100-85]所占百分比`,
    d.`[85-70]的人数`, d.`[85-70]所占百分比`,
    e.`[70-60]的人数`, e.`[70-60]所占百分比`,
    f.`[0-60]的人数`, f.`[0-60]所占百分比`
FROM
    course a
LEFT JOIN score b ON a.c_id = b.c_id
LEFT JOIN
    (SELECT *,SUM(CASE WHEN s_score >85 AND s_score <=100 THEN 1 ELSE 0 END) AS '[100-85]的人数' ,
    ROUND(SUM(CASE WHEN s_score >85 AND s_score <=100 THEN 1 ELSE 0 END)/COUNT(1)*100,2) AS '[100-85]所占百分比'
    FROM score GROUP BY c_id) c ON a.c_id=c.c_id
LEFT JOIN
    (SELECT*,SUM(CASE WHEN s_score >70 AND s_score <=85 THEN 1 ELSE 0 END) AS '[85-70]的人数' ,
    ROUND(SUM(CASE WHEN s_score >70 AND s_score <=85 THEN 1 ELSE 0 END)/COUNT(1)*100,2) AS '[85-70]所占百分比'
    FROM score GROUP BY c_id) d ON a.c_id=d.c_id
LEFT JOIN
    (SELECT*,SUM(CASE WHEN s_score >60 AND s_score <=70 THEN 1 ELSE 0 END) AS '[70-60]的人数' ,
    ROUND(SUM(CASE WHEN s_score >60 AND s_score <=70 THEN 1 ELSE 0 END)/COUNT(1)*100,2) AS '[70-60]所占百分比'
    FROM score GROUP BY c_id) e ON a.c_id=e.c_id
LEFT JOIN
    (SELECT *,SUM(CASE WHEN s_score >0 AND s_score <=60 THEN 1 ELSE 0 END) AS '[0-60]的人数' ,
    ROUND(SUM(CASE WHEN s_score >0 AND s_score <=60 THEN 1 ELSE 0 END)/COUNT(1)*100,2) AS '[0-60]所占百分比'
    FROM score GROUP BY c_id) f ON a.c_id=f.c_id
GROUP BY a.c_id

image.gif

23、查询学生平均成绩及其名次

SELECT
    b.s_id,
    @i:=@i+1 AS 相同分数的不同名次,
    @k:=(CASE WHEN @avg_s=b.avg_score THEN @k ELSE @i END) AS 相同分数的相同名次,
    @avg_s:=b.avg_score AS 平均成绩
FROM
(SELECT
    a.s_id,
    ROUND(AVG(a.s_score), 2) AS avg_score
FROM
    score a
GROUP BY
    a.s_id
ORDER BY AVG(a.s_score) DESC) b,(SELECT @i:=0,@avg_s:=0,@k:=0) c

image.gif

24、查询出只有两门课程的全部学生的学号和姓名

SELECT a.s_id,a.s_name FROM student a 
LEFT JOIN score b ON a.s_id=b.s_id
GROUP BY a.s_id
HAVING COUNT(1)=2

image.gif

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

SELECT a.s_name,a.s_sex,COUNT(1) AS 人数 FROM student a 
JOIN student b ON a.s_name=b.s_name AND a.s_sex=b.s_sex AND a.s_id!=b.s_id
GROUP BY a.s_name,a.s_sex

image.gif

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

SELECT c_id,ROUND(avg(s_score),2)FROM score 
GROUP BY c_id
ORDER BY avg(s_score) DESC,c_id ASC

image.gif

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

SELECT a.s_name,b.s_score FROM student a 
LEFT JOIN score b ON a.s_id=b.s_id
WHERE c_id=(
SELECT c_id FROM course WHERE c_name='数学'
) AND b.s_score < 60

image.gif

28、查询所有学生的课程及分数情况;

SELECT a.s_id,a.s_name,
SUM(CASE c.c_name WHEN '语文' THEN b.s_score ELSE 0 END) AS '语文',
SUM(CASE c.c_name WHEN '数学' THEN b.s_score ELSE 0 END) AS '数学',
SUM(CASE c.c_name WHEN '英语' THEN b.s_score ELSE 0 END) AS '英语',
SUM(b.s_score) as  '总分'
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.s_name

image.gif

29、查询不及格的学生id,姓名,及其课程名称,分数

SELECT a.s_id,a.s_name,c.c_name,b.s_score 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
WHERE b.s_score < 60

image.gif

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

SELECT c.*,d.s_score FROM student c
LEFT JOIN score d ON c.s_id=d.s_id AND d.c_id=
(SELECT c_id FROM course b WHERE t_id=(SELECT t_id FROM teacher a WHERE a.t_name='张三'))
HAVING  MAX(d.s_score)

image.gif

————————end————————

相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
6月前
|
关系型数据库 MySQL 数据库
Mysql基础第五天,Mysql简介
Mysql基础第五天,Mysql简介
46 0
Mysql基础第五天,Mysql简介
|
6月前
|
SQL 关系型数据库 MySQL
Mysql基础第六天,使用Mysql
Mysql基础第六天,使用Mysql
28 0
Mysql基础第六天,使用Mysql
|
存储 SQL 关系型数据库
一篇吃透mysql(个人笔记)
一篇吃透mysql(个人笔记)
96 0
|
存储 SQL 缓存
15天学习MySQL计划-数据库引擎(进阶篇)第六天
15天学习MySQL计划-数据库引擎(进阶篇)第六天
92 0
|
存储 监控 关系型数据库
【读书笔记】Mysql | 一图了解Mysql全貌
这段时间学习Mysql的笔记,对Mysql有个深入的了解。
|
存储 SQL JSON
MySQL技能树学习总结
MySQL技能树学习总结
233 0
MySQL技能树学习总结
|
SQL 存储 Oracle
|
SQL 关系型数据库 MySQL
深聊MySQL,从入门到入坟之:MySQL竟然也有后悔药!!!
深聊MySQL,从入门到入坟之:MySQL竟然也有后悔药!!!
74 0
|
存储 SQL 关系型数据库
MySQL 高频面试题目(3)
MySQL 高频面试题目(3)
110 0
MySQL 高频面试题目(3)
下一篇
无影云桌面