• 深夜小酌,50道经典SQL题,真香~


    晚上听说我们村子快解封了,居家办公的日子已不多,久久不能平息~~

    蹲坑之余,在网上找到了50道所谓经典SQL题,这不就是深夜必备小菜?我用脚叼起拖鞋,从冰箱拿出封印已久的半瓶可乐,打开数日未见的MySQL8,来吧,来场说卷就卷的刷题。

    在这里插入图片描述

    现在是6月9号00:15分,花了近3小时撸完这小50题,有点困了,不想对所谓标准答案了。。心中有猛虎,何必细嗅蔷薇?

    我先把自己的结果简单发上来(我会把题目和答案分开便与你刷题),相信和我一样爱玩儿的同学们,可以刷一遍后对照一下我的SQL,会找出我SQL中的问题,我再来复盘更新。整体来说难度中等偏低吧,但是如果你想复习SQL回味当初的你,还是比较推荐的,毕竟回味是福。废话不说了,开整。


    车票


    一、测试表数据

    学生表:student [学号,学生姓名,出生年月,性别]
    成绩表:score [学号,课程号,成绩]
    课程表:course [课程号,课程名称,教师号]
    教师表:teacher [教师号,教师姓名)

    在这里插入图片描述

    下面是表结构和数据,直接执行即可~

    -- ----------------------------
    -- Table structure for teacher
    -- ----------------------------
    DROP TABLE IF EXISTS `teacher`;
    CREATE TABLE `teacher`  (
      `t_id` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
      `t_name` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '',
      PRIMARY KEY (`t_id`) USING BTREE
    ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
    
    -- ----------------------------
    -- Records of teacher
    -- ----------------------------
    INSERT INTO `teacher` VALUES ('01', '数学老师-杰斯');
    INSERT INTO `teacher` VALUES ('02', '语文老师-盲僧');
    INSERT INTO `teacher` VALUES ('03', '英语老师-菲欧娜');
    
    -- ----------------------------
    -- Table structure for student
    -- ----------------------------
    DROP TABLE IF EXISTS `student`;
    CREATE TABLE `student`  (
      `s_id` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
      `s_name` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '',
      `s_birth` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '',
      `s_sex` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '',
      PRIMARY KEY (`s_id`) USING BTREE
    ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
    
    -- ----------------------------
    -- Records of student
    -- ----------------------------
    INSERT INTO `student` VALUES ('01', '流浪法师-瑞兹', '1990-01-01', '男');
    INSERT INTO `student` VALUES ('02', '探险家-EZ', '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 score
    -- ----------------------------
    DROP TABLE IF EXISTS `score`;
    CREATE TABLE `score`  (
      `s_id` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
      `c_id` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
      `s_score` int(0) NULL DEFAULT NULL,
      PRIMARY KEY (`s_id`, `c_id`) USING BTREE
    ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_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 course
    -- ----------------------------
    DROP TABLE IF EXISTS `course`;
    CREATE TABLE `course`  (
      `c_id` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
      `c_name` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '',
      `t_id` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
      PRIMARY KEY (`c_id`) USING BTREE
    ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_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');
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91

    二、50道SQL题(不含答案),用于自测

    好了,请打开你亲爱的navicat,关闭百度、手机等一切阻碍你进步的绊脚石。开始你的表演~

    -- 50道SQL面试题
    -- 1、查询课程编号为“01”的课程比“02”的课程成绩高的所有学生的学号(难)
    
    -- 2、查询平均成绩大于60分的学生的学号和平均成绩
    
    -- 3、查询所有学生的学号、姓名、选课数、总成绩
    
    -- 4、查询姓“猴”的老师的个数
    
    -- 5、查询没学过“数学老师-杰斯”老师课的学生的学号、姓名
    
    -- 6、查询学过“数学老师-杰斯”老师所教的所有课的同学的学号、姓名
    
    -- 7、查询学过编号为“01”的课程并且也学过编号为“02”的课程的学生的学号、姓名
    
    -- 8、查询课程编号为“02”的总成绩
    
    -- 9、查询所有,课程成绩小于60分的学生的学号、姓名
    
    -- 10、查询没有学全所有课的学生的学号、姓名
    
    -- 11、查询至少有一门课与学号为“01”的学生所学课程相同的学生的学号和姓名 (难)
    
    -- 12、查询和“01”号同学所学课程完全相同的其他同学的学号(难)
    
    -- 13、查询没学过"数学老师-杰斯"老师讲授的任一门课程的学生姓名
    
    -- 14、空
    
    -- 15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
    
    -- 16、检索"01"课程分数小于60,按分数降序排列的学生信息
    
    -- 17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩(难)
    
    -- 18、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率
    
    -- 19、查询学生的总成绩并进行排名
    
    -- 20、查询不同老师所教不同课程平均分,从高到低显示
    
    -- 21、查询学生平均成绩及其名次
    
    -- 22、按各科成绩进行排序,并显示排名(难)
    
    -- 23、查询每门功课成绩最好的前两名学生姓名
    
    -- 24、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
    
    -- 25、查询各科成绩前三名的记录(不考虑成绩并列情况)
    
    -- 26、使用分段[100-85],[85-70],[70-60],[<60]来统计各科成绩,分别统计各分数段人数:课程ID和课程名称
    
    -- 27、查询每门课程被选修的学生数
    
    -- 28、查询出只有两门课程的全部学生的学号和姓名
    
    -- 29、查询男生、女生人数
    
    -- 30、查询名字中含有"风"字的学生信息
    
    -- 31、查询1990年出生的学生名单
    
    -- 32、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
    
    -- 33、查询每门课程的平均成绩,结果按平均成绩升序排序,平均成绩相同时,按课程号降序排列
    
    -- 34、查询课程名称为"数学",且分数低于60的学生姓名和分数
    
    -- 35、查询所有学生的课程及分数情况
    
    -- 36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数
    
    -- 37、查询不及格的课程并按课程号从大到小排列
    
    -- 38、查询课程编号为03且课程成绩在80分以上的学生的学号和姓名
    
    -- 39、求每门课程的学生人数
    
    -- 40、查询选修“数学老师-杰斯”老师所授课程的学生中成绩最高的学生姓名及其成绩
    
    -- 41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 (难)
    
    -- 42、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
    
    -- 43、检索至少选修两门课程的学生学号
    
    -- 44、查询选修了全部课程的学生信息
    
    -- 45、查询各学生的年龄
    
    -- 46、查询两门以上不及格课程的同学的学号及其平均成绩
    
    -- 47、查询本月过生日的学生
    
    -- 48、查询下一个月过生日的学生
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96

    三、50道SQL题(含答案),用于参考对照

    – 先全数据关联一下看看

    -- 先全数据关联一下看看
    select * from student stu,score sc,course c,teacher t 
    where stu.s_id=sc.s_id and sc.c_id =c.c_id and c.t_id = t.t_id;
    
    • 1
    • 2
    • 3

    50道SQL面试题开撸

    1、查询课程编号为“01”的课程比“02”的课程成绩高的所有学生的学号(难)

    -- 1、查询课程编号为“01”的课程比“02”的课程成绩高的所有学生的学号(难)
    SELECT a.s_id from 
    (select * from score sc1 where sc1.c_id = '01') a , 
    (select * from score sc2 where sc2.c_id = '02') b 
    where a.s_id = b.s_id and a.s_score > b.s_score;
    
    • 1
    • 2
    • 3
    • 4
    • 5

    – 2、查询平均成绩大于60分的学生的学号和平均成绩

    -- 2、查询平均成绩大于60分的学生的学号和平均成绩
    select a.s_id,a.avg_score from 
    (select stu.s_id,stu.s_name,AVG(sc.s_score) as avg_score from student stu, score sc where stu.s_id = sc.s_id GROUP BY stu.s_id) a 
    where a.avg_score > 60 ORDER BY avg_score desc;  
    
    • 1
    • 2
    • 3
    • 4

    – 3、查询所有学生的学号、姓名、选课数、总成绩

    -- 3、查询所有学生的学号、姓名、选课数、总成绩
    -- 我这里MySQL8执行sql时是出现了sql_mode=only_full_group_by错误,执行下行sql进行配置即可解决;
    -- set @@global.sql_mode = 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'
    select stu.s_id,stu.s_name,count(sc.c_id),IFNULL(SUM(sc.s_score),0) from student stu 
    LEFT JOIN score sc on stu.s_id = sc.s_id GROUP BY stu.s_id;
    
    • 1
    • 2
    • 3
    • 4
    • 5

    – 4、查询姓“猴”的老师的个数

    -- 4、查询姓“猴”的老师的个数
    select count(*) from teacher where t_name like '猴%';
    
    • 1
    • 2

    – 5、查询没学过“数学老师-杰斯”老师课的学生的学号、姓名

    -- 5、查询没学过“数学老师-杰斯”老师课的学生的学号、姓名
    SELECT * from student a where a.s_id not in
     (select stu.s_id from student stu,score sc,course c,teacher t 
    where stu.s_id=sc.s_id and sc.c_id =c.c_id and c.t_id = t.t_id and t.t_name ='数学老师-杰斯')
    
    • 1
    • 2
    • 3
    • 4

    – 6、查询学过“数学老师-杰斯”老师所教的所有课的同学的学号、姓名

    -- 6、查询学过“数学老师-杰斯”老师所教的所有课的同学的学号、姓名
    select stu.s_id,stu.s_name from 
    student stu,score sc,course c,teacher t 
    where stu.s_id=sc.s_id and sc.c_id =c.c_id and c.t_id = t.t_id and t.t_name ='数学老师-杰斯';
    
    • 1
    • 2
    • 3
    • 4

    – 7、查询学过编号为“01”的课程并且也学过编号为“02”的课程的学生的学号、姓名

    -- 7、查询学过编号为“01”的课程并且也学过编号为“02”的课程的学生的学号、姓名
    SELECT a.s_id,a.s_name from 
    (SELECT stu1.s_id,stu1.s_name from student stu1, score sc1 where stu1.s_id = sc1.s_id and sc1.c_id ='01') a,  
    (SELECT stu2.s_id,stu2.s_name from student stu2, score sc2 where stu2.s_id = sc2.s_id and sc2.c_id ='02') b
    where a.s_id = b.s_id;
    
    • 1
    • 2
    • 3
    • 4
    • 5

    – 8、查询课程编号为“02”的总成绩

    -- 8、查询课程编号为“02”的总成绩
    select SUM(sc.s_score) from score sc where c_id = '02';
    
    • 1
    • 2

    – 9、查询所有,课程成绩小于60分的学生的学号、姓名

    -- 9、查询所有,课程成绩小于60分的学生的学号、姓名
    select stu.s_id,stu.s_name from student stu,score sc where stu.s_id = sc.s_id and sc.s_score < 60 GROUP BY stu.s_id;
    
    • 1
    • 2

    – 10、查询没有学全所有课的学生的学号、姓名

    -- 10、查询没有学全所有课的学生的学号、姓名
    SELECT stu2.s_id,stu2.s_name from student stu2 ,
    (SELECT stu.s_id,stu.s_name,IFNULL(COUNT(c.c_id),0) as c_count from student stu 
    LEFT JOIN score sc on stu.s_id = sc.s_id JOIN course c on sc.c_id = c.c_id GROUP BY stu.s_id) a
    where stu2.s_id = a.s_id and a.c_count = (SELECT count(*) from course);
    
    • 1
    • 2
    • 3
    • 4
    • 5

    – 11、查询至少有一门课与学号为“01”的学生所学课程相同的学生的学号和姓名 (难)

    -- 11、查询至少有一门课与学号为“01”的学生所学课程相同的学生的学号和姓名 (难)
    SELECT a.s_id,a.s_name from 
    (SELECT stu1.s_id,stu1.s_name,sc1.c_id from student stu1,score sc1 where stu1.s_id = sc1.s_id) a,
    (SELECT sc2.c_id from student stu2,score sc2 where stu2.s_id = sc2.s_id and stu2.s_id = '01') b
    where a.c_id = b.c_id GROUP BY a.s_id;
    
    • 1
    • 2
    • 3
    • 4
    • 5

    – 12、查询和“01”号同学所学课程完全相同的其他同学的学号(难)

    -- 12、查询和“01”号同学所学课程完全相同的其他同学的学号(难)
    select s_id,s_name from student 
    where s_id in (
    select s_id from score
    where s_id <> '01'
    group by s_id
    having count(*) = (select count(*) from score where s_id = '01'))
    and s_id not in(select distinct s_id from score where c_id not in (select c_id from score where s_id = '01'))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    – 13、查询没学过"数学老师-杰斯"老师讲授的任一门课程的学生姓名

    -- 13、查询没学过"数学老师-杰斯"老师讲授的任一门课程的学生姓名
    SELECT s_name from student stu2 where stu2.s_id not in
    (select DISTINCT stu.s_id from student stu,score sc,course c,teacher t 
    where stu.s_id=sc.s_id and sc.c_id =c.c_id and c.t_id = t.t_id and t.t_name = '数学老师-杰斯') ;
    
    • 1
    • 2
    • 3
    • 4

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

    -- 15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
    SELECT a.s_id, a.s_name, avg from student stu2,
    (SELECT stu.s_id, stu.s_name, count(*) as count, AVG(sc.s_score) as avg from student stu, score sc 
    where stu.s_id = sc.s_id and sc.s_score < 60 GROUP BY stu.s_id) a where stu2.s_id = a.s_id and a.count >= 2;
    
    • 1
    • 2
    • 3
    • 4

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

    -- 16、检索"01"课程分数小于60,按分数降序排列的学生信息
    SELECT stu.s_id, stu.s_name,sc.s_score from student stu, score sc where stu.s_id = sc.s_id and sc.c_id = '01' and sc.s_score < 60 ORDER BY sc.s_score desc;
    
    • 1
    • 2

    – 17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩(难)

    -- 17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩(难)
    -- 全量展示
    SELECT * from 
    (SELECT stu.s_id,stu.s_name,sc.c_id,IFNULL(sc.s_score,0) from student stu LEFT JOIN score sc on stu.s_id = sc.s_id ) a,
    (SELECT sc1.s_id,AVG(sc1.s_score) as avg from score sc1 GROUP BY sc1.s_id) b
    where a.s_id = b.s_id ORDER BY avg desc;
    
    
    -- 横排展示
    select s_id,s_name,
    (select s_score from score where score.s_id=student.s_id and c_id='01') as '语文',
    (select s_score from score where score.s_id=student.s_id and c_id='02') as '数学',
    (select s_score from score where score.s_id=student.s_id and c_id='03') as '英语',
    (select avg(s_score) from score where score.s_id=student.s_id) avg_score
    from student 
    order by avg_score desc;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    – 18、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率

    -- 18、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率
    SELECT c.c_id,c.c_name,MAX(sc.s_score),MIN(sc.s_score),AVG(sc.s_score),CONCAT(round(100 * sum(case when sc.s_score>=60 then 1.0 else 0.0 end)/count(*),2),'%') as '及格率' from score sc, course c where sc.c_id = c.c_id GROUP BY c.c_id;
    
    • 1
    • 2

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

    -- 19、查询学生的总成绩并进行排名
    SELECT stu.s_id,stu.s_name,IFNULL(SUM(sc.s_score),0) as totalCount from student stu LEFT JOIN score sc on stu.s_id = sc.s_id GROUP BY stu.s_id ORDER BY totalCount desc
    
    • 1
    • 2

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

    -- 20、查询不同老师所教不同课程平均分,从高到低显示
    SELECT t.t_name,c.c_name,AVG(sc.s_score) as avg from score sc, course c, teacher t where sc.c_id = c.c_id and c.t_id = t.t_id GROUP BY t.t_id ORDER BY avg desc;
    
    • 1
    • 2

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

    -- 21、查询学生平均成绩及其名次
    SELECT stu.s_id,stu.s_name,AVG(sc.s_score) as avg,rank() over(ORDER BY AVG(sc.s_score) desc) as `no` from student stu LEFT JOIN score sc on stu.s_id = sc.s_id GROUP BY stu.s_id ORDER BY avg desc;
    
    • 1
    • 2

    – 22、按各科成绩进行排序,并显示排名(难)

    -- 22、按各科成绩进行排序,并显示排名(难)
    select c_id,s_id,s_score,rank() over(partition by c_id order by s_score desc) num_row 
    from score;
    
    • 1
    • 2
    • 3

    – 23、查询每门功课成绩最好的前两名学生姓名

    -- 23、查询每门功课成绩最好的前两名学生姓名
    select a.s_id,a.s_name,b.num_row 
    from student a 
    join (select s_id, c_id, row_number() over(partition by c_id order by s_score desc) num_row from score) b on a.s_id=b.s_id 
    where b.num_row<=2;
    
    • 1
    • 2
    • 3
    • 4
    • 5

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

    -- 24、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
    select a.s_id,a.s_name,b.c_id,b.s_score,b.num_row 
    from student a 
    ,(select 
          s_id,
          c_id,
          s_score,
          row_number() over(partition by c_id order by s_score desc) num_row from score) b 
    where a.s_id=b.s_id 
    and b.num_row between 2 and 3;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    – 25、查询各科成绩前三名的记录(不考虑成绩并列情况)

    -- 25、查询各科成绩前三名的记录(不考虑成绩并列情况)
    select a.s_id,a.s_name,b.c_id,b.s_score,b.num_row 
    from student a 
    ,(select 
          s_id,
          c_id,
          s_score,
          row_number() over(partition by c_id order by s_score desc) num_row from score) b 
    where a.s_id=b.s_id 
    and b.num_row between 1 and 3;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    – 26、使用分段[100-85],[85-70],[70-60],[<60]来统计各科成绩,分别统计各分数段人数:课程ID和课程名称

    -- 26、使用分段[100-85],[85-70],[70-60],[<60]来统计各科成绩,分别统计各分数段人数:课程ID和课程名称
    SELECT c.c_id,c.c_name,
    (select count(*) from score sc1 where sc1.c_id=c.c_id and sc1.s_score >= 85) as '[100-85]',
    (select count(*) from score sc2 where sc2.c_id=c.c_id and sc2.s_score BETWEEN 70 and 85) as '[85-70]',
    (select count(*) from score sc3 where sc3.c_id=c.c_id and sc3.s_score BETWEEN 60 and 70) as '[70-60]',
    (select count(*) from score sc4 where sc4.c_id=c.c_id and sc4.s_score < 60) as '[<60]'
    from course c 
    GROUP BY c.c_id;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    – 27、查询每门课程被选修的学生数

    -- 27、查询每门课程被选修的学生数
    select c_id,count(c_id) count from score group by c_id;
    
    • 1
    • 2

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

    -- 28、查询出只有两门课程的全部学生的学号和姓名
    select a.s_id,a.s_name,count(b.s_id) count 
    from student a 
    join score b on a.s_id=b.s_id 
    group by s_id 
    having count=2;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    – 29、查询男生、女生人数

    -- 29、查询男生、女生人数
    select s_sex,count(s_sex) count_sex from student group by s_sex;
    
    • 1
    • 2

    – 30、查询名字中含有"风"字的学生信息

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

    – 31、查询1990年出生的学生名单

    -- 31、查询1990年出生的学生名单
    select s_id,s_name from student where year(s_birth)='1990';
    
    • 1
    • 2

    – 32、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩

    -- 32、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
    select a.s_id,a.s_name,avg(b.s_score) avg
    from student a 
    join score b on a.s_id=b.s_id 
    group by b.s_id 
    having avg>=85;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

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

    -- 33、查询每门课程的平均成绩,结果按平均成绩升序排序,平均成绩相同时,按课程号降序排列
    select c_id, avg(s_score) avg_score from score group by c_id order by avg_score,c_id desc;
    
    • 1
    • 2

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

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

    – 35、查询所有学生的课程及分数情况

    -- 35、查询所有学生的课程及分数情况
    select stu.s_id,stu.s_name,c.c_id,c.c_name,sc.s_score 
    from student stu 
    left join score sc on stu.s_id=sc.s_id 
    left join course c on sc.c_id=c.c_id ;
    
    • 1
    • 2
    • 3
    • 4
    • 5

    – 36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数

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

    – 37、查询不及格的课程并按课程号从大到小排列

    -- 37、查询不及格的课程并按课程号从大到小排列
    select c_id from score where s_score < 60 GROUP BY c_id order by c_id desc;
    
    • 1
    • 2

    – 38、查询课程编号为03且课程成绩在80分以上的学生的学号和姓名

    -- 38、查询课程编号为03且课程成绩在80分以上的学生的学号和姓名
    select stu.s_id,stu.s_name 
    from student stu, score sc where stu.s_id=sc.s_id 
    and sc.s_score>80 and sc.c_id=03;
    
    • 1
    • 2
    • 3
    • 4

    – 39、求每门课程的学生人数

    -- 39、求每门课程的学生人数
    select c_id, count(*) count from score group by c_id;
    
    • 1
    • 2

    – 40、查询选修“数学老师-杰斯”老师所授课程的学生中成绩最高的学生姓名及其成绩

    -- 40、查询选修“数学老师-杰斯”老师所授课程的学生中成绩最高的学生姓名及其成绩
    select stu.s_id,stu.s_name,sc.s_score from
    student stu,score sc,course c,teacher t where stu.s_id=sc.s_id and sc.c_id =c.c_id and c.t_id = t.t_id 
    and t.t_name='数学老师-杰斯' 
    order by sc.s_score desc 
    limit 1;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    – 41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 (难)

    -- 41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 (难)
    select a.s_id,a.c_id,a.s_score 
    from score a, score b where a.s_score=b.s_score 
    and a.c_id <> b.c_id and a.s_id=b.s_id 
    group by a.s_id,a.c_id,a.s_score;
    
    • 1
    • 2
    • 3
    • 4
    • 5

    – 42、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列

    -- 42、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
    select c_id, count(*) count from score group by c_id order by c_id asc;
    
    • 1
    • 2

    – 43、检索至少选修两门课程的学生学号

    -- 43、检索至少选修两门课程的学生学号
    select s_id,count(*) count from score group by s_id having count>=2;
    
    • 1
    • 2

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

    -- 44、查询选修了全部课程的学生信息
    select stu.s_id,stu.s_name,count(*) count from student stu,score sc where stu.s_id = sc.s_id group by sc.s_id having count = 3;
    
    • 1
    • 2

    – 45、查询各学生的年龄

    -- 45、查询各学生的年龄
    SELECT * ,(YEAR(CURDATE()) - YEAR(s_birth)) AS age from student; 
    
    • 1
    • 2

    – 46、查询两门以上不及格课程的同学的学号及其平均成绩

    -- 46、查询两门以上不及格课程的同学的学号及其平均成绩
    select s_id,avg(s_score) avg
    from score 
    where s_score < 60 
    group by s_id 
    having count(*) >= 2;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    – 47、查询本月过生日的学生

    -- 47、查询本月过生日的学生
    select s_id from student where month(s_birth) = month(now());
    
    • 1
    • 2

    – 48、查询下一个月过生日的学生

    -- 48、查询下一个月过生日的学生
    select s_id from student where month(s_birth) = month(now())+1;
    
    • 1
    • 2

    好了,早点休息~~
    如果发现有错误或性能待优化的SQL,记得评论区告诉我,我会让你给我来一脚~~

    在这里插入图片描述

    先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦

  • 相关阅读:
    Linux Framebuffer驱动框架、接口实现和使用
    华为机试 - 判断一组不等式是否满足约束并输出最大差
    [论文工具] LaTeX论文SVG和EPS矢量图转换方法详解
    字节测试总监,让我们用这份《测试用例规范》,再也没加班过
    Elasticsearch-03-JavaApi以及springboot中操作-RestHighLevelClient
    Shiro入门(五)Shiro自定义Realm和加密算法
    软件更新和无线升级 (SOTA)
    VUE学习
    什么是美颜SDK?如何在直播应用中集成直播美颜SDK?
    Spring学习+Spring整合durid+Spring整合Mybatis
  • 原文地址:https://blog.csdn.net/m0_67402914/article/details/126081477