Mysql的40道练习题

前期准备

由于我使用的是win10系统,所以提前把一些要注意的点给标注出来;
在windows下因为windows使用的默认编码问题,会导致你如果是在cmd下操作sql命令,到了mysql-fron查看时中文会出行乱码现象。因为我们需要提前输入以下命令:(在创建表的时候需要声明utf8编码)


set names 'gbk' ;

创建数据表

一共有 张表:学生表,课程表,教师表,成绩表
1. 学生表

create table Student(SId varchar(10),Sname varchar(10),Sage datetime,Ssex varchar(10),PRIMARY key(SId))charset=utf8;
insert into Student values('01' , '赵雷' , '1990-01-01' , '男');
insert into Student values('02' , '钱电' , '1990-12-21' , '男');
insert into Student values('03' , '孙风' , '1990-12-20' , '男');
insert into Student values('04' , '李云' , '1990-12-06' , '男');
insert into Student values('05' , '周梅' , '1991-12-01' , '女');
insert into Student values('06' , '吴兰' , '1992-01-01' , '女');
insert into Student values('07' , '郑竹' , '1989-01-01' , '女');
insert into Student values('09' , '张三' , '2017-12-20' , '女');
insert into Student values('10' , '李四' , '2017-12-25' , '女');
insert into Student values('11' , '李四' , '2012-06-06' , '女');
insert into Student values('12' , '赵六' , '2013-06-13' , '女');
insert into Student values('13' , '孙七' , '2014-06-01' , '女');

2. 课程表

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

3. 教师表

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

4. 成绩表

create table SC(skey int ,SId varchar(10),CId varchar(10),score decimal(18,1),PRIMARY key(skey))charset=utf8;
insert into SC values('1','01' , '01' , 80);
insert into SC values('2','01' , '02' , 90);
insert into SC values('3','01' , '03' , 99);
insert into SC values('4','02' , '01' , 70);
insert into SC values('5','02' , '02' , 60);
insert into SC values('6','02' , '03' , 80);
insert into SC values('7','03' , '01' , 80);
insert into SC values('8','03' , '02' , 80);
insert into SC values('9','03' , '03' , 80);
insert into SC values('10','04' , '01' , 50);
insert into SC values('11','04' , '02' , 30);
insert into SC values('12','04' , '03' , 20);
insert into SC values('13','05' , '01' , 76);
insert into SC values('14','05' , '02' , 87);
insert into SC values('15','06' , '01' , 31);
insert into SC values('16','06' , '03' , 34);
insert into SC values('17','07' , '02' , 89);
insert into SC values('18','07' , '03' , 98);
insert into SC values('19','08' , '01' , 88);

题目

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

select Course.cname'课程名称',count(*)'人数' from SC,Course 
where SC.CId=Course.CId
GROUP BY SC.CId

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

select student.sid,student.sname from student,sc 
where student.sid = sc.sid 
and sc.cid = '01' 
and sc.score >= 80;

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

select course.cname,count(*) from sc,course where course.cid=sc.cid 
group by course.cid 
having count(*) > 5;

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

select sc.sid from sc 
group by sid 
having count(cid) > 2;

5. 选修了全部课程的学生信息

select student.* from student, sc 
where sc.sid = student.sid 
group by student.sid
having count(cid) = (select count(*) from course);

6. 查询存在不及格的课程

select DISTINCT course.cname from course,sc 
where sc.cid = course.cid
and sc.score < 60;

DISTINCT(仅列出不同的值)

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

select student.sname, course.cname, sc.score from student, course, sc
where student.sid = sc.sid and course.cid = sc.cid and sc.score > 70;

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

select student.sname, course.cname, sc.score from student
left join sc on sc.sid = student.sid
left join course on course.cid = sc.cid;

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

select student.sname, sc.score from student, course, sc
where student.sid = sc.sid
and course.cid = sc.cid
and sc.score < 60
and course.cname = '数学';

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

select student.sid, student.sname, avg(sc.score) from student, sc
where student.sid = sc.sid
group by student.sid
having avg(sc.score) >= 85;

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

select cid, avg(score) from sc
group by cid
order by avg(score) desc, cid asc;

(desc为降序,asc为升序)

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

以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率

及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90

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

select sc.cid, course.cname, max(sc.score), min(sc.score), avg(sc.score), count(sc.sid),
sum(case when score < 60 then 1 else 0 end)/count(sc.cid) '合格率',
sum(case when score < 80 and score >= 60 then 1 else 0 end)/count(sc.cid) '中等率',
sum(case when score >= 90 then 1 else 0 end)/count(sc.cid) '优秀率'
from sc, course
where sc.cid = course.cid
group by sc.cid;

13. 查询男生、女生人数

select ssex, count(*) from student 
group by ssex;

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

select student.* from student, sc
where student.sid = sc.sid
and sc.cid = '01'
and sc.score < 60
order by sc.score desc; 

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

select sc.sid, sc.score,  t.ascore from sc
right join (select sid,avg(score) ascore from sc group by sid) t on sc.sid = t.sid
order by t.ascore desc; 

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

select student.sname from student where student.sid not in
(select distinct student.sname from student, teacher, course, sc
where student.sid = sc.sid
and course.cid = sc.cid
and teacher.tid = course.tid
and teacher.tname = "张三");

not in 不在某个区间内

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

select student.*, sc.score from student, sc, teacher, course
where student.sid = sc.sid
and sc.cid = course.cid
and course.tid = teacher.tid
and teacher.tname = "张三"
order by sc.score desc
limit 1;

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

UPDATE sc SET score=90
where skey=17

select student.*, sc.score from student, sc, course, teacher
where student.sid = sc.sid
and sc.cid = course.cid
and course.tid = teacher.tid
and teacher.tname = "张三"
and sc.score = (
    select max(sc.score) from student, sc, course, teacher
    where student.sid = sc.sid
    and sc.cid = course.cid
    and course.tid = teacher.tid
    and teacher.tname = "张三"
);

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

select a.sid, a.cid, a.score from sc as a
inner join 
sc as b
on a.sid = b.sid
and a.cid != b.cid
and a.score = b.score
GROUP BY a.cid, b.sid; 

20. 查询每门功成绩最好的前两名
方法一

select a.sid,a.cid,a.score from sc a 
left join sc b on a.cid = b.cid
and a.score < b.score
group by a.cid,a.sid
having count(b.score)<2
order by a.cid;

方法二

select a.cid,a.sid,a.score from sc as a
where
(select count(1) from sc as b where
a.cid=b.cid and b.score >= a.score)<=2
order by a.cid,a.score desc;

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

select course.cname,count(*) from course,sc 
where course.cid = sc.cid
group by sc.cid;

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

select student.sid,student.sname from student,sc
where student.sid = sc.sid
group by sid
having count(*) = 2;

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

select student.sname, count(*) from student
group by student.sname
having count(*) > 1;

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

select * from student 
where year(sage)=1990;

25. 查询各学生的年龄

select student.sid, student.sname, TIMESTAMPDIFF(year,sage,CURDATE()) '年龄' from student;

timestampdiff(参数1,参数2,参数3)。参数1 可以选择小时hour,秒second,月month,年year。参与2,比较的时间数据(小的哪一个),参数3,比较的时间数据cutdate()用来返回今天的时间(大的那一个)。

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

select * from student
where(curdate()) = week(sage);

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

select * from student
where(curdate()) = month(sage);

28. 查询「李」姓老师的数量

select count(*) from teacher
where tname like '李%';

29. 查有成绩的学生信息
方法一:

select * from student
where sid in (select sc.sid from sc); 

方法二:

select student.* from student,sc
where sc.sid = student.sid
group by sc.sid;

30. 查询所有同学的学生编号、学生姓名、选课总数、所有课程的成绩总和

select student.sid, student.sname, count(sc.cid),sum(sc.score)
from student
left join sc on student.sid = sc.sid
group by student.sid;

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

select * from student
where sid in (select sid from sc);

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

select student.sid,student.sname,avg(sc.score)
from student,sc
where student.sid = sc.sid
group by student.sid
having avg(sc.score) >= 60;

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

select * from sc where cid = "02" 
and sid not in(
    select sid from sc where cid = "01"
);

34. 查询存在" 01 "课程但可能不存在" 02 "课程的情况

select * from sc where cid = "01";

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

select a.cid, a.sid, a.score, count(b.sid)+1
from sc a left join sc b on a.cid = b.cid
and a.score < b.score
group by a.cid, a.sid
order by a.cid, count(b.sid)+1;

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

select student.*, a.score, b.score 
from student,
(select score,sid from sc where cid = "01") a,
(select score,sid from sc where cid = "02") b
where a.sid = b.sid
and b.sid = student.sid
and a.score > b.score;

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

select student.* from student, teacher, course, sc
where student.sid = sc.sid
and sc.cid = course.cid
and course.tid = teacher.tid
and teacher.tname = "张三";

(考察的是多表联查的运用)

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

select student.* from student
left join sc on student.sid = sc.sid
group by student.sid
having count(sc.cid) < (select count(*) from course);

的方法是❌的,查询出的同学也只是成绩表中有登记过成绩的学生中没学全课程的学生
✔ 的写法应该是这个

select student.* from student
left join sc on sc.sid = student.sid
group by student.sid
having count(sc.cid) < (select count(*) from course);

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

select student.* from student, sc
where student.sid = sc.sid
and sc.cid in (select cid from sc where sid = "01")
group by student.sid;

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

select * from Student where sid in
(select  sid from sc where sid not in 
(select  sid from sc where  cid not in 
(select cid from sc where sid ='01'))
GROUP BY sid 
having count(*)=(select count(cid) from sc where sid ='01')and sid<>'01')

你可能感兴趣的:(Mysql的40道练习题)