学习笔记--亲测MySQL练习题(WIN10)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/d345389812/article/details/80683619

在上一章学习笔记--图解mySQL安装过程后,相信大家都已经学会了怎么在WIN10安装MySQL了,今天小文给大家带来一些MySQL练习题,据说学会这些练习题就可以完美撒花了!

MySQL版本:MySQL8.0

测试表格:

  • Student (SId,Sname,Sage,Ssex)
  • Course (CId,Cname,TId)
  • Teacher (TId,Tname)
  • SC (SId,CId,score)

建数据库,建表格:

Student:

CREATE DATABASE practise;
USE pratice;
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' , '张三' , '1989-12-20' , '女');
INSERT INTO Student VALUES('10' , '李四' , '1989-12-25' , '女');
INSERT INTO Student VALUES('11' , '李四' , '1991-12-30' , '女');
INSERT INTO Student VALUES('12' , '赵六' , '1992-01-01' , '女');
INSERT INTO Student VALUES('13' , '孙七' , '1991-01-01' , '女');

Course:

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

Teacher:

CREATE TABLE Teacher(TId varchar(10),Tname varchar(10));
INSERT INTO Teacher VALUES('01' , '张三');
INSERT INTO Teacher VALUES('02' , '李四');
INSERT INTO Teacher VALUES('03' , '王五');

SC:

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

练习题:

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

select * 
from (select * from SC where CId = '01') A, (select * from SC where CId = '02') B 
where A.SId = B.SId and A.Score > B.Score;

2.查询同时存在" 01 "课程和" 02 "课程的情况

select * 
from (select * from SC where CId = '01') A, (select * from SC where CId = '02') B 
where A.SId = B.SId;

3. 查询存在" 01 "课程但可能不存在" 02 "课程的情况(不存在时显示为 null )

select *
from (select SId ,score from SC where CId = '01') A 
left join (select SId ,score from SC where CId = '02') B
on A.SId = B.SId

4. 查询不存在" 01 "课程但存在" 02 "课程的情况

select * from SC 
where CId = '02' and SId not in (select SId from SC where CId = '01');

5. 查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩

select S.SId,S.Sname,T.avgscore from Student S 
inner join (select SC.SId ,AVG(SC.Score) avgscore from SC 
group by SC.SId having AVG(SC.Score)>60) T on S.SId = T.SId;

6. 查询在 SC 表存在成绩的学生信息

select distinct S.* 
from Student S,SC 
where S.SId = SC.SId;

7. 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )

select S.SId,S.Sname,T.count,T.sum 
from Student S ,(select SId,COUNT(CId) count, SUM(score) sum from SC group by SId) T 
on S.SId = T.SId;

8 查有成绩的学生信息

select distinct S.* 
from Student S,(select SId from SC where score is not null) T 
where S.SId = T.SId;

9 查询「李」姓老师的数量

select count(TId) from Teacher 
where Tname like '李%';

10 查询学过「张三」老师授课的同学的信息

select S.* from Student S,Course C,Teacher T,SC 
where T.Tname ='张三' and T.TId = C.TId and C.CId = SC.CId and SC.SId = S.SId;

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

select S.* 
from Student S,(select SId,count(CId) from SC group by SId having count(CId) < 3) T 
where S.SId = T.SId;

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

select distinct S.* from Student S,SC 
where S.SId = SC.SId and SC.CId in (select CId from SC where SId ='01');

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

select * from Student 
where SId in(select SId from SC where CId in (select CId from SC where SId = '01') 
and SId <> '01' group by SId having count(CId)=3);

14. 查询没学过"张三"老师讲授的任一门课程的学生姓名

select Student.Sname from Student 
where Student.SId not in(select SC.SId from Teacher T,Course C,SC where T.Tname ='张三' 
and T.TId = C.TId and C.CId = SC.CId);

15. 查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩

select S.Sname,S.SId,T.Avgscore from Student S,(select SID, AVG(Score) Avgscore from SC 
where score < 60 group by SId having count(CId) >= 2) T 
where S.SId = T.SId;

16. 检索" 01 "课程分数小于 60,按分数降序排列的学生信息

select S.* from Student S,
(select SId  from SC where CId = '01' and score < 60 order by score desc) T 
where S.SId = T.SId;

17. 按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩

select SC.*,T.Avgscore from SC ,(select SId,AVG(score) avgscore from sc group by SId) T 
where SC.SId = T. SId order by T.Avgscore desc;

18. 查询各科成绩最高分、最低分和平均分:

以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率, 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90

要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列

select CId,MAX(score),MIN(score),AVG(score),
SUM(case when score >= 60 then 1 else 0 end)/count(SId) 及格率,
SUM(case when score >= 70 and score < 80 then 1 else 0 end)/count(SId) 中等率,
SUM(case when score >= 80 and score < 90 then 1 else 0 end)/count(SId) 优良率,
SUM(case when score >= 90 then 1 else 0 end)/count(SId) 优秀率 
from SC group by CId order by count(SId) desc,CId asc;

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

select *,RANK()over(order by score desc)排名 from SC;

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

select *,DENSE_RANK()over(order by score desc)排名 from SC;

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

select *,RANK()over(order by 总成绩 desc)排名 
from (select SId,SUM(score)总成绩 from SC group by SId)A;

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

select *,dense_rank()over(order by 总成绩 desc)排名 
from(select SId,SUM(score)总成绩 from SC group by SId)A;

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

select C.Cname, T.* from Course C 
left join (select SC.CId,CONCAT(SUM(case when SC.score <= 60 then 1 else 0 end)/count(SC.SId)*100,'%') '[0-60]',
CONCAT(SUM(case when SC.score >60 and SC.score <= 70 then 1 else 0 end)/count(SC.SId)*100,'%') '[60-70]',
CONCAT(SUM(case when SC.score >70 and SC.score <= 85 then 1 else 0 end)/count(SC.SId)*100,'%') '[70-85]',
CONCAT(SUM(case when SC.score >85 and SC.score <= 100 then 1 else 0 end)/count(SC.SId)*100,'%') '[85-100]', 
from SC group by SC.CId) T on C.CId = T.CId;

24. 查询各科成绩前三名的记录

select * from(select *,rank()over (partition by CID order by score desc) 排名 from SC)B
 where B.排名<=3;

25. 查询每门课程被选修的学生数

select CId,count(SId) from SC group by CId;

26. 查询出只选修两门课程的学生学号和姓名

select S.SId,S.Sname from Student S 
where S.SId in (select SC.SId from SC group by SC.SId having count(SC.CId) = 2);

27. 查询男生、女生人数

select Student.Ssex ,count(*) 人数 
from student group by Student.Ssex;

28. 查询名字中含有「风」字的学生信息

select * from Student 
where Sname like '%风%';

29. 查询同名同性学生名单,并统计同名人数

select * from student 
left join (select Sname,Ssex,count(*)人数 from Student group by Sname,Ssex) T
on Student.Sname =T.Sname and Student.Ssex=T.Ssex where T.人数>1;

30. 查询 1990 年出生的学生名单

select * from Student 
where year(Sage)=1990;

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

select CId,AVG(score) 平均成绩 from SC 
group by CId order by 平均成绩 desc,CId asc;

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

select S.SId,S.Sname,T.平均成绩 from Student S,
(select SC.SId,AVG(SC.score) 平均成绩 from SC group by SC.SId having 平均成绩 > 85) T 
where S.SId = T.SId;

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

select S.Sname,SC.score from Student S, SC, Course C 
where S.SId = SC.SId and SC.score < 60 and SC.CId = C.CId and C.Cname = '数学';

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

select S.SId,sc.CId,SC.score 
from Student S left join SC on S.SId = SC.SId;

35. 查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数

select S.Sname,C.Cname,SC.score from Student S,Course C,SC 
where SC.score>=70 and S.SId = SC.SId and SC.CId=C.CId;

36. 查询不及格的课程

select DISTINCT CId from SC where score <60;

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

select SC.SId,S.Sname from Student S,SC 
where SC.CId='01'and S.SId=SC.SId and SC.score>80;

38. 求每门课程的学生人数

select CId,count(SId) 人数 from SC group by CId;

39. 成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩

select S.*,SC.score from Student S,Course C,Teacher T,SC 
where C.CId=SC.CId and C.TId=T.TId and T.Tname='张三' 
and S.SId =SC.SId order by SC.score desc limit 1;

40. 成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩

select * from (select SC.SId, dense_rank()over(partition by SC.CId order by SC.score desc) 排名 from SC 
where SC.CId in(select C.CId from Course C, Teacher T where C.TId = T.TId and T.Tname = '张三')) T1
where T1.排名 = 1;

41. 查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩

select * from SC T1 
where exists(select * from SC T2 where T1.SId=T2.SId and T1.CId!=T2.CId and T1.score =T2.score );

42. 查询每门功成绩最好的前两名

select S.* from Student S 
inner join(select SC.SId, row_number()over (partition by SC.CId order by SC.score) 排名 from SC)T 
where T.排名 < 3 and S.SId = T.SId;

43. 统计每门课程的学生选修人数(超过 5 人的课程才统计)

select CId, count(SId) from SC group by CId having count(SId) > 5;

44. 检索至少选修两门课程的学生学号

select SId from SC group by SId having count(CId) >= 2;

45. 查询选修了全部课程的学生信息

select S.* from Student S 
where S.SId in (select SId from SC group by SId having count(CId) = 3);

46. 查询各学生的年龄,只按年份来算

select SId,Sname,TIMESTAMPDIFF(YEAR,Sage,now())年龄 from student;

47. 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一

select SId,Sname,(DATE_FORMAT(NOW(),'%Y')-DATE_FORMAT(Sage,'%Y') 
-(case when DATE_FORMAT(NOW(),'%m%d')>DATE_FORMAT(Sage,'%m%d') then 0 else 1 end)) 年龄 
from student;

48. 查询本周过生日的学生

select * from Student  where WEEK(Sage)=WEEK(now());

49. 查询下周过生日的学生

select * from Student  where WEEK(Sage)=WEEK(now()) + 1;

50. 查询本月过生日的学生

select * from Student  where month(Sage)=month(now());

51. 查询下月过生日的学生

select * from Student  where month(Sage)=month(now()) + 1;

猜你喜欢

转载自blog.csdn.net/d345389812/article/details/80683619