七十九、MySQL——MySQL50道练习题(每题都有答案哦)

2024-04-05 03:58

本文主要是介绍七十九、MySQL——MySQL50道练习题(每题都有答案哦),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

show databases ;
use schoolSql;create  table  if not exists  student(stu_id int(10),stu_name  varchar(10),stu_age  datetime,stu_gender  varchar(10)
);insert  into student values
('1','刘邦','1000-01-01','男'),
('2','吕雉','1000-01-02','女'),
('3','项羽','1000-01-03','男'),
('4','虞姬','1000-01-04','女');create  table  if not exists  course(cou_id int(10),cou_name  varchar(10),tea_id  int(10)
);insert into  course values
('1','语文','2'),
('2','数学','3'),
('3','英语','1');create  table  if not exists  teacher(tea_id int(10) primary key ,tea_name varchar(20)
);
insert into  teacher values('1','张三'),('2','李四'),('3','王五');select  * from teacher;drop table  result;
create  table  if not exists  result(stu_id int(10),cou_id int(10),score decimal (18,1)
);insert into  result values
('1','2',90),
('1','3',74),
('2','1',85),
('2','2',65),
('2','3',75),
('3','1',43),
('3','2',50),
('4','1',88),
('4','2',43),
('4','3',99);# 1、查询"1"课程比"2"课程成绩高的学生的信息及课程分数
select  s.* ,b.score as '1分数',c.score as '2分数'
from student as s
inner  join result b  on b.stu_id=s.stu_id  and b.cou_id='1'
inner join result c on c.stu_id=s.stu_id  and c.cou_id='2'
where b.score>c.score;#2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
select  s.*,r.score '1分数',c.score '2分数'
from student s
left join  result r on s.stu_id = r.stu_id and r.cou_id='1'
left join result c on c.stu_id=s.stu_id and c.cou_id='2'
where r.score<c.score;#3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
select  s.stu_id,s.stu_name ,avg(score) as avg_score
from student as s
left join  result r on s.stu_id = r.stu_id
group by s.stu_id, s.stu_name
HAVING avg_score>=60;#4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩
select s.stu_id,s.stu_name,avg(score) as  avg_score
from student as s
left join  result r on s.stu_id = r.stu_id
group by s.stu_id, s.stu_name
having  avg_score<60;#5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
select  s.stu_id,s.stu_name,count(r.cou_id) as count,sum(r.score) as sum_score
from student as s
left join    result r on s.stu_id = r.stu_id
group by s.stu_id, s.stu_name;#6、查询"李"姓老师的数量
select count(t.tea_name)
from teacher as t where  t.tea_name like '李%';#7、查询学过"张三"老师授课的同学的信息
select  s.*
from student as s
left join result r on s.stu_id = r.stu_id
left join course c on r.cou_id = c.cou_id
left join teacher t on c.tea_id = t.tea_id
where t.tea_name='张三';#8、查询没学过"张三"老师授课的同学的信息
/*   错误
select  s.*
from student as s
left join result r on s.stu_id = r.stu_id
left join course c on r.cou_id = c.cou_id
left join teacher t on c.tea_id = t.tea_id
where t.tea_name!='张三';*/select  s2.* from student as s2
where s2.stu_id not in
(select  s.stu_id
from student as s
left join result r on s.stu_id = r.stu_id
left join course c on r.cou_id = c.cou_id
left join teacher t on c.tea_id = t.tea_id
where t.tea_name='张三');#9、查询学过编号为"1"并且也学过编号为"2"的课程的同学的信息
select s.*
from student as s
left join result r on s.stu_id = r.stu_id
left join result r2 on s.stu_id=r2.stu_id
where  r.cou_id='1' and r2.cou_id='2';#10、查询学过编号为"1"但是没有学过编号为"2"的课程的同学的信息
select  s.* from student as s
left join result r on s.stu_id = r.stu_id
left join result r2 on s.stu_id=r2.stu_id
where r.cou_id='1' and r2.cou_id is null ;#11、查询没有学全所有课程的同学的信息#方法1
select  s.* from student as s
left join result r1 on s.stu_id = r1.stu_id
and  r1.cou_id='1'
left join result r2 on s.stu_id=r2.stu_id
and r2.cou_id='2'
left join  result r3 on s.stu_id=r3.stu_id
and r3.cou_id='3'
where  r1.score is null  or r2.score is null  or r3.score is null ;#方法2
SELECT  st.stu_id,st.stu_name,st.stu_age,stu_gender
FROM student st
LEFT JOIN result sc ON st.stu_id=sc.stu_id
GROUP BY st.stu_id ,stu_name,stu_age,stu_gender
HAVING COUNT(sc.score)<3;#12、查询至少有一门课与学号为"1"的同学所学相同的同学的信息
select  distinct s.* from student  as s
left join result r on s.stu_id = r.stu_id
where r.cou_id in(select r2.cou_id from result r2left join  student s2 on s2.stu_id = r2.stu_idwhere  r2.stu_id='1');#13、查询和"01"号的同学学习的课程完全相同的其他同学的信息
#使用GROUP_CONCAT函数和GROUP BY
select  s.* from student as s  where stu_id in(select  r.stu_id  from(select  * from result where stu_id='1') as  rjoin  result as r2 on r.cou_id=r2.cou_idgroup by  r2.stu_idhaving  count(*)=(select  count(*) from result where  r.stu_id='1' group by  r.stu_id))
and stu_id<>'1';#14、查询没学过"张三"老师讲授的任一门课程的学生姓名
select s.stu_name from student as s
where s.stu_id  not in(select s2.stu_id from student as s2left join  result r on s2.stu_id = r.stu_idleft join course c on r.cou_id = c.cou_idleft join teacher t on c.tea_id = t.tea_idwhere  t.tea_name='张三');#15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
select s.stu_id, s.stu_name,avg(r.score) from student as s
left join result r on s.stu_id = r.stu_id
where  r.stu_id in(select  r.stu_id from result rwhere  r.score<60 or r.score is nullgroup by  r.stu_idhaving  count(1)>=2)
group by s.stu_id, s.stu_name;#16、检索"01"课程分数小于60,按分数降序排列的学生信息
select s.* from student as s
left join result r on s.stu_id = r.stu_id
where r.stu_id  in(select  r.stu_id from resultwhere r.cou_id='1' and r.score<60)
order by  r.score desc ;#17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
select s.stu_id,s.stu_name,(case when avg(r4.score) is null then 0 else avg(r4.score) end) "平均分",(case when r1.score is null then 0 else r1.score end) "语文",(case when r2.score is null  then 0 else r2.score end) "数学",(case when  r3.score is null  then 0 else r3.score end) "英语"
from student as s
left join result r1 on s.stu_id = r1.stu_id and r1.cou_id='1'
left join result r2 on s.stu_id=r2.stu_id and r2.cou_id='2'
left join result r3 on s.stu_id=r3.stu_id and r3.cou_id='3'
left join result r4 on s.stu_id=r4.stu_id
group by s.stu_id, s.stu_name, (case when r1.score is null then 0 else r1.score end), (case when r2.score is null  then 0 else r2.score end), (case when  r3.score is null  then 0 else r3.score end)
order by  avg(r4.score) desc ;#18、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
#   及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
select c.cou_id,c.cou_name,max(r.score) "最高分" ,min(r.score) "最高分",avg(r.score) "平均分",sum(case when r.score>=60 then 1 else 0 end)/count(1) "及格率",sum(case when r.score>=70 and r.score<80 then 1 else  0 end)/count(1) "中等率",sum(case when r.score>=80 and r.score<90 then 1 else  0 end)/count(1) "优良率",sum(case when r.score>=90 then 1 else 0 end)/count(1) "优秀率"
from course as c
left join result r on c.cou_id = r.cou_id
group by c.cou_id, c.cou_name;#19、按各科成绩进行排序,并显示排名
select c.cou_name ,r.score from course as c
left join result r on c.cou_id = r.cou_id
group by c.cou_name, r.score
order by c.cou_name, r.score desc;#20、查询学生的总成绩并进行排名
select s.stu_id,s.stu_name,sum(r.score) as "总成绩" from student as s
left join result r on s.stu_id = r.stu_id
group by  s.stu_id, s.stu_name
order by  sum(r.score) desc;#21、查询不同老师所教不同课程平均分从高到低显示
select t.tea_id,t.tea_name,c.cou_id,avg(r.score) from teacher as t
left join course c on t.tea_id = c.tea_id
left join result r on c.cou_id = r.cou_id
group by t.tea_id, t.tea_name,c.cou_id
order by avg(r.score) desc;#22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
select  t1.* from
(select s.*,c.cou_id,c.cou_name,r.score from student as s
left join result r on s.stu_id = r.stu_id
inner join course c on r.cou_id = c.cou_id and c.cou_id='1'
order by r.score desc limit  1,2)t1union allselect  t2.* from
(select s.*,c.cou_id,c.cou_name,r.score from student as s
left join result r on s.stu_id = r.stu_id
inner join course c on r.cou_id = c.cou_id and c.cou_id='2'
order by r.score desc limit  1,2)t2union allselect  t3.* from
(select s.*,c.cou_id,c.cou_name,r.score from student as s
left join result r on s.stu_id = r.stu_id
inner join course c on r.cou_id = c.cou_id and c.cou_id='3'
order by r.score desc limit  1,2) t3;#23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
select c.cou_id,c.cou_name,sum(case  when r.score >=0 and r.score<60 then 1 else 0 end) as "0-60",sum(case  when r.score>=60 and r.score<70 then 1 else  0 end) as "70-60",sum(case  when r.score>=70 and r.score<85 then 1 else 0 end) as "85-70",sum(case  when r.score>=85 and r.score<100 then 1 else  0 end) as"100-85",sum(case  when r.score >=0 and r.score<60 then 1 else 0 end)/count(1) as "0-60百分比",sum(case  when r.score>=60 and r.score<70 then 1 else  0 end)/count(1) as "70-60百分比",sum(case  when r.score>=70 and r.score<85 then 1 else 0 end)/count(1) as "85-70百分比",sum(case  when r.score>=85 and r.score<100 then 1 else  0 end)/count(1) as"100-85百分比"
from course as c
left join result r on c.cou_id = r.cou_id
group by c.cou_id, c.cou_name;#24、查询学生平均成绩及其名次
#自己对自己左交,查看比自己分数高的有几个
select s.stu_name,avg(r.score) from student as s
left join result r on s.stu_id = r.stu_id
group by s.stu_name
order by avg(r.score) desc ;#25、查询各科成绩前三名的记录
SELECT a.*,COUNT(b.cou_id)+1 AS ascore
FROM result a
LEFT JOIN result b
ON a.cou_id=b.cou_id AND a.score<b.score
GROUP BY 1,2,3
HAVING ascore<=3
ORDER BY a.cou_id,ascore;
#方法二
select s.stu_name,c.cou_name,count(r2.cou_id)+1 as ascore from result r
left join result r2 on r.cou_id=r2.cou_id and r.score<r2.score
left join student s on r.stu_id = s.stu_id
left join course c on r.cou_id = c.cou_id
group by s.stu_name, c.cou_name
having  ascore<=3
order by c.cou_name,ascore;#26、查询每门课程被选修的学生数
select c.cou_name,count(r.cou_id) as "选修人数" from course as c
left join result r on c.cou_id = r.cou_id
group by c.cou_name;#27、查询出只有两门课程的全部学生的学号和姓名
select s.stu_id,s.stu_name from student as s
left join result r on s.stu_id = r.stu_id
group by s.stu_id, s.stu_name
having  count(r.cou_id)=2;#28、查询男生、女生人数
select s.stu_gender,count(1) from student as s
group by s.stu_gender;#29、查询名字中含有"羽"字的学生信息
select s.* from student as s
where s.stu_name like '%羽%';#30、查询同名同姓学生名单,并统计同名人数
select s.stu_name,count(1) from student as s
group by s.stu_name
having count(1)>1;#31、查询1000年出生的学生名单(注:Student表中Sage列的类型是datetime)
select s.* from student as s
where year(s.stu_age)=1000;#32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号
select r.cou_id,c.cou_name,avg(r.score) as avg_score from result as r
left join course c on r.cou_id = c.cou_id
group by r.cou_id, c.cou_name
order by avg_score desc ,r.cou_id ;#33、查询平均成绩大于等于80的所有学生的学号、姓名和平均成绩
select s.stu_id,s.stu_name,avg(r.score) "平均分" from student as s
left join result r on s.stu_id = r.stu_id
group by s.stu_id, s.stu_name
having  avg(r.score)>=80;#34、查询课程名称为"数学",且分数低于60的学生姓名和分数
select s.stu_id,s.stu_name,r.score  from student as s
left join result r on s.stu_id = r.stu_id
inner  join course c on r.cou_id = c.cou_id
where c.cou_name='数学'
having r.score<60;#35、查询所有学生的课程及分数情况
select s.stu_id,s.stu_name,c.cou_id,c.cou_name,r.score from student as s
left join result r on s.stu_id = r.stu_id
left join course c on r.cou_id = c.cou_id
group by s.stu_id, s.stu_name, c.cou_id, c.cou_name, r.score;#36、查询任何一门课程成绩在80分以上的姓名、课程名称和分数
select s.stu_name,c.cou_name,r.score from student as s
left join result r on s.stu_id = r.stu_id
left join course c on r.cou_id = c.cou_id
group by s.stu_name, c.cou_name, r.score
having r.score>80;#37、查询不及格的课程
select s.stu_id,s.stu_name,c.cou_name,r.score from student as s
left join  result r on s.stu_id = r.stu_id
left join course c on r.cou_id = c.cou_id
where r.score<60
group by s.stu_id, s.stu_name, c.cou_name, r.score;#38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名
select s.stu_id,s.stu_name,r.score  from student as s
left join result r on s.stu_id = r.stu_id
left join course c on r.cou_id = c.cou_id
where  c.cou_id='1' and  r.score>80;#39、求每门课程的学生人数
select c.cou_name,count(c.cou_id) from course as c
left join result r on c.cou_id = r.cou_id
group by c.cou_name;#40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
select s.*,r.score from student as s
left join result r on s.stu_id = r.stu_id
left join course c on r.cou_id = c.cou_id
left join teacher t on c.tea_id = t.tea_id
where t.tea_name='张三'
order by r.score  desc  limit  1;#41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
select r.stu_id,r.cou_id,r.score from result as r
left join result r2 on r.score=r2.score and r.cou_id <>r2.cou_id
#group by 1,2,3
group by r.stu_id, r.cou_id, r.score
having  count(r2.stu_id)>0;#方法2
select r.* from result as r
left join result r2 on r.score=r2.score and r.cou_id <>r2.cou_id
group by 1,2,3
having  count(r2.stu_id)>0;#42、查询每门功成绩最好的前两名
select r1.stu_id,r1.cou_id,r1.score from result as r1
left join  result r2 on r1.cou_id=r2.cou_id and r1.score<r2.score
group by  r1.stu_id, r1.cou_id, r1.score
having  count(r2.cou_id)<2
order by r1.cou_id,r1.score  desc;#43、统计每门课程的学生选修人数(超过2人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
select c.cou_id,count(r.cou_id) as acount from course as c
left join result r on c.cou_id = r.cou_id
group by c.cou_id
having acount>2
order by acount  desc ,c.cou_id;#44、检索至少选修两门课程的学生学号
select s.stu_id,s.stu_name ,count(r.cou_id) as acount from student as s
left join result r on s.stu_id = r.stu_id
group by s.stu_id, s.stu_name
having  acount>=2;#45、查询选修了全部课程的学生信息
select s.stu_id,s.stu_name,s.stu_age,s.stu_gender from student as s
left join result r on s.stu_id = r.stu_id
group by s.stu_id, s.stu_name, s.stu_age, s.stu_gender
having  count(r.cou_id)=3;#46、查询各学生的年龄
select s.*,(YEAR(CURDATE())-YEAR(s.stu_age))as "年龄" from student as s;#47、查询本周过生日的学生
select s.* from student as s
where weekofyear(s.stu_age)=weekofyear(curdate());#48、查询下周过生日的学生
select s.* from student as s
where weekofyear(s.stu_age)=weekofyear(curdate())+1;#49、查询本月过生日的学生
select  s.* from student as s
where month(s.stu_age)=month(curdate());#50、查询上月过生日的学生
select  s.* from student as s
where month(s.stu_age)=month(curdate())-1;

乐于奉献共享,帮助你我他!

这篇关于七十九、MySQL——MySQL50道练习题(每题都有答案哦)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/877578

相关文章

SQL中的外键约束

外键约束用于表示两张表中的指标连接关系。外键约束的作用主要有以下三点: 1.确保子表中的某个字段(外键)只能引用父表中的有效记录2.主表中的列被删除时,子表中的关联列也会被删除3.主表中的列更新时,子表中的关联元素也会被更新 子表中的元素指向主表 以下是一个外键约束的实例展示

基于MySQL Binlog的Elasticsearch数据同步实践

一、为什么要做 随着马蜂窝的逐渐发展,我们的业务数据越来越多,单纯使用 MySQL 已经不能满足我们的数据查询需求,例如对于商品、订单等数据的多维度检索。 使用 Elasticsearch 存储业务数据可以很好的解决我们业务中的搜索需求。而数据进行异构存储后,随之而来的就是数据同步的问题。 二、现有方法及问题 对于数据同步,我们目前的解决方案是建立数据中间表。把需要检索的业务数据,统一放到一张M

如何去写一手好SQL

MySQL性能 最大数据量 抛开数据量和并发数,谈性能都是耍流氓。MySQL没有限制单表最大记录数,它取决于操作系统对文件大小的限制。 《阿里巴巴Java开发手册》提出单表行数超过500万行或者单表容量超过2GB,才推荐分库分表。性能由综合因素决定,抛开业务复杂度,影响程度依次是硬件配置、MySQL配置、数据表设计、索引优化。500万这个值仅供参考,并非铁律。 博主曾经操作过超过4亿行数据

性能分析之MySQL索引实战案例

文章目录 一、前言二、准备三、MySQL索引优化四、MySQL 索引知识回顾五、总结 一、前言 在上一讲性能工具之 JProfiler 简单登录案例分析实战中已经发现SQL没有建立索引问题,本文将一起从代码层去分析为什么没有建立索引? 开源ERP项目地址:https://gitee.com/jishenghua/JSH_ERP 二、准备 打开IDEA找到登录请求资源路径位置

MySQL数据库宕机,启动不起来,教你一招搞定!

作者介绍:老苏,10余年DBA工作运维经验,擅长Oracle、MySQL、PG、Mongodb数据库运维(如安装迁移,性能优化、故障应急处理等)公众号:老苏畅谈运维欢迎关注本人公众号,更多精彩与您分享。 MySQL数据库宕机,数据页损坏问题,启动不起来,该如何排查和解决,本文将为你说明具体的排查过程。 查看MySQL error日志 查看 MySQL error日志,排查哪个表(表空间

MySQL高性能优化规范

前言:      笔者最近上班途中突然想丰富下自己的数据库优化技能。于是在查阅了多篇文章后,总结出了这篇! 数据库命令规范 所有数据库对象名称必须使用小写字母并用下划线分割 所有数据库对象名称禁止使用mysql保留关键字(如果表名中包含关键字查询时,需要将其用单引号括起来) 数据库对象的命名要能做到见名识意,并且最后不要超过32个字符 临时库表必须以tmp_为前缀并以日期为后缀,备份

[MySQL表的增删改查-进阶]

🌈个人主页:努力学编程’ ⛅个人推荐: c语言从初阶到进阶 JavaEE详解 数据结构 ⚡学好数据结构,刷题刻不容缓:点击一起刷题 🌙心灵鸡汤:总有人要赢,为什么不能是我呢 💻💻💻数据库约束 🔭🔭🔭约束类型 not null: 指示某列不能存储 NULL 值unique: 保证某列的每行必须有唯一的值default: 规定没有给列赋值时的默认值.primary key:

dp算法练习题【8】

不同二叉搜索树 96. 不同的二叉搜索树 给你一个整数 n ,求恰由 n 个节点组成且节点值从 1 到 n 互不相同的 二叉搜索树 有多少种?返回满足题意的二叉搜索树的种数。 示例 1: 输入:n = 3输出:5 示例 2: 输入:n = 1输出:1 class Solution {public int numTrees(int n) {int[] dp = new int

poj 3104 二分答案

题意: n件湿度为num的衣服,每秒钟自己可以蒸发掉1个湿度。 然而如果使用了暖炉,每秒可以烧掉k个湿度,但不计算蒸发了。 现在问这么多的衣服,怎么烧事件最短。 解析: 二分答案咯。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <c

MySQL-CRUD入门1

文章目录 认识配置文件client节点mysql节点mysqld节点 数据的添加(Create)添加一行数据添加多行数据两种添加数据的效率对比 数据的查询(Retrieve)全列查询指定列查询查询中带有表达式关于字面量关于as重命名 临时表引入distinct去重order by 排序关于NULL 认识配置文件 在我们的MySQL服务安装好了之后, 会有一个配置文件, 也就