MySQL 50题练习
表名和字段
– 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 TABLEStudent
(
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 TABLECourse
(
c_id
VARCHAR(20),
c_name
VARCHAR(20) NOT NULL DEFAULT '',
t_id
VARCHAR(20) NOT NULL,
PRIMARY KEY(c_id
)
);教师表
CREATE TABLETeacher
(
t_id
VARCHAR(20),
t_name
VARCHAR(20) NOT NULL DEFAULT '',
PRIMARY KEY(t_id
)
);成绩表
CREATE TABLEScore
(
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;
- 解法1:分别把课程01的成绩和课程02的成绩查询出来之后再比较
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 DESC27、查询课程名称为「数学」,且分数低于 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 score29、查询任何一门课程成绩在 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));
- 取反,先把所有课程成绩在70分以下的学生id找出来(总课程计数和70以下课程计数一样的就是全部课程成绩在70分以下的),取反就是任何一门课程成绩在70分以上的同学
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题练习的更多相关文章
- MySQL练习50题
介绍一个学习SQL的网站:https://sqlbolt.com/ 习题来源于网络,SQL语句是自己的练习答案,部分参考了网络上的答案. 花了一晚上的时间做完,个人认为其中的难点有:分组提取前几名的数 ...
- sql语句练习50题(Mysql版-详加注释)
表名和字段 1.学生表 Student(s_id,s_name,s_birth,s_sex) --学生编号,学生姓名, 出生年月,学生性别 2.课程表 Course(c_id, ...
- MYSQL 50 基础题 (转载)
MYSQL 50 基础题 (转载) 前言:最近在强化MYSQL 能力 答案在(也是转载处) https://www.cnblogs.com/kangxinxin/p/11585935.html 下面是 ...
- POJ推荐50题
此文来自北京邮电大学ACM-ICPC集训队 此50题在本博客均有代码,可以在左侧的搜索框中搜索题号查看代码. 以下是原文: POJ推荐50题1.标记“难”和“稍难”的题目可以看看,思考一下,不做要求, ...
- JAVA经典算法50题(转)
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/51097928 JAVA经典算法50题 [程序1] 题目:古典问题:有一对兔子, ...
- 剑指offer 面试50题
面试50题: 题目:第一个只出现一次的字符 题:在一个字符串(1<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置. 解题思路一:利用Python特 ...
- Java经典逻辑编程50题
Java经典逻辑编程50题 2016-11-03 09:29:28 0个评论 来源:Alias_fa的博客 收藏 我要投稿 [程序1] 題目:古典问题:有一对兔子,从出生后第 ...
- sql 经典查询50题 思路(一)
因为需要提高一下sql的查询能力,当然最快的方式就是做一些实际的题目了.选择了这个sql的50题,这次大概做了前10题左右,把思路放上来,也是一个总结. 具体题目见: https://zhuanlan ...
- 转:sql 经典50题--可能是你见过的最全解析
题记:从知乎上看到的一篇文章,刚好最近工作中发现遇到的题目与这个几乎一样,可能就是从这里来的吧.^_^ 里面的答案没有细看,SQL求解重在思路,很多时候同一种结果可能有多种写法,比如题中的各科成绩取前 ...
随机推荐
- Building Applications with Force.com and VisualForce(Dev401)(十四):Implementing Business Processes:Auditing Processes
Dev401-015:Implementing Business Processes:Auditing Processes Module Objectives1.list some of the fe ...
- Linux 常用命令速记
1.touch:创建文件,比如: touch a.txt 2.vi:编辑修改文件,比如: vi a.txt 打开编辑页面后: a 键:开始输入编辑: Esc键:结束输入内容 :wq:保存文件编辑内 ...
- zookeeper 负载均衡
1,原理 将启动的服务注册到zookeeper 注册中心上面,采用临时节点,zookeeper 客户端从注册中心上读取服务的信息,之后再本地采用负载均衡算法(取模算法),将请求轮询到每个服务. 同时z ...
- spring boot 源码赏析之事件监听
使用spring Boot已经快1年多了,期间一直想点开springboot源码查看,但由于种种原因一直未能如愿(主要是人类的惰性...),今天就拿springboot 的监听事件祭刀. spring ...
- MATLAB——nctoolbox安装及使用
1.nctoolbox安装 nctoolbox是一个Matlab工具箱,它提供对通用数据模型数据集的只读访问. (1)下载nctoolbox安装包. 地址:https://code.google.co ...
- A 拜访奶牛
时间限制 : - MS 空间限制 : 65536 KB 评测说明 : 时限1000ms 问题描述 经过了几周的辛苦工作,贝茜终于迎来了一个假期.作为奶牛群中最会社交的牛,她希望去拜访N(1< ...
- ERROR:TypeError: Cannot read property 'upgrade' of undefined
- Elasticsearch创建mapping
(put)请求方式 http://192.168.1.200:9200/index_mapping body 参数 { "mappings":{ "properties& ...
- Blocked Billboard II题解--模拟到崩溃的模拟
前言 比赛真的状态不好(腐了一小会),导致差点爆0. 这个题解真的是在非常非常专注下写出来的,要不然真的心态崩. 题目 题目描述 奶牛Bassie想要覆盖一大块广告牌,她在之前已经覆盖了一小部分广告牌 ...
- 对Web语义化的思考。
很有意思的HTML语义化 在昨天和做SEO的同学聊了一会儿,当然我没有学会搜索引擎优化的技巧和知识,但在此之前一直对HTML5中header.footer.sidebar.article等标签嗤之以鼻 ...