mysql - 经典50题
经典案例50题,确实经典,在我大学的时候,老师就把这些题目,当成课下作业给我们做了;
还有另一套题目,是算部门收入的,也非常经典,如果这些题不够做,可以再找找(建表语句包含employee的);
这些题目包含多个答案,有一些实习生做的,添加了部分语法分析,可能仍有改进空间。
50题大纲
1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩
5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
6、查询"李"姓老师的数量
7、查询学过"张三"老师授课的同学的信息
8、查询没学过"张三"老师授课的同学的信息
9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
11、查询没有学全所有课程的同学的信息
12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
13、查询和"01"号的同学学习的课程完全相同的其他同学的信息
14、查询没学过"张三"老师讲授的任一门课程的学生姓名
15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
16、检索"01"课程分数小于60,按分数降序排列的学生信息
17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
18、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
19、按各科成绩进行排序,并显示排名(实现不完全)
20、查询学生的总成绩并进行排名
21、查询不同老师所教不同课程平均分从高到低显示
22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
24、查询学生平均成绩及其名次
25、查询各科成绩前三名的记录
26、查询每门课程被选修的学生数
27、查询出只有两门课程的全部学生的学号和姓名
28、查询男生、女生人数
29、查询名字中含有"风"字的学生信息
30、查询同名同性学生名单,并统计同名人数
31、查询1990年出生的学生名单
32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
34、查询课程名称为"数学",且分数低于60的学生姓名和分数
35、查询所有学生的课程及分数情况;
36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数
37、查询不及格的课程
38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名
39、求每门课程的学生人数
40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
42、查询每门功成绩最好的前两名
43、统计每门课程的学生选修人数(超过5人的课程才统计);要求输出课程号和选修人数,查询结果按人数降序排列。
44、检索至少选修两门课程的学生学号
45、查询选修了全部课程的学生信息
46、查询各学生的年龄
47、查询本周过生日的学生
48、查询下周过生日的学生
49、查询本月过生日的学生
50、查询下月过生日的学生
建表语句
/*
navicat premium data transfer
source server : localhost
source server type : mysql
source server version : 50744
source host : localhost:3306
source schema : sqlpractice
target server type : mysql
target server version : 50744
file encoding : 65001
date: 17/06/2024 17:24:13
*/
set names utf8mb4;
set foreign_key_checks = 0;
-- ----------------------------
-- table structure for course
-- ----------------------------
drop table if exists `course`;
create table `course` (
`c_id` varchar(20) not null,
`c_name` varchar(20) not null,
`t_id` varchar(20) not null,
primary key (`c_id`) using btree,
index `t_id`(`t_id`) using btree,
constraint `course_ibfk_1` foreign key (`t_id`) references `teacher` (`t_id`) on delete restrict on update restrict
) engine = innodb character set = utf8 collate = utf8_general_ci row_format = dynamic;
-- ----------------------------
-- records of course
-- ----------------------------
insert into `course` values ('01', '语文', '02');
insert into `course` values ('02', '数学', '01');
insert into `course` values ('03', '英语', '03');
-- ----------------------------
-- table structure for score
-- ----------------------------
drop table if exists `score`;
create table `score` (
`s_id` varchar(20) not null,
`c_id` varchar(20) not null,
`s_score` int(3) null default null,
primary key (`s_id`, `c_id`) using btree,
index `c_id`(`c_id`) using btree,
constraint `score_ibfk_1` foreign key (`s_id`) references `student` (`s_id`) on delete restrict on update restrict,
constraint `score_ibfk_2` foreign key (`c_id`) references `course` (`c_id`) on delete restrict on update restrict
) engine = innodb character set = utf8 collate = utf8_general_ci row_format = dynamic;
-- ----------------------------
-- records of score
-- ----------------------------
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);
-- ----------------------------
-- table structure for student
-- ----------------------------
drop table if exists `student`;
create table `student` (
`s_id` varchar(20) not null,
`s_name` varchar(20) not null,
`s_birth` varchar(20) not null,
`s_sex` varchar(10) not null,
primary key (`s_id`) using btree
) engine = innodb character set = utf8 collate = utf8_general_ci row_format = dynamic;
-- ----------------------------
-- records of student
-- ----------------------------
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', '女');
-- ----------------------------
-- table structure for teacher
-- ----------------------------
drop table if exists `teacher`;
create table `teacher` (
`t_id` varchar(20) not null,
`t_name` varchar(20) not null default '',
primary key (`t_id`) using btree
) engine = innodb character set = utf8 collate = utf8_general_ci row_format = dynamic;
-- ----------------------------
-- records of teacher
-- ----------------------------
insert into `teacher` values ('01', '张三');
insert into `teacher` values ('02', '李四');
insert into `teacher` values ('03', '王五');
set foreign_key_checks = 1;
1. 查询“01”课程比“02”课程成绩高的学生的信息及课程分数。
-- 经典语法 join
-- 已知问题:
-- 如果去除 where 条件,总共 48 条记录,远大于任何一张基础表,
-- 生产环境这么查,大概率查不出数据
SELECT
stu.*,
s1.s_score
FROM
student AS stu
INNER JOIN score AS s1 ON stu.s_id = s1.s_id
INNER JOIN score AS s2 ON s1.s_id = s2.s_id
WHERE
s1.s_score > s2.s_score
AND s1.c_id = '01'
AND s2.c_id = '02';
-- 使用子查询嵌套,提前过滤出数据,之后再进行 join
SELECT
stu.*,
s1.s_score
FROM
student AS stu
INNER JOIN (SELECT * FROM score where score.c_id = '01') AS s1 ON stu.s_id = s1.s_id
INNER JOIN (SELECT * FROM score where score.c_id = '02') AS s2 ON s1.s_id = s2.s_id
WHERE
s1.s_score > s2.s_score;
-- 或者这么写
SELECT
stu.*,
s1.c_id,
s1.s_score,
s2.c_id,
s2.s_score
FROM
student AS stu
INNER JOIN score AS s1 ON stu.s_id = s1.s_id AND s1.c_id = '01'
INNER JOIN score AS s2 ON stu.s_id = s2.s_id AND s2.c_id = '02'
WHERE
s1.s_score > s2.s_score
2. 查询“01”课程比“02”课程成绩低的学生的信息及课程分数。(同上)
3. 查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩。
思路: 求平均成绩可用聚合函数,考虑表关联直接分组。
-- 经典语法:group by ... having ...
SELECT
s.s_id,
s.s_name,
AVG(sc.s_score) avg_score
FROM
Student s
JOIN Score sc ON s.s_id = sc.s_id
GROUP BY
s.s_id,
s.s_name
HAVING
avg_score >= 60
-- 使用子查询
SELECT
stu.s_id,
stu.s_name,
avg.avg_score
FROM
student AS stu
INNER JOIN (
SELECT
s_id,
avg(s_score) AS avg_score
FROM
score
GROUP BY
s_id
) AS avg ON stu.s_id = avg.s_id
WHERE avg_score >= 60;
4.查询平均成绩小于 60 分的同学的学生编号和学生姓名和平均成绩(包括有成绩的和无成绩的)。
SELECT
s.s_id,
s.s_name,
AVG(sc.s_score) avg_score
FROM
Student s
LEFT JOIN Score sc ON s.s_id = sc.s_id
GROUP BY
s.s_id,
s.s_name
HAVING
avg_score < 60
OR avg_score IS NULL
-- 如果业务允许,也可以用 ifnull 替换空值
SELECT
s.s_id,
s.s_name,
AVG(IFNULL(sc.s_score, 0)) avg_score
FROM
Student s
LEFT JOIN Score sc ON s.s_id = sc.s_id
GROUP BY
s.s_id,
s.s_name
HAVING
avg_score < 60
5. 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩。(同案例3)
6. 查询“李”姓老师的数量。
-- 基础语法
SELECT COUNT(*)
FROM teacher
WHERE t_name LIKE '李%'
7. 查询学过张三老师授课的同学的信息。
-- 同案例1
SELECT
stu.*
FROM
student AS stu
INNER JOIN score ON stu.s_id = score.s_id
INNER JOIN course ON score.c_id = course.c_id
INNER JOIN teacher ON course.t_id = teacher.t_id
WHERE
t_name = '张三';
8. 找出没有学过张三老师课程的学生
思路: 参考题目7查询出张三老师授课的学生信息将他们排除。
-- 方式 1:经典 not in 写法
-- 已知问题:not in 的子查询本部分,如果结果集太大,会导致内存相关的报错
SELECT
*
FROM
student
where student.s_id not in (
SELECT
sc.s_id,
c.c_name,
t.t_name
FROM
score sc
JOIN course c ON sc.c_id = c.c_id
JOIN teacher t ON c.t_id = t.t_id
AND t_name = '张三'
)
-- 方式 2:用 join 代替 not in
-- 写法通常优于上一句,但是 is null 条件,也用不到索引
select a.* from student a
left join (
SELECT
sc.s_id,
c.c_name,
t.t_name
FROM
score sc
JOIN course c ON sc.c_id = c.c_id
JOIN teacher t ON c.t_id = t.t_id
AND t_name = '张三'
) b on a.s_id = b.s_id
where b.s_id is null
9. 查询学过编号为 01,并且学过编号为 02 课程的学生信息
-- 使用 in 语法
SELECT s_id, s_name
FROM Student
WHERE s_id IN(
SELECT s1.s_id
FROM Score s1
JOIN Score s2
ON s1.s_id = s2.s_id
WHERE s1.c_id = '01' AND s2.c_id = '02'
)
-- 使用 join 语法
SELECT
stu.*,
score1.s_id,
score2.s_id
FROM
student AS stu
LEFT JOIN score AS score1 ON stu.s_id = score1.s_id and score1.c_id = '01'
LEFT JOIN score AS score2 ON stu.s_id = score2.s_id and score2.c_id = '02'
where score1.s_id is not null and score2.s_id is not null;
-- 使用 join 语法(不太好的写法)
SELECT
stu.*
FROM
student AS stu
LEFT JOIN score AS score1 ON stu.s_id = score1.s_id
LEFT JOIN score AS score2 ON score1.s_id = score2.s_id
WHERE
score1.c_id = '01'
AND score2.c_id = '02';
10. 查询学过 01 课程,但是没有学过 02 课程的学生信息
-- 使用 in 语法
SELECT
s.*
FROM
student s
JOIN score sc ON s.s_id = sc.s_id
WHERE
sc.c_id = '01'
AND s.s_id NOT IN (
SELECT
s_id
FROM
score
WHERE
c_id = '02'
)
-- 使用 join 语法
SELECT
stu.*,
score1.s_id,
score2.s_id
FROM
student AS stu
LEFT JOIN score AS score1 ON stu.s_id = score1.s_id and score1.c_id = '01'
LEFT JOIN score AS score2 ON stu.s_id = score2.s_id and score2.c_id = '02'
where score1.s_id is not null and score2.s_id is null;
待续……
疯狂的妞妞 :每一天,做什么都好,不要什么都不做!