表名和字段

– 1.学生表

- Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别

– 2.课程表

- Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号

– 3.教师表

- Teacher(t_id,t_name) –教师编号,教师姓名

– 4.成绩表

- Score(s_id,c_id,s_score) –学生编号,课程编号,分数

  • set sql_mode = '';
  • set sql_mode = 'NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES';

测试数据

建表

  • 学生表

    CREATE TABLE Student(

    s_id VARCHAR(20),

    s_name VARCHAR(20) NOT NULL DEFAULT '',

    s_birth VARCHAR(20) NOT NULL DEFAULT '',

    s_sex VARCHAR(10) NOT NULL DEFAULT '',

    PRIMARY KEY(s_id)

    );

  • 课程表

    CREATE TABLE Course(

    c_id VARCHAR(20),

    c_name VARCHAR(20) NOT NULL DEFAULT '',

    t_id VARCHAR(20) NOT NULL,

    PRIMARY KEY(c_id)

    );

  • 教师表

    CREATE TABLE Teacher(

    t_id VARCHAR(20),

    t_name VARCHAR(20) NOT NULL DEFAULT '',

    PRIMARY KEY(t_id)

    );

  • 成绩表

    CREATE TABLE Score(

    s_id VARCHAR(20),

    c_id VARCHAR(20),

    s_score INT(3),

    PRIMARY KEY(s_id,c_id)

    );

  • 插入学生表测试数据

    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);

    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);

MySQL50题

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

    • 解法1:分别把课程01的成绩和课程02的成绩查询出来之后再比较

      SELECT a.*, d.01_score, d.02_score FROM student a INNER JOIN

      (SELECT b.s_id, b.01_score, c.02_score FROM

      (SELECT s_id, s_score as 01_score FROM score WHERE c_id='01')b INNER JOIN

      (SELECT s_id, s_score as 02_score FROM score WHERE c_id='02')c ON b.s_id=c.s_id

      WHERE b.01_score > c.02_score)d ON a.s_id=d.s_id;
    • 解法2:全部查出来多重条件筛选

      SELECT a.*, b.s_score as 01_score, c.s_score as 02_score FROM student a, score b, score c

      WHERE a.s_id = c.s_id AND

      a.s_id = b.s_id AND

      b.c_id = '01' AND

      c.c_id = '02' AND

      b.s_score > c.s_score;
    • 解法3:

      select a.* ,b.s_score as 01_score,c.s_score as 02_score from

      student a

      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;
  • 2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数

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

    SELECT 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_score >= 60

    ORDER BY avg_score DESC;

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

    SELECT b.s_name, b.s_id, ROUND(SUM(a.s_score)/COUNT(a.c_id),2) as avg_score

    FROM score a JOIN student b ON a.s_id=b.s_id

    GROUP BY a.s_id HAVING avg_score < 60

    UNION SELECT a.s_name, a.s_id, 0 as avg_score FROM student a WHERE a.s_id NOT in (SELECT DISTINCT s_id FROM score);

  • 5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩

    SELECT a.s_id, a.s_name, COUNT(b.c_id)as sum_course, SUM(b.s_score) as sum_score FROM student a JOIN score b on a.s_id=b.s_id GROUP BY a.s_id,a.s_name

    UNION SELECT a.s_id, a.s_name, 0 as sum_sourse, 0 as sum_score FROM student a WHERE a.s_id NOT in (SELECT DISTINCT s_id FROM score);

  • 6、查询"李"姓老师的数量

    SELECT COUNT(t_id) as '李姓老师数量' FROM teacher WHERE t_name LIKE "李%" GROUP BY t_id;

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

    SELECT a.* FROM student a, score b, teacher c, course d

    WHERE a.s_id=b.s_id AND

    b.c_id = d.c_id AND

    c.t_id = d.t_id AND

    c.t_name = '张三';

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

    SELECT a.* FROM student a WHERE a.s_id NOT in (SELECT a.s_id FROM student a, score b, teacher c, course d

    WHERE a.s_id=b.s_id AND

    b.c_id = d.c_id AND

    c.t_id = d.t_id AND

    c.t_name = '张三');

  • 9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息

    SELECT a.* FROM student a JOIN

    (SELECT a.s_id FROM (SELECT s_id FROM score WHERE c_id='01')a INNER JOIN (SELECT s_id FROM score WHERE c_id='02')b on a.s_id=b.s_id)d on a.s_id=d.s_id;

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

    SELECT c.* FROM student c JOIN

    (SELECT a.s_id FROM score a WHERE a.c_id='01' AND a.s_id NOT in (SELECT s_id FROM score WHERE c_id='02'))d ON c.s_id=d.s_id;

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

    SELECT a.*, COUNT(b.c_id) as sum_course FROM student a JOIN score b ON a.s_id=b.s_id GROUP BY a.s_id HAVING sum_course < 3;

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

    SELECT DISTINCT a.* FROM student a JOIN score b ON a.s_id=b.s_id WHERE b.c_id in

    (SELECT c.c_id FROM score c WHERE c.s_id='01');

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

    • GROUP_CONCAT(expr) 配合 GROUP BY 可以将同一个分组中产生的值连接起来
  • SELECT c.*, d.course FROM student c JOIN

    (SELECT a.s_id, a.course FROM

    (SELECT s_id, GROUP_CONCAT(c_id) as course FROM score GROUP BY s_id)a JOIN (SELECT s_id, GROUP_CONCAT(c_id) as course FROM score GROUP BY s_id HAVING s_id='01')b

    ON a.course=b.course)d ON c.s_id=d.s_id WHERE c.s_id not in (1);

  • 14、查询各科成绩最高分、最低分和平均分: 以如下形式显示:

  • 课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率

  • 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列

    SELECT a.c_id, b.c_name, MAX(a.s_score) as max_score, MIN(a.s_score) as min_score, ROUND(AVG(a.s_score), 2) as avg_score, COUNT(a.s_id)as count_stu,

    CONCAT(ROUND(SUM(CASE when a.s_score >= 60 THEN 1 ELSE 0 END)/COUNT(), 2)100, '%') as '及格率',

    CONCAT(ROUND(SUM(CASE when a.s_score >= 70 and a.s_score < 80 THEN 1 ELSE 0 END)/COUNT(), 2)100, '%') as '中等率',

    CONCAT(ROUND(SUM(CASE when a.s_score >= 80 and a.s_score < 80 THEN 1 ELSE 0 END)/COUNT(),2)100, '%') as '优良率',

    CONCAT(ROUND(SUM(CASE when a.s_score >= 90 THEN 1 ELSE 0 END)/COUNT(),2)100, '%') as '优秀率'

    FROM score a JOIN course b ON a.c_id=b.c_id GROUP BY a.c_id ORDER BY count_stu DESC, a.c_id ASC;

  • 15、按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺

  • 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次

    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 GROUP BY s_id,c_id,s_score ORDER BY s_score DESC

    )a,(select @k:=0,@i:=0,@score:=0)s;

  • 16、查询学生的总成绩,并进行排名,总分重复时保留名次空缺

  • 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺

    SELECT a.s_id, a.sum_score,

    @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)a, (SELECT @k:=0, @i:=0, @score:=0)s;

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

    SELECT score.c_id, c_name,

    CONCAT(ROUND(sum(CASE WHEN s_score <=100 and s_score>85 THEN 1 ELSE 0 END)/COUNT(),2)100,'%') as '[100-85]',

    CONCAT(ROUND(sum(CASE WHEN s_score <=85 and s_score>70 THEN 1 ELSE 0 END)/COUNT(),2)100,'%') as '[85-70]',

    CONCAT(ROUND(sum(CASE WHEN s_score <=70 and s_score>60 THEN 1 ELSE 0 END)/COUNT(),2)100,'%') as '[70-60]',

    CONCAT(ROUND(sum(CASE WHEN s_score <=60 and s_score>=0 THEN 1 ELSE 0 END)/COUNT(),2)100,'%') as '[60-0]'

    FROM score JOIN course ON score.c_id=course.c_id GROUP BY score.c_id;

  • 18、查询各科成绩前三名的记录

    (SELECT c_id, s_score FROM score WHERE c_id = '01' ORDER BY s_score DESC LIMIT 3)

    UNION (SELECT c_id, s_score FROM score WHERE c_id = '02' ORDER BY s_score DESC LIMIT 3)

    UNION (SELECT c_id, s_score FROM score WHERE c_id = '03' ORDER BY s_score DESC LIMIT 3);

  • 19、查询每门课程被选修的学生数

    SELECT c_id, COUNT(s_id)as sum_stu FROM score GROUP BY c_id;

  • 20、查询出只选修两门课程的学生学号和姓名

    SELECT a.s_id, b.s_name, COUNT(a.c_id)as sum_course FROM score a JOIN student b ON a.s_id=b.s_id GROUP BY a.s_id HAVING sum_course = 2;

  • 21、查询男生、女生人数

    SELECT s_sex, COUNT(s_id)as sex_count FROM student GROUP BY s_sex;

  • 22、查询名字中含有「风」字的学生信息

    SELECT * FROM student WHERE s_name LIKE '%风%';

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

    SELECT s_name,s_sex, count(*)as '同名人数'

    FROM student

    GROUP BY s_name,s_sex

    HAVING COUNT(*) >1;

  • 24、查询 1990 年出生的学生名单

    SELECT * FROM student WHERE YEAR(s_birth)='1990';

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

    SELECT c_id, ROUND(AVG(s_score),2)as avg_score FROM score GROUP BY c_id ORDER BY avg_score DESC, c_id;

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

    SELECT a.s_id,b.s_name, ROUND(SUM(a.s_score)/COUNT(a.c_id), 2) as avg_score FROM score a JOIN student b on a.s_id=b.s_id GROUP BY a.s_id ORDER BY avg_score DESC

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

    SELECT a.s_id, b.c_name,c.s_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 b.c_name='数学' AND a.s_score < 60;

  • 28、查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况)

    SELECT * FROM score

  • 29、查询任何一门课程成绩在 70 分以上的学生姓名、课程名称和分数

    • 取反,先把所有课程成绩在70分以下的学生id找出来(总课程计数和70以下课程计数一样的就是全部课程成绩在70分以下的),取反就是任何一门课程成绩在70分以上的同学

      SELECT d.s_name, f.c_name, e.s_score FROM student d JOIN score e ON d.s_id=e.s_id JOIN course f ON e.c_id = f.c_id WHERE d.s_id NOT in

      (SELECT a.s_id FROM

      (SELECT s_id, COUNT(c_id)as course_count FROM score GROUP BY s_id)a JOIN

      (SELECT s_id, COUNT(c_id)as course_count FROM score WHERE s_score < 70 GROUP BY s_id)b ON (a.course_count=b.course_count and a.s_id=b.s_id));
  • 30、查询不及格的课程

    SELECT a.s_id,b.s_name, c.c_name, a.s_score FROM score a JOIN student b ON a.s_id=b.s_id JOIN course c ON a.c_id=c.c_id WHERE s_score < 60;

  • 31、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名

    SELECT a.s_id, b.s_name, a.c_id,a.s_score FROM score a JOIN student b ON a.s_id=b.s_id WHERE a.c_id='01' AND a.s_score >= 80;

  • 32、求每门课程的学生人数

    SELECT c_id, COUNT(s_id) as count_stu FROM score GROUP BY c_id;

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

    SELECT d.*, b.c_name, c.s_score FROM teacher a

    JOIN course b ON a.t_id=b.t_id

    JOIN score c ON b.c_id=c.c_id

    JOIN student d ON c.s_id=d.s_id

    WHERE a.t_name='张三' ORDER BY c.s_score DESC LIMIT 1;

  • select a.*,b.s_score,b.c_id,c.c_name 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.c_id =(select c_id from course c,teacher d where c.t_id=d.t_id and d.t_name='张三')

    and b.s_score in (select MAX(s_score) from score where c_id='02')

  • 34、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩

    select DISTINCT b.s_id,b.c_id,b.s_score from score a,score b where a.c_id != b.c_id and a.s_score = b.s_score;

  • 35、查询每门功成绩最好的前两名

  • 查询两张score表,找出b表中比a表中单科成绩高的计数(双循环比较 O(n^2)),小于等于2那就是找到了前两位

    select a.s_id,a.c_id,a.s_score from score a

    where (select COUNT(1) from score b where b.c_id=a.c_id and b.s_score>=a.s_score)<=2 ORDER BY a.c_id;

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

    SELECT c_id, COUNT(s_id)as stu_count FROM score GROUP BY c_id HAVING stu_count > 5 ORDER BY stu_count DESC, c_id;

  • 37、检索至少选修两门课程的学生学号

    SELECT s_id, COUNT(c_id)as course_count FROM score GROUP BY s_id HAVING course_count >= 2;

  • 38、查询选修了全部课程的学生信息

    SELECT b.*,COUNT(a.c_id) as course_count FROM score a JOIN student b ON a.s_id=b.s_id GROUP BY a.s_id HAVING course_count=(SELECT COUNT(c_id) FROM course);

  • 39、查询各学生的年龄

    • 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一
  • SELECT s_name, s_birth, (DATE_FORMAT(CURDATE(),'%Y')-DATE_FORMAT(s_birth,'%Y') -

    (CASE WHEN DATE_FORMAT(CURDATE(),'%m%d') > DATE_FORMAT(s_birth,'%m%d')THEN 0 ELSE 1 END ))as age FROM student;

  • 40、查询本周过生日的学生

    SELECT s_name FROM student WHERE WEEK(CURDATE()) = WEEK(s_birth);

  • 41、查询下周过生日的学生

    SELECT s_name FROM student WHERE WEEK(CURDATE())+1 = WEEK(s_birth);

  • 42、查询本月过生日的学生

    SELECT s_name FROM student WHERE MONTH(CURDATE()) = MONTH(s_birth)

  • 43、查询本月过生日的学生

    SELECT s_name FROM student WHERE MONTH(CURDATE())+1 = MONTH(s_birth)

MySQL 50题练习的更多相关文章

  1. MySQL练习50题

    介绍一个学习SQL的网站:https://sqlbolt.com/ 习题来源于网络,SQL语句是自己的练习答案,部分参考了网络上的答案. 花了一晚上的时间做完,个人认为其中的难点有:分组提取前几名的数 ...

  2. sql语句练习50题(Mysql版-详加注释)

    表名和字段 1.学生表       Student(s_id,s_name,s_birth,s_sex) --学生编号,学生姓名, 出生年月,学生性别 2.课程表       Course(c_id, ...

  3. MYSQL 50 基础题 (转载)

    MYSQL 50 基础题 (转载) 前言:最近在强化MYSQL 能力 答案在(也是转载处) https://www.cnblogs.com/kangxinxin/p/11585935.html 下面是 ...

  4. POJ推荐50题

    此文来自北京邮电大学ACM-ICPC集训队 此50题在本博客均有代码,可以在左侧的搜索框中搜索题号查看代码. 以下是原文: POJ推荐50题1.标记“难”和“稍难”的题目可以看看,思考一下,不做要求, ...

  5. JAVA经典算法50题(转)

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/51097928 JAVA经典算法50题 [程序1]   题目:古典问题:有一对兔子, ...

  6. 剑指offer 面试50题

    面试50题: 题目:第一个只出现一次的字符 题:在一个字符串(1<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置. 解题思路一:利用Python特 ...

  7. Java经典逻辑编程50题

    Java经典逻辑编程50题 2016-11-03 09:29:28      0个评论    来源:Alias_fa的博客    收藏   我要投稿 [程序1] 題目:古典问题:有一对兔子,从出生后第 ...

  8. sql 经典查询50题 思路(一)

    因为需要提高一下sql的查询能力,当然最快的方式就是做一些实际的题目了.选择了这个sql的50题,这次大概做了前10题左右,把思路放上来,也是一个总结. 具体题目见: https://zhuanlan ...

  9. 转:sql 经典50题--可能是你见过的最全解析

    题记:从知乎上看到的一篇文章,刚好最近工作中发现遇到的题目与这个几乎一样,可能就是从这里来的吧.^_^ 里面的答案没有细看,SQL求解重在思路,很多时候同一种结果可能有多种写法,比如题中的各科成绩取前 ...

随机推荐

  1. SpringCloud服务的注册发现--------zookeeper实现服务与发现 + Ribbon实现客户端负载均衡

    1,Eureka 闭源了,但是我们可以通过zookeeper实现注册中心的功能. zookeeper 是一个分布式协调工具,可以实现服务的注册和发现,配置中心,注册中心,消息中间件的功能 2,工具准备 ...

  2. iOS 优化实例

    一.接口请求优化 在工程项目中,多个一级界面包含状态,如:服务入口的动态配置,未读消息数量,图片文字等,因此产品设计要每次切换 tab 时都请求数据,及时的更新页面状态.在实际开发中,频繁的调用接口, ...

  3. IdentityServer 部署踩坑记

    IdentityServer 部署踩坑记 Intro 周末终于部署了 IdentityServer 以及 IdentityServerAdmin 项目,踩了几个坑,在此记录分享一下. 部署架构 项目是 ...

  4. 万物互联—MQTT协议

    一.简介 MQTT(Message Queuing Telemetry Transport,消息队列遥测传输),最早是1999年由IBM开发的基于发布/订阅范式的消息协议,是一种极其简单和轻量级的消息 ...

  5. flex布局取消子元素(img、div等)缩放:

    取消子元素(img.div等)缩放: 父元素: display: flex ; 子元素: flex-shrink: 0;

  6. 转载:Docker源码分析(一):Docker架构

    原文地址: http://www.infoq.com/cn/articles/docker-source-code-analysis-part1  作者:孙宏亮 1 背景 1.1 Docker简介 D ...

  7. Elasticsearch 核心术语概念

    Elasticsearch 相当于一个关系型数据库 索引 index 类型 type 文档 document 字段 fields 跟关系型数据库对比 Elasticsearch 相当于一个数据库 索引 ...

  8. Python语言上机题实现方法(持续更新...)

    Python语言上机题实现方法(持续更新...) 1.[字符串循环左移]给定一个字符串S,要求把S的前k个字符移动到S的尾部,如把字符串"abcdef"前面的2个字符'a'.'b' ...

  9. RHCS概述

    RHCS概述 创建RHCS集群环境 创建高可用Apache服务 1 创建RHCS集群环境 1.1 问题 准备四台KVM虚拟机,其三台作为集群节点,一台安装luci并配置iSCSI存储服务,实现如下功能 ...

  10. PAT 链表倒序的算法优化

    之前的答案错误问题已经解决了,现在还有运行超时的问题,先贴上之前的代码 1 #include <iostream> 2 #include <string.h> 3 using ...