Mysql 50题练习

在网上找到的Mysql 50题练习(https://note.youdao.com/ynoteshare1/index.html?id=45d4298f42397bd52ccf6fc716e27ee9&type=note#/),原博部分题目的答案有些问题,我根据原博所给答案自己做了一遍,对有问题的答案做了些修改。部分答案可能有更简单的方法,欢迎提出。
创建表:

create table Student(SId varchar(10),Sname varchar(10),Sage datetime,Ssex varchar(10));
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('09' , '张三' , '2017-12-20' , '女');
insert into Student values('10' , '李四' , '2017-12-25' , '女');
insert into Student values('11' , '李四' , '2017-12-30' , '女');
insert into Student values('12' , '赵六' , '2017-01-01' , '女');
insert into Student values('13' , '孙七' , '2018-01-01' , '女');

create table Course(CId varchar(10),Cname nvarchar(10),TId varchar(10));
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');

create table Teacher(TId varchar(10),Tname varchar(10));
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');

create table SC(SId varchar(10),CId varchar(10),score decimal(18,1));
insert into SC values('01' , '01' , 80);
insert into SC values('01' , '02' , 90);
insert into SC values('01' , '03' , 99);
insert into SC values('02' , '01' , 70);
insert into SC values('02' , '02' , 60);
insert into SC values('02' , '03' , 80);
insert into SC values('03' , '01' , 80);
insert into SC values('03' , '02' , 80);
insert into SC values('03' , '03' , 80);
insert into SC values('04' , '01' , 50);
insert into SC values('04' , '02' , 30);
insert into SC values('04' , '03' , 20);
insert into SC values('05' , '01' , 76);
insert into SC values('05' , '02' , 87);
insert into SC values('06' , '01' , 31);
insert into SC values('06' , '03' , 34);
insert into SC values('07' , '02' , 89);
insert into SC values('07' , '03' , 98);

题目与答案:

#一 查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数
select * from (select * from sc where CId='01') as t1,(select * from sc where CId='02') as t2
WHERE t1.SId=t2.SId
and t1.score>t2.score;
#1.1 查询同时存在" 01 "课程和" 02 "课程的情况
select *
from (select SId ,score from sc where sc.CId='01')as t1 , (select SId ,score from sc where sc.CId='02') as t2
where t1.SId=t2.SId;
#1.2 查询存在" 01 "课程但可能不存在" 02 "课程的情况(不存在时显示为 null )
select *
from (select SId ,score from sc where sc.CId='01')as t1 left join (select SId ,score from sc where sc.CId='02') as t2
on t1.SId=t2.SId;
#1.3 查询不存在" 01 "课程但存在" 02 "课程的情况
select *
from sc
where sc.SId not in (select SId from sc where sc.CId='01')
and  sc.CId='02';

#二 查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩
select sc.SId,Sname,avg(score) from sc,student as st
where sc.SId=st.SId
group by SId
having avg(score)>=60;

#三 查询在 SC 表存在成绩的学生信息
select DISTINCT student.*
from student ,sc
where student.SId=sc.SId;

#四 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为null)
select student.SId,student.Sname,t1.sumscore,t1.coursecount
from student ,(
select SC.SId,sum(sc.score)as sumscore ,count(sc.CId) as coursecount
from sc 
GROUP BY sc.SId) as t1
where student.SId =t1.SId;

#五 查询「李」姓老师的数量
select count(*)
from teacher
where teacher.Tname like '李%';


#六 查询学过「张三」老师授课的同学的信息
select student.*
from teacher  ,course  ,student,sc
where teacher.Tname='张三'
and   teacher.TId=course.TId
and   course.CId=sc.CId
and   sc.SId=student.SId;


#七 查询没有学全所有课程的同学的信息
select student.*
from sc ,student
where sc.SId=student.SId
GROUP BY sc.SId
Having count(*)<(select count(*) from course);
#此方法无法找出全都没选的学生

select DISTINCT student.*
from 
(select student.SId,course.CId
from student,course ) as t1 LEFT JOIN (SELECT sc.SId,sc.CId from sc)as t2 on t1.SId=t2.SId and t1.CId=t2.CId,student
where t2.SId is null
and   t1.SId=student.SId;

#八 查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息
select DISTINCT student.*
from  sc ,student
where sc.CId in (select CId from sc where sc.SId='01')
and   sc.SId=student.SId;


#九 查询和" 01 "号的同学学习的课程完全相同的其他同学的信息
select student.* from student
	where student.SId not in (
	select distinct r.SId from 
	(
	select   b1.SId, b1.CId as choose_course, sc.CId as has_score from
	(select student.SId, b.CId
	from student,(select SId,CId from sc where SId = '01') as b) as b1 left join sc on b1.CId = sc.CId and b1.SId = sc.SId
	) as r
	where r.has_score is null ) and student.SId != '01';
    
#第十题 查询没学过"张三"老师讲授的任一门课程的学生姓名
select * from student
where student.SId not in (
select SId from teacher,course,sc
where course.TId=teacher.TId
and course.CId=sc.CId
and teacher.Tname='张三');

#十一题 查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
SELECT sc.SId,Sname,AVG(score) from sc,
(select student.SId,Sname,avg(sc.score)
from sc ,student 
where student.SId=sc.SId
and score < 60
group by student.SId
having count(score)>=2) as t1
where sc.SId=t1.SId
group by sc.SId;

#十二题 检索" 01 "课程分数小于 60,按分数降序排列的学生信息
select * from student,sc
where student.SId=sc.SId
and sc.CId='01'
and score<60
order by score DESC;

#十三题 按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
select sc.SId,sc.CId,sc.score,t1.avgs
from sc
left join (
select SId,avg(score) as avgs from sc
group by SId
) as t1
on t1.SId=sc.SId
order by avgs DESC;

#十四题 查询各科成绩最高分、最低分和平均分: 
#以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 及格为>=60,
#中等为:70-80,优良为:80-90,优秀为:>=90 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
select sc.CId as '课程ID',course.Cname as '课程name',max(score) as '最高分',min(score) as '最低分',avg(score) as '平均分' ,count(*) as '选修人数',
t2.t1 as 及格率,t3.t1 as 中等率,t4.t1 as 优良率,t5.t1 as 优秀率
from sc,course,(select sum(case when sc.score>=60 then 1 else 0 end)/count(*) as t1,CId from sc group by sc.CId) as t2,
(select sum(case when sc.score>=70 and sc.score<80 then 1 else 0 end)/count(*) as t1,CId from sc group by sc.CId) as t3,
(select sum(case when sc.score>=80 and sc.score<90 then 1 else 0 end)/count(*) as t1,CId from sc group by sc.CId) as t4,
(select sum(case when sc.score>=90 then 1 else 0 end)/count(*) as t1,CId from sc group by sc.CId) as t5
where sc.CId=course.CId
and t2.CId=sc.CId
and t3.CId=sc.CId
and t4.CId=sc.CId
and t5.CId=sc.CId
group by sc.CId
ORDER BY count(*)DESC,sc.CId asc;

#十五题 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺
#按各科成绩进行排序,并显示排名, Score 重复时合并名次
select *,rank() over(partition by sc.CId order by score desc) as 'rank' from sc;
#参考网址:https://yq.aliyun.com/articles/593698?utm_content=m_1000014867

#十六题  查询学生的总成绩,并进行排名,总分重复时不保留名次空缺
#查询学生的总成绩,并进行排名,总分重复时不保留名次空缺
select SId, ss,rank() over(order by  t1.ss desc) as 'rank' from
(select SId,sum(score) as ss from sc
group by sc.SId) as t1;
#更好的答案:https://zhuanlan.zhihu.com/p/137908870
select SId,sum(score) 总成绩,rank() over(order by sum(score) desc) 排名 from sc group by SId;
select SId,sum(score) 总成绩,dense_rank() over(order by sum(score) desc) 排名 from sc group by SId;


#十七题 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比
select sc.CId,course.Cname,t1.t0 as '[100-85]',t1.num,t2.t0 as '[85-70]',t2.num,t3.t0 as '[70-60]',t3.num,t4.t0 as '[60-0]',t4.num
from sc,course,
(select sum(case when score>=85 then 1 else 0 end)/count(*) as t0 ,CId,sum(case when score>=85 then 1 else 0 end) as num from sc group by sc.CId) as t1,
(select sum(case when score>=70 and score<85 then 1 else 0 end)/count(*) as t0 ,CId ,sum(case when score>=70 and score<85 then 1 else 0 end) as num from sc group by sc.CId) as t2,
(select sum(case when score>=60 and score<70 then 1 else 0 end)/count(*) as t0,CId,sum(case when score>=60 and score<70 then 1 else 0 end) as num  from sc group by sc.CId) as t3,
(select sum(case when score<60  then 1 else 0 end)/count(*) as t0,CId ,sum(case when score<60  then 1 else 0 end) as num from sc group by sc.CId) as t4
where sc.CId = course.CId
and t1.CId=sc.CId
and t2.CId=sc.CId
and t3.CId=sc.CId
and t4.CId=sc.CId
group by sc.CId;

#十八题 查询各科成绩前三名的记录
select SId,CId,score,r from (select *,rank() over(partition by sc.CId order by score desc) as r from sc) as t1
where r<=3;

#十九题 查询每门课程被选修的学生数
select count(*) from sc group by CId;

#二十题 查询出只选修两门课程的学生学号和姓名
select Sname,sc.SId from student,sc
where student.SId = sc.SId
group by sc.SId
having count(CId)=2;

#二十一题 查询男生、女生人数
select Ssex,count(Ssex) from student
group by Ssex;

#二十二题 查询名字中含有「风」字的学生信息
select * from student where Sname like '%风%';
#模糊查询:like between and in innull

#二十三题 查询同名同性学生名单,并统计同名人数
select Sname,count(*) from student group by Sname,Ssex having count(*)>1;

#二十四题 查询 1990 年出生的学生名单
select Sname from student where sage like '%1990%';
#更好的答案
select *
from student
where YEAR(student.Sage)=1990;

#二十五题 查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
select CId,avg(score) from sc group by CId order by avg(score) desc,CId asc;

#二十六题 查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩
select student.SId,student.Sname,avg(score) from sc,student
where sc.SId=student.SId
group by student.SId
having avg(score)>=85;

#二十七题 查询课程名称为「数学」,且分数低于 60 的学生姓名和分数
select Sname,score from sc,student,course
where sc.SId=student.SId
and sc.CId=course.CId
and course.Cname='数学'
and score<60;

#二十八 查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况)
select student.SId,sc.CId,sc.score from sc
right join student
on sc.SId=student.SId;

#二十九 查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数
select Sname,Cname,t1.score 
from course,(select * from sc where score>70) as t1,student
where course.CId=t1.CId
and t1.SId=student.SId;

#三十 查询存在不及格的课程
select distinct(Cname)
from course,sc
where sc.CId=course.CId
and score<60;

#三十一 查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名
select student.SId,student.Sname,sc.score
from student,sc
where student.SId=sc.SId
and sc.CID='01'
and sc.score>80;

#三十二 求每门课程的学生人数
select sc.CId,count(*) as 学生人数
from sc
GROUP BY sc.CId;

#三十三 成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
select Sname,score from student,sc,teacher,course
where student.SId=sc.SId
and course.TId=teacher.TId
and course.CId=sc.CId
and Tname='张三'
order by score DESC
limit 1;

#三十四 成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
select sc.SId,Sname,score,dense_rank() over(order by sc.CId desc) 排名 from sc ,student,course,teacher
where student.SId=sc.SId
and course.TId=teacher.TId
and course.CId=sc.CId
and teacher.Tname='张三'
group by sc.SId
order by score DESC
LIMIT 1;

#三十五 查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
select *
from sc as t1
where exists(select * from sc as t2 where t1.SId=t2.SId and t1.CId!=t2.CId and t1.score =t2.score );

#三十六 查询每门功成绩最好的前两名
select SId,CId,score,t1.r as 'rank' 
from (select *,rank() over(partition by sc.CId order by score desc) as r from sc) as t1
where r<=2;

#三十七 统计每门课程的学生选修人数(超过 5 人的课程才统计)
select sc.CId,count(*) from sc
group by sc.CId
having count(*)>5;

#三十八 检索至少选修两门课程的学生学号
select SId,count(*) from sc
group by SId
having count(*)>2;

#三十九 查询选修了全部课程的学生信息
select student.SId,student.Sname,student.Sage,student.Ssex from student,sc
where student.SId=sc.SId
group by sc.SId
having count(sc.CId)=(select count(distinct(CId)) from sc );

#四十 查询各学生的年龄,只按年份来算
select Sname,2020-year(Sage) from student;
#更好的答案
select student.SId as 学生编号,student.Sname  as  学生姓名,TIMESTAMPDIFF(year,student.Sage,CURDATE()) as 学生年龄
from student;

#四十一 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一
SELECT YEAR(NOW())-YEAR(Sage)-(CASE WHEN DATE_FORMAT(NOW(),'%m%d')>DATE_FORMAT(Sage,'%m%d') THEN 0 ELSE 1 END ) AS age FROM student;
#我觉得这题有点难 

#四十二 查询本周过生日的学生
select Sname from student
where week(now()) = week(Sage);

#四十三 查询下周过生日的学生
select Sname from student
where week(now())+1 = week(Sage);

#四十四 查询本月过生日的学生
select Sname from student
where month(now()) = month(Sage);

#四十五 查询下月过生日的学生
select Sname from student
where month(now())+1 = month(Sage);




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值