七十九、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

相关文章

MySQL分表自动化创建的实现方案

《MySQL分表自动化创建的实现方案》在数据库应用场景中,随着数据量的不断增长,单表存储数据可能会面临性能瓶颈,例如查询、插入、更新等操作的效率会逐渐降低,分表是一种有效的优化策略,它将数据分散存储在... 目录一、项目目的二、实现过程(一)mysql 事件调度器结合存储过程方式1. 开启事件调度器2. 创

SQL Server使用SELECT INTO实现表备份的代码示例

《SQLServer使用SELECTINTO实现表备份的代码示例》在数据库管理过程中,有时我们需要对表进行备份,以防数据丢失或修改错误,在SQLServer中,可以使用SELECTINT... 在数据库管理过程中,有时我们需要对表进行备份,以防数据丢失或修改错误。在 SQL Server 中,可以使用 SE

mysql外键创建不成功/失效如何处理

《mysql外键创建不成功/失效如何处理》文章介绍了在MySQL5.5.40版本中,创建带有外键约束的`stu`和`grade`表时遇到的问题,发现`grade`表的`id`字段没有随着`studen... 当前mysql版本:SELECT VERSION();结果为:5.5.40。在复习mysql外键约

SQL注入漏洞扫描之sqlmap详解

《SQL注入漏洞扫描之sqlmap详解》SQLMap是一款自动执行SQL注入的审计工具,支持多种SQL注入技术,包括布尔型盲注、时间型盲注、报错型注入、联合查询注入和堆叠查询注入... 目录what支持类型how---less-1为例1.检测网站是否存在sql注入漏洞的注入点2.列举可用数据库3.列举数据库

Mysql虚拟列的使用场景

《Mysql虚拟列的使用场景》MySQL虚拟列是一种在查询时动态生成的特殊列,它不占用存储空间,可以提高查询效率和数据处理便利性,本文给大家介绍Mysql虚拟列的相关知识,感兴趣的朋友一起看看吧... 目录1. 介绍mysql虚拟列1.1 定义和作用1.2 虚拟列与普通列的区别2. MySQL虚拟列的类型2

mysql数据库分区的使用

《mysql数据库分区的使用》MySQL分区技术通过将大表分割成多个较小片段,提高查询性能、管理效率和数据存储效率,本文就来介绍一下mysql数据库分区的使用,感兴趣的可以了解一下... 目录【一】分区的基本概念【1】物理存储与逻辑分割【2】查询性能提升【3】数据管理与维护【4】扩展性与并行处理【二】分区的

MySQL中时区参数time_zone解读

《MySQL中时区参数time_zone解读》MySQL时区参数time_zone用于控制系统函数和字段的DEFAULTCURRENT_TIMESTAMP属性,修改时区可能会影响timestamp类型... 目录前言1.时区参数影响2.如何设置3.字段类型选择总结前言mysql 时区参数 time_zon

Python MySQL如何通过Binlog获取变更记录恢复数据

《PythonMySQL如何通过Binlog获取变更记录恢复数据》本文介绍了如何使用Python和pymysqlreplication库通过MySQL的二进制日志(Binlog)获取数据库的变更记录... 目录python mysql通过Binlog获取变更记录恢复数据1.安装pymysqlreplicat

使用SQL语言查询多个Excel表格的操作方法

《使用SQL语言查询多个Excel表格的操作方法》本文介绍了如何使用SQL语言查询多个Excel表格,通过将所有Excel表格放入一个.xlsx文件中,并使用pandas和pandasql库进行读取和... 目录如何用SQL语言查询多个Excel表格如何使用sql查询excel内容1. 简介2. 实现思路3

Mysql DATETIME 毫秒坑的解决

《MysqlDATETIME毫秒坑的解决》本文主要介绍了MysqlDATETIME毫秒坑的解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着... 今天写代码突发一个诡异的 bug,代码逻辑大概如下。1. 新增退款单记录boolean save = s