目录
- 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-80,优良为 80-90,优秀为 >= 90。
- 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 表中的字段SELECTstudent.*,score1.s_score FROMstudent,score AS score1,score AS score2 WHEREstudent.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"课程成绩低的学生的信息及课程分数
SELECTstudent.*,score1.s_score FROMstudent,score AS score1,score AS score2 WHEREstudent.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.创建临时表 ssEXPLAIN SELECTstudent.s_id,student.s_name,ss.avg_score FROMstudent,(SELECT s_id, AVG(s_score) AS avg_score FROM score GROUP BY s_id) AS ss WHEREstudent.s_id = ss.s_id AND ss.avg_score >= 60;# 2.先进行内连接,然后再分组SELECTstudent.s_id,s_name,round(AVG(score.s_score), 2) as avg_scoreFROMstudentINNER JOIN score ON student.s_id = score.s_id GROUP BYstudent.s_id,s_name HAVINGAVG(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。SELECTstudent.s_id,s_name,round(AVG(score.s_score), 2) as avg_scoreFROMstudentLEFT OUTER JOIN score ON student.s_id = score.s_id GROUP BYstudent.s_id,s_name HAVINGAVG(IFNULL(score.s_score,0)) < 60
3.5.SQL 05——查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
SELECTstudent.s_id,student.s_name,COUNT(DISTINCT c_id) AS totalCourses,SUM(s_score) AS totalScores FROMstudent# 由于要查询所有的学生,故无论其是否有课程信息都要查询,所以使用 LEFT OUTER JOINLEFT OUTER JOIN score ON student.s_id = score.s_id GROUP BYstudent.s_id,student.s_name;
3.6.SQL 06——查询"李"姓老师的数量
# 1.模糊查询SELECTCOUNT(*) FROMteacher WHEREt_name LIKE '李%'# 2.正则表达式查询,字符 '^' 匹配以特定字符或者字符串开头的文本SELECTcount(*) FROMteacher WHEREt_name REGEXP '^李'
3.7.✨SQL 07——查询学过"张三"老师授课的同学的信息
# 1.使用多表连接(score, course, teacher)找到上张三老师课的同学的 s_id,然后再根据 s_id 从 student 表中查询同学信息SELECTstudent.* FROMstudent WHEREs_id IN (SELECTs_id FROMscore,course,teacher WHEREteacher.t_name = '张三' AND teacher.t_id = course.t_id AND course.c_id = score.c_id )# 2.多层嵌套子查询(当数据量较大时,一般不推荐使用子查询)# 在 student 表中根据上过张三老师教的课的学生 s_id 来查询他们的信息SELECTstudent.* FROMstudent WHEREstudent.s_id IN (# 在 score 表中根据张三老师教的课程 c_id 来查找上这些课的学生 s_idSELECT DISTINCTs_id FROMscore WHEREscore.c_id IN (# 在 course 表中根据张三老师的 t_id 查询他所教的课程 c_idSELECT c_id FROM course WHERE course.t_id = (# 在 teacher 表中查询张三老师的 t_idSELECT t_id FROM teacher WHERE t_name = '张三')))
3.8.SQL 08——查询没学过"张三"老师授课的同学的信息
SELECTstudent.* FROMstudent WHEREstudent.s_id NOT IN (SELECT DISTINCTs_id FROMscore WHEREscore.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"的课程的同学的信息
SELECTstudent.* FROMstudent WHEREstudent.s_id IN (SELECTs1.s_id FROMscore AS s1,score AS s2 WHEREs1.s_id = s2.s_id AND s1.c_id = '01' AND s2.c_id = '02' )
3.10.✨SQL 10——查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
SELECTstu.s_id,stu.s_name,stu.s_birth,stu.s_sex FROMstudent AS stuJOIN score AS sc ON stu.s_id = sc.s_idJOIN course AS co ON co.c_id = sc.c_id WHEREco.c_id = '01' AND stu.s_id NOT IN (# 查询学过编号为 "02" 的课程的同学 idSELECTstu.s_idFROMstudent AS stuJOIN score AS sc ON stu.s_id = sc.s_idJOIN course AS co ON co.c_id = sc.c_id WHEREco.c_id = '02' )
3.11.SQL 11——查询没有学全所有课程的同学的信息
# 下面的课程数量 3 也可以用 (SELECT count(*) FROM course) 来代替SELECT* FROMstudent WHEREs_id IN (SELECT s_id FROM score GROUP BY s_id HAVING count(c_id) < 3)
3.12.SQL 12——查询至少有一门课与学号为"01"的同学所学相同的同学的信息
# 不包括学号为 '01' 学生自己SELECT* FROMstudent WHEREs_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* FROMstudent WHEREs_id IN (SELECTs_id FROMscore WHERE# 保证学习的课程相同c_id IN (SELECT DISTINCT c_id FROM score WHERE s_id = '01') AND s_id != '01' GROUP BYs_id HAVING# 保证学习的课程数量相同count(c_id) = (select count(*) from score where s_id = '01'))
3.14.SQL 14——查询没学过"张三"老师讲授的任一门课程的学生姓名
SELECTs_name FROMstudent WHEREs_id NOT IN (# 查询学习过"张三"老师讲授的任一门课程的学生 idSELECTs_id FROMscore WHEREc_id IN ( # 查询由姓名为张三的老师所讲授的课程 idSELECTc_id FROMcourse WHEREt_id IN (SELECT t_id FROM teacher WHERE t_name = '张三')))
3.15.✨SQL 15——查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
SELECTstu.s_id,stu.s_name,tmp_t.avg_score FROMstudent AS stuINNER 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,按分数降序排列的学生信息
SELECTstu.* FROMstudent AS stuINNER JOIN score ON stu.s_id = score.s_id WHEREc_id = '01' AND s_score < 60 ORDER BYs_score DESC
3.17.SQL 17——按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
SELECTs_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 FROMscore GROUP BYs_id ORDER BYavg_score DESC
3.18.✨SQL 18——查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程名称,最高分,最低分,平均分,及格率,中等率,优良率,优秀率。及格为 >= 60,中等为 70-80,优良为 80-90,优秀为 >= 90。
SELECTsc.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 70 AND 80, 1, 0)) / COUNT(*) as '中等率',SUM(IF (sc.s_score BETWEEN 80 AND 90, 1, 0)) / COUNT(*) as '优良率',SUM(IF (sc.s_score >= 90, 1, 0)) / COUNT(*) as '优秀率' FROMscore AS scJOIN course AS c ON sc.c_id = c.c_id GROUP BYsc.c_id
3.19.✨SQL 19——按各科成绩进行排序,并显示排名,成绩重复时合并名次
SELECTsc1.c_id,sc1.s_id,sc1.s_score,count(sc2.s_score) + 1 AS rank FROMscore AS sc1 LEFT JOIN score AS sc2 ON sc1.s_score < sc2.s_score AND sc1.c_id = sc2.c_id GROUP BYsc1.c_id,sc1.s_id,sc1.s_score ORDER BYsc1.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 rankFROM 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_idORDER BY total_score DESC;
3.21.SQL 21——查询不同老师所教不同课程平均分,并从高到低显示
SELECTteacher.t_id,t_name,round(avg(s_score), 2) AS avg_score FROMteacher,course,score WHEREteacher.t_id = course.t_id AND course.c_id = score.c_id GROUP BYteacher.t_id,t_name,score.c_id ORDER BYavg(score.s_score) DESC
3.22.SQL 22——查询所有课程的成绩第 2 名到第 3 名的学生信息及该课程成绩
# 1.分别对每门课程进行查询,然后再合并查询结果,但是如果课程太多,该方法就不太合适SELECTt1.* FROM(SELECTst.*,c.c_id,c.c_name,sc.s_score FROMstudent stLEFT JOIN score sc ON sc.s_id = st.s_idINNER JOIN course c ON c.c_id = sc.c_id AND c.c_id = "01" ORDER BYsc.s_score DESC LIMIT 1,2 ) as t1UNION ALLSELECTt2.* FROM(SELECTst.*,c.c_id,c.c_name,sc.s_score FROMstudent stLEFT JOIN score sc ON sc.s_id = st.s_idINNER JOIN course c ON c.c_id = sc.c_id AND c.c_id = "02" ORDER BYsc.s_score DESC LIMIT 1,2 ) as t2UNION ALLSELECTt3.* FROM(SELECTst.*,c.c_id,c.c_name,sc.s_score FROMstudent stLEFT JOIN score sc ON sc.s_id = st.s_idINNER JOIN course c ON c.c_id = sc.c_id AND c.c_id = "03" ORDER BYsc.s_score DESC LIMIT 1,2 ) as t3# 2.一次性查询,需要注意的是 row_number() 在 MySQL 8.0 中才支持SELECTc_id,student.*,s_score FROMstudentINNER 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 WHEREtmp_t.rank IN (2, 3)
3.23.✨SQL 23——统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
SELECTscore.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]人数所占百分比' FROMscore LEFT JOIN course ON score.c_id = course.c_id GROUP BYscore.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 rankFROM student as stu INNER JOIN score as sc ON stu.s_id = sc.s_idGROUP BY stu.s_id, stu.s_nameORDER 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 DISTINCTtmp_t.c_id,tmp_t.s_score FROM(SELECT DISTINCTstudent.*,sc1.c_id,sc1.s_score,count(DISTINCT sc2.s_score) + 1 AS rank FROMscore AS sc1LEFT JOIN score AS sc2 ON sc1.c_id = sc2.c_id AND sc1.s_score < sc2.s_scoreLEFT JOIN student ON sc1.s_id = student.s_id GROUP BYsc1.c_id,sc1.s_id ORDER BYsc1.c_id,sc1.s_score DESC ) AS tmp_t WHEREtmp_t.rank BETWEEN 1 AND 3
3.26.SQL 26——查询每门课程被选修的学生数
SELECTc_id,count( s_id ) AS '选修该门课程的学生数' FROMscore GROUP BYc_id
3.27.SQL 27——查询出只有两门课程的全部学生的学号和姓名
SELECTstudent.s_id,student.s_name FROMstudent,score WHEREstudent.s_id = score.s_id GROUP BYs_id HAVINGcount(c_id) = 2
3.28.SQL 28——查询男生、女生人数
SELECTsum(CASE WHEN s_sex = '男' THEN 1 ELSE NULL END) AS '男生人数',sum(CASE WHEN s_sex = '女' THEN 1 ELSE NULL END) AS '女生人数' FROMstudent
3.29.SQL 29——查询名字中含有"风"字的学生信息
# 1.使用模糊匹配SELECT* FROMstudent WHEREs_name LIKE '%风%'# 2.使用正则表达式SELECT* FROMstudent WHEREs_name REGEXP '风'
在 MySQL 中,LIKE 操作符用于在文本字段中搜索特定的模式。如果需要在文本字段中匹配通配符本身,可以使用反斜杠字符转义通配符。例如,如果要在一个名为 ’mytable’ 的表中查找包含下划线字符的字符串,可以使用以下查询:
SELECT * FROM mytable WHERE mycolumn LIKE '%\_%' ESCAPE '\';
在上面的查询中,ESCAPE 关键字指定了转义字符为反斜杠,因此我们在通配符前添加了一个反斜杠字符。这将告诉 MySQL 仅匹配下划线字符本身,而不是作为通配符进行匹配。
3.30.✨SQL 30——查询同姓名同性别学生名单,并统计同名人数
SELECTstu1.s_name,tmp_t.cnt AS '同名人数' FROMstudent AS stu1LEFT 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 WHEREtmp_t.cnt > 1
3.31.SQL 31——查询 1990 年出生的学生名单
# 1.使用模糊匹配SELECT* FROMstudent WHEREs_birth LIKE '1990%'# 2.使用正则表达式SELECT* FROMstudent WHEREs_birth REGEXP '^1990'
3.32.✨SQL 32——查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
SELECTscore.c_id,course.c_name,round(avg(s_score), 2) AS avg_score FROMscore, coursewhere score.c_id = course.c_idGROUP BYc_idORDER BYavg_score DESC,c_id ASC
3.33.SQL 33——查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩
SELECTstudent.s_id,student.s_name,round(avg(s_score), 2) AS '平均成绩' FROMstudentINNER JOIN score ON student.s_id = score.s_id GROUP BYscore.s_id,student.s_id,student.s_name HAVINGavg(score.s_score) > 85
3.34.SQL 34——查询课程名称为"数学",且分数低于 60 的学生姓名和分数
SELECTs_name,s_score FROMstudent,score WHEREstudent.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——查询所有学生的课程及分数情况
SELECTstudent.s_id,student.s_name,course.c_name,score.s_score FROMstudent,course,score WHEREstudent.s_id = score.s_id AND score.c_id = course.c_id ORDER BYs_id
3.36.SQL 36——查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数
SELECTstudent.s_name,course.c_name,score.s_score FROMstudent,score,course WHEREstudent.s_id = score.s_id AND score.c_id = course.c_id AND s_score > 70
3.37.SQL 37——查询不及格的课程的学生姓名、课程名称和分数
SELECTstudent.s_name,course.c_name,score.s_score FROMstudent,score,course WHEREstudent.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——求每门课程的学生人数
SELECTc_name,count(s_id) AS '学生人数' FROMscore,course WHEREscore.c_id = course.c_id GROUP BYscore.c_id
3.40.✨SQL 40——查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
# 这里默认的是一门老师只教授一门课程SELECTstudent.*,score.s_score FROMstudent,score WHEREstudent.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 BYs_score DESC LIMIT 1
3.41.SQL 41——查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
SELECTsc1.s_id,sc1.c_id,sc2.c_id,sc1.s_score,sc2.s_score FROMscore AS sc1,score AS sc2 WHEREsc1.s_id = sc2.s_id AND sc1.s_score = sc2.s_score AND sc1.c_id != sc2.c_id
3.42.✨SQL 42——查询每门课程成绩最好的前两名
SELECTsc1.c_id,sc1.s_id,count(sc2.s_score) + 1 AS rank FROMscore AS sc1LEFT JOIN score AS sc2 ON sc1.c_id = sc2.c_id AND sc1.s_score < sc2.s_score GROUP BYsc1.c_id,sc1.s_score,sc1.s_id HAVINGcount(sc2.s_score) < 2 ORDER BYsc1.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* FROMstudent 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.按照年份来计算SELECTs_id,s_name,(YEAR(now()) - YEAR(s_birth)) AS age FROMstudentSELECTs_id,s_name,timestampdiff(YEAR, s_birth, now()) AS age FROMstudent
3.47.SQL 47——查询本周过生日的学生
# week(时间): 默认从 0 开始,表示星期天为一个星期的第一天,国外算法# week(时间, 1): 从 1 开始,表示星期一为一个星期的第一天,国内算法SELECTs_id,s_name FROMstudent WHEREWEEK (s_birth) = WEEK (now(), 1)
3.48.SQL 48——查询下周过生日的学生
SELECTs_id,s_name FROMstudent WHEREWEEK (s_birth) = WEEK (now(), 1) + 1
3.49.SQL 49——查询本月过生日的学生
SELECTs_id,s_name FROMstudent WHEREMONTH (s_birth) = MONTH (now())
3.50.SQL 50——查询下月过生日的学生
SELECTs_id,s_name FROMstudent WHEREMONTH (s_birth) = MONTH (now()) + 1
来源地址:https://blog.csdn.net/weixin_43004044/article/details/126557968