目录
- 1.建库建表
- 2.插入数据
- 3.SQL 50 题
-
- 3.1.✨SQL 01——查询”01″课程比”02″课程成绩高的学生的信息及课程分数
- 3.2.SQL 02——查询”01″课程比”02″课程成绩低的学生的信息及课程分数
- 3.3.SQL 03——查询平均成绩大于等于 60 分的同学的学生编号、学生姓名和平均成绩
- 3.4.✨SQL 04——查询平均成绩小于 60 分的同学的学生编号和学生姓名和平均成绩(包括有成绩的和无成绩的)
- 3.5.SQL 05——查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
- 3.6.SQL 06——查询”李”姓老师的数量
- 3.7.✨SQL 07——查询学过”张三”老师授课的同学的信息
- 3.8.SQL 08——查询没学过”张三”老师授课的同学的信息
- 3.9.SQL 09——查询学过编号为”01″并且也学过编号为”02″的课程的同学的信息
- 3.10.✨SQL 10——查询学过编号为”01″但是没有学过编号为”02″的课程的同学的信息
- 3.11.SQL 11——查询没有学全所有课程的同学的信息
- 3.12.SQL 12——查询至少有一门课与学号为”01″的同学所学相同的同学的信息
- 3.13.✨SQL 13——查询和”01″号的同学学习的课程完全相同的其他同学的信息
- 3.14.SQL 14——查询没学过”张三”老师讲授的任一门课程的学生姓名
- 3.15.✨SQL 15——查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
- 3.16.SQL 16——检索”01″课程分数小于60,按分数降序排列的学生信息
- 3.17.SQL 17——按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
- 3.18.✨SQL 18——查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程名称,最高分,最低分,平均分,及格率,中等率,优良率,优秀率。及格为 60-70,中等为 71-80,优良为 81-90,优秀为 >= 91。
- 3.19.✨SQL 19——按各科成绩进行排序,并显示排名,成绩重复时合并名次
- 3.20.✨SQL 20——查询学生的总成绩并进行排名,总分重复时不保留名次空缺
- 3.21.SQL 21——查询不同老师所教不同课程平均分,并从高到低显示
- 3.22.SQL 22——查询所有课程的成绩第 2 名到第 3 名的学生信息及该课程成绩
- 3.23.✨SQL 23——统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
- 3.24.SQL 24——查询学生平均成绩及其名次
- 3.25.SQL 25——查询各科成绩前三名的记录
- 3.26.SQL 26——查询每门课程被选修的学生数
- 3.27.SQL 27——查询出只有两门课程的全部学生的学号和姓名
- 3.28.SQL 28——查询男生、女生人数
- 3.29.SQL 29——查询名字中含有”风”字的学生信息
- 3.30.✨SQL 30——查询同姓名同性别学生名单,并统计同名人数
- 3.31.SQL 31——查询 1990 年出生的学生名单
- 3.32.✨SQL 32——查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
- 3.33.SQL 33——查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩
- 3.34.SQL 34——查询课程名称为”数学”,且分数低于 60 的学生姓名和分数
- 3.35.SQL 35——查询所有学生的课程及分数情况
- 3.36.SQL 36——查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数
- 3.37.SQL 37——查询不及格的课程的学生姓名、课程名称和分数
- 3.38.SQL 38——查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名
- 3.39.SQL 39——求每门课程的学生人数
- 3.40.✨SQL 40——查询选修”张三”老师所授课程的学生中,成绩最高的学生信息及其成绩
- 3.41.SQL 41——查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
- 3.42.✨SQL 42——查询每门课程成绩最好的前两名
- 3.43.SQL 43——统计每门课程的学生选修人数(超过 5 人的课程才统计),要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
- 3.44.SQL 44——查询至少选修两门课程的学生学号
- 3.45.SQL 45——查询选修了全部课程的学生信息
- 3.46.✨SQL 46——查询各学生的年龄
- 3.47.SQL 47——查询本周过生日的学生
- 3.48.SQL 48——查询下周过生日的学生
- 3.49.SQL 49——查询本月过生日的学生
- 3.50.SQL 50——查询下月过生日的学生
① 本文整理了经典的 50 道 SQL 题目,文本分为建库建表、插入数据以及 SQL 50 题这三个部分。
② 这些题目许多博主也整理过,但本人不太了解这些题目具体的出处。第一次了解这些题目是本科期间老师出的题目。如果有网友知道这些题目的最原始出处,可以在评论评论区中告知。
③ 本文所使用的 MySQL 版本为 5.5,虽然版本有一点旧,但是对 SQL 知识点的复习没有太大的影响(除了一些旧版没有的函数)。
④ 由于本文旨在对 SQL 基础知识进行复习,并且所涉及的数据量也十分的小,所以在编写 SQL 语句时,并未过多考虑 SQL 优化的方面。如果读者有其它的解法或者发现错误之处,可在评论区留言,笔者在看到后会及时更新!
1.建库建表
(1)建库:创建一个名为 sqlpractice 的数据库。
(2)建表:建立 student、course、teacher 和 score 这 4 张表。它们的字段以及之间的关系如下图所示。
(3)建库建表的完整 SQL 语句如下所示。
# 建库
create database sqlpractice;
use sqlpractice;
# 建立 Student 学生表
CREATE TABLE Student(
s_id VARCHAR(20),
s_name VARCHAR(20) NOT NULL,
s_birth VARCHAR(20) NOT NULL,
s_sex VARCHAR(10) NOT NULL,
PRIMARY KEY(s_id) # 主键
);
# 建立 Course 课程表
CREATE TABLE Course(
c_id VARCHAR(20),
c_name VARCHAR(20) NOT NULL,
t_id VARCHAR(20) NOT NULL,
PRIMARY KEY(c_id) # 主键
);
# 建立 Teacher 教师表
CREATE TABLE Teacher(
t_id VARCHAR(20),
t_name VARCHAR(20) NOT NULL DEFAULT '',
PRIMARY KEY(t_id) # 主键
);
# 建立 Score 分数表
CREATE TABLE Score(
s_id VARCHAR(20),
c_id VARCHAR(20),
s_score INT(3),
PRIMARY KEY(s_id, c_id) # 联合主键
);
# 添加外键
# 语法:ALTER TABLE 从表 ADD FOREIGN KEY(外键字段) REFERENCES 主表(主键字段)
ALTER TABLE Course ADD FOREIGN KEY(t_id) REFERENCES Teacher(t_id)
ALTER TABLE Score ADD FOREIGN KEY(s_id) REFERENCES Student(s_id)
ALTER TABLE Score ADD FOREIGN KEY(c_id) REFERENCES Course(c_id)
2.插入数据
(1)向上面创建的 4 张表中插入测试数据的 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 Teacher VALUES('01', '张三');
INSERT INTO Teacher VALUES('02', '李四');
INSERT INTO Teacher VALUES('03', '王五');
INSERT INTO Course VALUES('01', '语文', '02');
INSERT INTO Course VALUES('02', '数学', '01');
INSERT INTO Course VALUES('03', '英语', '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);
INSERT INTO Score VALUES('03', '03', 80);
INSERT INTO Score VALUES('04', '01', 50);
INSERT INTO Score VALUES('04', '02', 30);
INSERT INTO Score VALUES('04', '03', 20);
INSERT INTO Score VALUES('05', '01', 76);
INSERT INTO Score VALUES('05', '02', 87);
INSERT INTO Score VALUES('06', '01', 31);
INSERT INTO Score VALUES('06', '03', 34);
INSERT INTO Score VALUES('07', '02', 89);
INSERT INTO Score VALUES('07', '03', 98);
(2)检验插入数据是否成功
SELECT * FROM Student;
SELECT * FROM Course;
SELECT * FROM Teacher;
SELECT * FROM Score;
Student 表
Course 表
Teacher 表
Score 表
3.SQL 50 题
3.1.✨SQL 01——查询”01″课程比”02″课程成绩高的学生的信息及课程分数
# 本题需要比较"01"课程比"02"课程的成绩,故在 where 中将 score 表中的字段 s_score 使用 2 次(即分别对应"01"课程的成绩和"02"课程的成绩)
# 所以可以使用为 s_score 表取别名的方式来多次使用 score 表中的字段
SELECT
student.*,
score1.s_score
FROM
student,
score AS score1,
score AS score2
WHERE
student.s_id = score1.s_id
AND score1.s_id = score2.s_id # student, score1, score2 表连接的条件是它们的 s_id 均相等
AND score1.c_id = '01'
AND score2.c_id = '02'
AND score1.s_score > score2.s_score;
3.2.SQL 02——查询”01″课程比”02″课程成绩低的学生的信息及课程分数
SELECT
student.*,
score1.s_score
FROM
student,
score AS score1,
score AS score2
WHERE
student.s_id = score1.s_id
AND score1.s_id = score2.s_id # student, score1, score2 表连接的条件是它们的 s_id 均相等
AND score1.c_id = '01'
AND score2.c_id = '02'
AND score1.s_score score2.s_score;
3.3.SQL 03——查询平均成绩大于等于 60 分的同学的学生编号、学生姓名和平均成绩
# 1.创建临时表 ss
EXPLAIN SELECT
student.s_id,
student.s_name,
ss.avg_score
FROM
student,
(SELECT s_id, AVG(s_score) AS avg_score FROM score GROUP BY s_id) AS ss
WHERE
student.s_id = ss.s_id
AND ss.avg_score >= 60;
# 2.先进行内连接,然后再分组
SELECT
student.s_id,
s_name,
round(AVG(score.s_score), 2) as avg_score
FROM
student
INNER JOIN score ON student.s_id = score.s_id
GROUP BY
student.s_id,
s_name
HAVING
AVG(score.s_score) >= 60
3.4.✨SQL 04——查询平均成绩小于 60 分的同学的学生编号和学生姓名和平均成绩(包括有成绩的和无成绩的)
# isnull(exper) 判断 exper 是否为空,是则返回 1,否则返回 0
# ifnull(exper1, exper2) 判断 exper1 是否为空,是则用 exper2 代替
# nullif(exper1, exper2) 如果 expr1 = expr2 成立,那么返回值为 NULL,否则返回值为 expr1。
SELECT
student.s_id,
s_name,
round(AVG(score.s_score), 2) as avg_score
FROM
student
LEFT OUTER JOIN score ON student.s_id = score.s_id
GROUP BY
student.s_id,
s_name
HAVING
AVG(IFNULL(score.s_score,0)) 60
3.5.SQL 05——查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
SELECT
student.s_id,
student.s_name,
COUNT(DISTINCT c_id) AS totalCourses,
SUM(s_score) AS totalScores
FROM
student
# 由于要查询所有的学生,故无论其是否有课程信息都要查询,所以使用 LEFT OUTER JOIN
LEFT OUTER JOIN score ON student.s_id = score.s_id
GROUP BY
student.s_id,
student.s_name;
3.6.SQL 06——查询”李”姓老师的数量
# 1.模糊查询
SELECT
COUNT(*)
FROM
teacher
WHERE
t_name LIKE '李%'
# 2.正则表达式查询,字符 '^' 匹配以特定字符或者字符串开头的文本
SELECT
count(*)
FROM
teacher
WHERE
t_name REGEXP '^李'
3.7.✨SQL 07——查询学过”张三”老师授课的同学的信息
# 1.使用多表连接(score, course, teacher)找到上张三老师课的同学的 s_id,然后再根据 s_id 从 student 表中查询同学信息
SELECT
student.*
FROM
student
WHERE
s_id IN (
SELECT
s_id
FROM
score,
course,
teacher
WHERE
teacher.t_name = '张三'
AND teacher.t_id = course.t_id
AND course.c_id = score.c_id
)
# 2.多层嵌套子查询(当数据量较大时,一般不推荐使用子查询)
# 在 student 表中根据上过张三老师教的课的学生 s_id 来查询他们的信息
SELECT
student.*
FROM
student
WHERE
student.s_id IN (
# 在 score 表中根据张三老师教的课程 c_id 来查找上这些课的学生 s_id
SELECT DISTINCT
s_id
FROM
score
WHERE
score.c_id IN (
# 在 course 表中根据张三老师的 t_id 查询他所教的课程 c_id
SELECT
c_id
FROM
course
WHERE
course.t_id = (
# 在 teacher 表中查询张三老师的 t_id
SELECT
t_id
FROM
teacher
WHERE
t_name = '张三'
)
)
)
3.8.SQL 08——查询没学过”张三”老师授课的同学的信息
SELECT
student.*
FROM
student
WHERE
student.s_id NOT IN (
SELECT DISTINCT
s_id
FROM
score
WHERE
score.c_id IN (
SELECT
c_id
FROM
course
WHERE
course.t_id = (
SELECT
t_id
FROM
teacher
WHERE
t_name = '张三'
)
)
)
3.9.SQL 09——查询学过编号为”01″并且也学过编号为”02″的课程的同学的信息
SELECT
student.*
FROM
student
WHERE
student.s_id IN (
SELECT
s1.s_id
FROM
score AS s1,
score AS s2
WHERE
s1.s_id = s2.s_id
AND s1.c_id = '01'
AND s2.c_id = '02'
)
3.10.✨SQL 10——查询学过编号为”01″但是没有学过编号为”02″的课程的同学的信息
SELECT
stu.s_id,
stu.s_name,
stu.s_birth,
stu.s_sex
FROM
student AS stu
JOIN score AS sc ON stu.s_id = sc.s_id
JOIN course AS co ON co.c_id = sc.c_id
WHERE
co.c_id = '01'
AND stu.s_id NOT IN (
# 查询学过编号为 "02" 的课程的同学 id
SELECT
stu.s_id
FROM
student AS stu
JOIN score AS sc ON stu.s_id = sc.s_id
JOIN course AS co ON co.c_id = sc.c_id
WHERE
co.c_id = '02'
)
3.11.SQL 11——查询没有学全所有课程的同学的信息
# 下面的课程数量 3 也可以用 (SELECT count(*) FROM course) 来代替
SELECT
*
FROM
student
WHERE
s_id IN (SELECT s_id FROM score GROUP BY s_id HAVING count(c_id) 3)
3.12.SQL 12——查询至少有一门课与学号为”01″的同学所学相同的同学的信息
# 不包括学号为 '01' 学生自己
SELECT
*
FROM
student
WHERE
s_id IN (
SELECT DISTINCT s_id FROM score
WHERE c_id IN (SELECT c_id FROM score WHERE s_id = '01')
AND s_id != '01'
)
3.13.✨SQL 13——查询和”01″号的同学学习的课程完全相同的其他同学的信息
SELECT
*
FROM
student
WHERE
s_id IN (
SELECT
s_id
FROM
score
WHERE
# 保证学习的课程相同
c_id IN (SELECT DISTINCT c_id FROM score WHERE s_id = '01')
AND s_id != '01'
GROUP BY
s_id
HAVING
# 保证学习的课程数量相同
count(c_id) = (select count(*) from score where s_id = '01')
)
3.14.SQL 14——查询没学过”张三”老师讲授的任一门课程的学生姓名
SELECT
s_name
FROM
student
WHERE
s_id NOT IN (
# 查询学习过"张三"老师讲授的任一门课程的学生 id
SELECT
s_id
FROM
score
WHERE
c_id IN (
# 查询由姓名为张三的老师所讲授的课程 id
SELECT
c_id
FROM
course
WHERE
t_id IN (SELECT t_id FROM teacher WHERE t_name = '张三')
)
)
3.15.✨SQL 15——查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
SELECT
stu.s_id,
stu.s_name,
tmp_t.avg_score
FROM
student AS stu
INNER JOIN
(
SELECT
s_id,
round(avg(s_score), 2) AS avg_score
FROM
score
WHERE
s_score 60
GROUP BY
s_id
HAVING
count(s_score) >= 2
) AS tmp_t
ON
stu.s_id = tmp_t.s_id
3.16.SQL 16——检索”01″课程分数小于60,按分数降序排列的学生信息
SELECT
stu.*
FROM
student AS stu
INNER JOIN score ON stu.s_id = score.s_id
WHERE
c_id = '01'
AND
s_score 60
ORDER BY
s_score DESC
3.17.SQL 17——按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
SELECT
s_id,
max(CASE c_id WHEN '01' THEN s_score ELSE 0 END) AS '01',
max(CASE c_id WHEN '02' THEN s_score ELSE 0 END) AS '02',
max(CASE c_id WHEN '03' THEN s_score ELSE 0 END) AS '03',
avg(s_score) AS avg_score
FROM
score
GROUP BY
s_id
ORDER BY
avg_score DESC
3.18.✨SQL 18——查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程名称,最高分,最低分,平均分,及格率,中等率,优良率,优秀率。及格为 60-70,中等为 71-80,优良为 81-90,优秀为 >= 91。
SELECT
sc.c_id AS "课程ID",
c.c_name AS '课程名称',
MAX(sc.s_score) AS "最高分",
MIN(sc.s_score) AS '最低分',
AVG(sc.s_score) AS '平均分',
SUM(IF (sc.s_score BETWEEN 60 AND 70, 1, 0)) / COUNT(*) as '及格率',
SUM(IF (sc.s_score BETWEEN 71 AND 80, 1, 0)) / COUNT(*) as '中等率',
SUM(IF (sc.s_score BETWEEN 81 AND 90, 1, 0)) / COUNT(*) as '优良率',
SUM(IF (sc.s_score >= 91, 1, 0)) / COUNT(*) as '优秀率'
FROM
score AS sc
JOIN course AS c ON sc.c_id = c.c_id
GROUP BY
sc.c_id
3.19.✨SQL 19——按各科成绩进行排序,并显示排名,成绩重复时合并名次
SELECT
sc1.c_id,
sc1.s_id,
sc1.s_score,
count(sc2.s_score) + 1 AS rank
FROM
score AS sc1 LEFT JOIN score AS sc2
ON sc1.s_score sc2.s_score
AND sc1.c_id = sc2.c_id
GROUP BY
sc1.c_id,
sc1.s_id,
sc1.s_score
ORDER BY
sc1.c_id,
rank
3.20.✨SQL 20——查询学生的总成绩并进行排名,总分重复时不保留名次空缺
SELECT
stu.s_id,
stu.s_name,
total_score,
(
SELECT COUNT(DISTINCT total_score)
FROM (SELECT SUM(s_score) AS total_score FROM score GROUP BY s_id) AS sub
WHERE total_score >= tmp.total_score
) AS rank
FROM
student as stu
INNER JOIN (
SELECT
s_id,
SUM(s_score) AS total_score
FROM
score
GROUP BY
s_id
) AS tmp ON stu.s_id = tmp.s_id
ORDER BY
total_score DESC;
3.21.SQL 21——查询不同老师所教不同课程平均分,并从高到低显示
SELECT
teacher.t_id,
t_name,
round(avg(s_score), 2) AS avg_score
FROM
teacher,
course,
score
WHERE
teacher.t_id = course.t_id
AND course.c_id = score.c_id
GROUP BY
teacher.t_id,
t_name,
score.c_id
ORDER BY
avg(score.s_score) DESC
3.22.SQL 22——查询所有课程的成绩第 2 名到第 3 名的学生信息及该课程成绩
# 1.分别对每门课程进行查询,然后再合并查询结果,但是如果课程太多,该方法就不太合适
SELECT
t1.*
FROM
(
SELECT
st.*,
c.c_id,
c.c_name,
sc.s_score
FROM
student st
LEFT JOIN score sc ON sc.s_id = st.s_id
INNER JOIN course c ON c.c_id = sc.c_id
AND c.c_id = "01"
ORDER BY
sc.s_score DESC
LIMIT 1,
2
) as t1
UNION ALL
SELECT
t2.*
FROM
(
SELECT
st.*,
c.c_id,
c.c_name,
sc.s_score
FROM
student st
LEFT JOIN score sc ON sc.s_id = st.s_id
INNER JOIN course c ON c.c_id = sc.c_id
AND c.c_id = "02"
ORDER BY
sc.s_score DESC
LIMIT 1,
2
) as t2
UNION ALL
SELECT
t3.*
FROM
(
SELECT
st.*,
c.c_id,
c.c_name,
sc.s_score
FROM
student st
LEFT JOIN score sc ON sc.s_id = st.s_id
INNER JOIN course c ON c.c_id = sc.c_id
AND c.c_id = "03"
ORDER BY
sc.s_score DESC
LIMIT 1,
2
) as t3
# 2.一次性查询,需要注意的是 row_number() 在 MySQL 8.0 中才支持
SELECT
c_id,
student.*,
s_score
FROM
student
INNER JOIN (
SELECT
s_id,
s_score,
c_id,
row_number() over (PARTITION BY c_id ORDER BY s_score DESC) AS rank
FROM
score
) AS tmp_t ON tmp_t.s_id = student.s_id
WHERE
tmp_t.rank IN (2, 3)
3.23.✨SQL 23——统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
SELECT
score.c_id,
course.c_name,
sum(CASE WHEN s_score BETWEEN 0 AND 60 THEN 1 ELSE 0 END) AS '[0-60]人数',
sum(CASE WHEN s_score BETWEEN 61 AND 70 THEN 1 ELSE 0 END) AS '[61-70]人数',
sum(CASE WHEN s_score BETWEEN 71 AND 85 THEN 1 ELSE 0 END) AS '[71-85]人数',
sum(CASE WHEN s_score BETWEEN 86 AND 100 THEN 1 ELSE 0 END) AS '[86-100]人数',
round(sum(CASE WHEN s_score BETWEEN 0 AND 60 THEN 1 ELSE 0 END) / count(*), 2) AS '[0-60]人数所占百分比',
round(sum(CASE WHEN s_score BETWEEN 61 AND 70 THEN 1 ELSE 0 END) / count(*), 2) AS '[61-70]人数所占百分比',
round(sum(CASE WHEN s_score BETWEEN 71 AND 85 THEN 1 ELSE 0 END) / count(*), 2) AS '[71-85]人数所占百分比',
round(sum(CASE WHEN s_score BETWEEN 86 AND 100 THEN 1 ELSE 0 END) / count(*), 2) AS '[86-100]人数所占百分比'
FROM
score LEFT JOIN course
ON score.c_id = course.c_id
GROUP BY
score.c_id,
course.c_name
3.24.SQL 24——查询学生平均成绩及其名次
# 在 MySQL 8 中可以使用 rank 函数来实现排名
SELECT
stu.s_id,
stu.s_name,
round(avg(sc.s_score), 2) AS average_score,
(
SELECT COUNT(DISTINCT avg_score)
FROM (SELECT AVG(s_score) AS avg_score FROM score GROUP BY s_id) AS sub
WHERE avg_score >= AVG(sc.s_score)
) AS rank
FROM
student as stu
INNER JOIN score as sc ON stu.s_id = sc.s_id
GROUP BY
stu.s_id,
stu.s_name
ORDER BY
average_score DESC;
3.25.SQL 25——查询各科成绩前三名的记录
# 1.分别对每科进行查询,然后再合并查询结果,但是如果课程太多,该方法就不太合适
(SELECT c_id, s_score FROM score WHERE c_id = '01' ORDER BY s_score DESC LIMIT 3) UNION ALL
(SELECT c_id, s_score FROM score WHERE c_id = '02' ORDER BY s_score DESC LIMIT 3) UNION ALL
(SELECT c_id, s_score FROM score WHERE c_id = '03' ORDER BY s_score DESC LIMIT 3)
# 2.一次性查询出结果
SELECT DISTINCT
tmp_t.c_id,
tmp_t.s_score
FROM
(
SELECT DISTINCT
student.*,
sc1.c_id,
sc1.s_score,
count(DISTINCT sc2.s_score) + 1 AS rank
FROM
score AS sc1
LEFT JOIN score AS sc2 ON sc1.c_id = sc2.c_id
AND sc1.s_score sc2.s_score
LEFT JOIN student ON sc1.s_id = student.s_id
GROUP BY
sc1.c_id,
sc1.s_id
ORDER BY
sc1.c_id,
sc1.s_score DESC
) AS tmp_t
WHERE
tmp_t.rank BETWEEN 1 AND 3
3.26.SQL 26——查询每门课程被选修的学生数
SELECT
c_id,
count( s_id ) AS '选修该门课程的学生数'
FROM
score
GROUP BY
c_id
3.27.SQL 27——查询出只有两门课程的全部学生的学号和姓名
SELECT
student.s_id,
student.s_name
FROM
student,
score
WHERE
student.s_id = score.s_id
GROUP BY
s_id
HAVING
count(c_id) = 2
3.28.SQL 28——查询男生、女生人数
SELECT
sum(CASE WHEN s_sex = '男' THEN 1 ELSE NULL END) AS '男生人数',
sum(CASE WHEN s_sex = '女' THEN 1 ELSE NULL END) AS '女生人数'
FROM
student
3.29.SQL 29——查询名字中含有”风”字的学生信息
# 1.使用模糊匹配
SELECT
*
FROM
student
WHERE
s_name LIKE '%风%'
# 2.使用正则表达式
SELECT
*
FROM
student
WHERE
s_name REGEXP '风'
在 MySQL 中,LIKE 操作符用于在文本字段中搜索特定的模式。如果需要在文本字段中匹配通配符本身,可以使用反斜杠字符转义通配符。例如,如果要在一个名为 ’mytable’ 的表中查找包含下划线字符的字符串,可以使用以下查询:
SELECT * FROM mytable WHERE mycolumn LIKE '%_%' ESCAPE '';
在上面的查询中,ESCAPE 关键字指定了转义字符为反斜杠,因此我们在通配符前添加了一个反斜杠字符。这将告诉 MySQL 仅匹配下划线字符本身,而不是作为通配符进行匹配。
3.30.✨SQL 30——查询同姓名同性别学生名单,并统计同名人数
SELECT
stu1.s_name,
tmp_t.cnt AS '同名人数'
FROM
student AS stu1
LEFT JOIN (
SELECT s_name, s_sex, count(*) AS cnt
FROM student
GROUP BY s_name, s_sex
) AS tmp_t
ON stu1.s_name = tmp_t.s_name AND stu1.s_sex = tmp_t.s_sex
WHERE
tmp_t.cnt > 1
3.31.SQL 31——查询 1990 年出生的学生名单
# 1.使用模糊匹配
SELECT
*
FROM
student
WHERE
s_birth LIKE '1990%'
# 2.使用正则表达式
SELECT
*
FROM
student
WHERE
s_birth REGEXP '^1990'
3.32.✨SQL 32——查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
SELECT
score.c_id,
course.c_name,
round(avg(s_score), 2) AS avg_score
FROM
score, course
where score.c_id = course.c_id
GROUP BY
c_id
ORDER BY
avg_score DESC,
c_id ASC
3.33.SQL 33——查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩
SELECT
student.s_id,
student.s_name,
round(avg(s_score), 2) AS '平均成绩'
FROM
student
INNER JOIN score ON student.s_id = score.s_id
GROUP BY
score.s_id,
student.s_id,
student.s_name
HAVING
avg(score.s_score) > 85
3.34.SQL 34——查询课程名称为”数学”,且分数低于 60 的学生姓名和分数
SELECT
s_name,
s_score
FROM
student,
score
WHERE
student.s_id = score.s_id
AND c_id IN (SELECT c_id FROM course WHERE c_name = '数学')
AND s_score 60
3.35.SQL 35——查询所有学生的课程及分数情况
SELECT
student.s_id,
student.s_name,
course.c_name,
score.s_score
FROM
student,
course,
score
WHERE
student.s_id = score.s_id
AND score.c_id = course.c_id
ORDER BY
s_id
3.36.SQL 36——查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数
SELECT
student.s_name,
course.c_name,
score.s_score
FROM
student,
score,
course
WHERE
student.s_id = score.s_id
AND score.c_id = course.c_id
AND s_score > 70
3.37.SQL 37——查询不及格的课程的学生姓名、课程名称和分数
SELECT
student.s_name,
course.c_name,
score.s_score
FROM
student,
score,
course
WHERE
student.s_id = score.s_id
AND score.c_id = course.c_id
AND s_score 60
3.38.SQL 38——查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名
SELECT
student.s_id,
s_name
FROM
student,
score
WHERE
student.s_id = score.s_id
AND c_id = '01'
AND s_score >= 80
3.39.SQL 39——求每门课程的学生人数
SELECT
c_name,
count(s_id) AS '学生人数'
FROM
score,
course
WHERE
score.c_id = course.c_id
GROUP BY
score.c_id
3.40.✨SQL 40——查询选修”张三”老师所授课程的学生中,成绩最高的学生信息及其成绩
# 这里默认的是一门老师只教授一门课程
SELECT
student.*,
score.s_score
FROM
student,
score
WHERE
student.s_id = score.s_id
AND c_id IN (SELECT c_id FROM course WHERE t_id IN (SELECT t_id FROM teacher WHERE t_name = '张三'))
ORDER BY
s_score DESC
LIMIT 1
3.41.SQL 41——查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
SELECT
sc1.s_id,
sc1.c_id,
sc2.c_id,
sc1.s_score,
sc2.s_score
FROM
score AS sc1,
score AS sc2
WHERE
sc1.s_id = sc2.s_id
AND sc1.s_score = sc2.s_score
AND sc1.c_id != sc2.c_id
3.42.✨SQL 42——查询每门课程成绩最好的前两名
SELECT
sc1.c_id,
sc1.s_id,
count(sc2.s_score) + 1 AS rank
FROM
score AS sc1
LEFT JOIN score AS sc2 ON sc1.c_id = sc2.c_id
AND sc1.s_score sc2.s_score
GROUP BY
sc1.c_id,
sc1.s_score,
sc1.s_id
HAVING
count(sc2.s_score) 2
ORDER BY
sc1.c_id,
rank
3.43.SQL 43——统计每门课程的学生选修人数(超过 5 人的课程才统计),要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
SELECT
c_id,
count(*) AS '选修人数'
FROM
score
GROUP BY
c_id
HAVING
count(*) > 5
ORDER BY
'选修人数' DESC,
c_id ASC
3.44.SQL 44——查询至少选修两门课程的学生学号
SELECT s_id FROM score GROUP BY s_id HAVING count(c_id) >= 2
3.45.SQL 45——查询选修了全部课程的学生信息
SELECT
*
FROM
student
WHERE
# SELECT count(*) FROM course) 查询的是总课程的数量
s_id IN (SELECT s_id FROM score GROUP BY s_id HAVING count(c_id) = (SELECT count(*) FROM course))
3.46.✨SQL 46——查询各学生的年龄
# 1.按照年份来计算
SELECT
s_id,
s_name,
(YEAR(now()) - YEAR(s_birth)) AS age
FROM
student
/*
2.使用 timestampdiff()
(1) TIMESTAMPDIFF(): 第一个参数设置时间单位,可以精确到年(YEAR)、天(DAY)、小时(HOUR),分钟(MINUTE)和秒(SECOND)。对于比较
的两个时间,时间小的放在前面,时间大的放在后面。
(3) datediff(): 返回值是相差的天数,无法定位到小时、分钟和秒。
*/
SELECT
s_id,
s_name,
timestampdiff(YEAR, s_birth, now()) AS age
FROM
student
3.47.SQL 47——查询本周过生日的学生
# week(时间): 默认从 0 开始,表示星期天为一个星期的第一天,国外算法
# week(时间, 1): 从 1 开始,表示星期一为一个星期的第一天,国内算法
SELECT
s_id,
s_name
FROM
student
WHERE
WEEK (s_birth) = WEEK (now(), 1)
3.48.SQL 48——查询下周过生日的学生
SELECT
s_id,
s_name
FROM
student
WHERE
WEEK (s_birth) = WEEK (now(), 1) + 1
3.49.SQL 49——查询本月过生日的学生
SELECT
s_id,
s_name
FROM
student
WHERE
MONTH (s_birth) = MONTH (now())
3.50.SQL 50——查询下月过生日的学生
SELECT
s_id,
s_name
FROM
student
WHERE
(MONTH(s_birth) = (((MONTH(NOW()) + 12) % 12) + 1))