mysql经典50题(练习)

1.5号oracle50题链接在此

mysql经典50题(练习)

  • mysql经典50题(练习)
    • 创建数据库和表
      • 数据库
      • 学生表 student
        • 创建学生表 student
        • 插入学生数据
      • 课程表 course
        • 创建课程表 course
        • 插入课程数据
      • 教师表 teacher
        • 创建教师表 teacher
        • 插入教师数据
      • 成绩表 score
        • 创建成绩表 score
        • 插入成绩数据
    • 经典练习 50 题
      • 1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
      • 2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
      • 3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
      • 4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩
      • 5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
      • 6、查询"李"姓老师的数量
      • 7、询学过"张三"老师授课的同学的信息
      • 8、查询没学过"张三"老师授课的同学的信息
      • 9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
      • 10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
      • 11、查询没有学全所有课程的同学的信息
      • 12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
      • 13、查询和"01"号的同学学习的课程完全相同的其他同学的信息
      • 14、查询没学过"张三"老师讲授的任一门课程的学生姓名
      • 15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
      • 16、检索"01"课程分数小于60,按分数降序排列的学生信息
      • 17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
      • 18、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率,及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
      • 19、按各科成绩进行排序,并显示排名 开窗函数不会
      • 20、查询学生的总成绩并进行排名
      • 21、查询不同老师所教不同课程平均分从高到低显示
      • 22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
      • 23、统计各科成绩各分数段人数:课程编号,课程名称, 100-85 , 85-70 , 70-60 , 0-60 及所占百分比
      • 24、查询学生平均成绩及其名次
      • 25、查询各科成绩前三名的记录
      • 26、查询每门课程被选修的学生数
      • 27、查询出只有两门课程的全部学生的学号和姓名
      • 28、查询男生、女生人数
      • 29、查询名字中含有"风"字的学生信息
      • 30、查询同名同性学生名单,并统计同名人数
      • 31、查询1990年出生的学生名单
      • 32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
      • 33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
      • 34、查询课程名称为"数学",且分数低于60的学生姓名和分数
      • 35、查询所有学生的课程及分数情况;
      • 36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数;
      • 37、查询课程不及格的学生
      • 38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名;
      • 39、求每门课程的学生人数
      • 40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
      • 41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
      • 42、查询每门功课成绩最好的前两名
      • 43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
      • 44、检索至少选修两门课程的学生学号
      • 45、查询选修了全部课程的学生信息
      • 46、查询各学生的年龄
      • 47、查询本周过生日的学生
      • 48、查询下周过生日的学生
      • 49、查询本月过生日的学生
      • 50、查询下月过生日的学生

mysql经典50题(练习)

创建数据库和表

数据库

drop database if exists mysql_test cascade;
create database mysql_test;
use mysql_test;

学生表 student

创建学生表 student
create table student (
	s_id int,
	s_name varchar(8),
	s_birth date,
	s_sex varchar(4)
);
插入学生数据
insert into student values
(1,'赵雷','1990-01-01','男'),
(2,'钱电','1990-12-21','男'),
(3,'孙风','1990-05-20','男'),
(4,'李云','1990-08-06','男'),
(5,'周梅','1991-12-01','女'),
(6,'吴兰','1992-03-01','女'),
(7,'郑竹','1989-07-01','女'),
(8,'王菊','1990-01-20','女');

课程表 course

创建课程表 course
create table course (
	c_id int,
	c_name varchar(8),
	t_id int
);
插入课程数据
insert into course values
(1,'语文',2),
(2,'数学',1),
(3,'英语',3);

教师表 teacher

创建教师表 teacher
create table teacher (
	t_id int,
	t_name varchar(8)
);
插入教师数据
insert into teacher values
(1,'张三'),
(2,'李四'),
(3,'王五');

成绩表 score

创建成绩表 score
create table score (
	s_id int,
	c_id int,
	s_score int
);
插入成绩数据
insert into score values
(1,1,80),
(1,2,90),
(1,3,99),
(2,1,70),
(2,2,60),
(2,3,65),
(3,1,80),
(3,2,80),
(3,3,80),
(4,1,50),
(4,2,30),
(4,3,40),
(5,1,76),
(5,2,87),
(6,1,31),
(6,3,34),
(7,2,89),
(7,3,98);

mysql经典50题(练习)_第1张图片

经典练习 50 题

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

select stu.*,sc0.s_score from student stu inner join (select sc1.* from score sc1 inner join score sc2 on sc1.s_id
 = sc2.s_id and sc1.c_id = 01 and sc2.c_id = 02 where sc1.s_score > sc2.s_score) sc0
    on stu.s_id = sc0.s_id;

1

2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数

select stu.*,sc0.s_score from student stu inner join (select sc1.* from score sc1 inner join score sc2 on sc1.s_id
 = sc2.s_id and sc1.c_id = 01 and sc2.c_id = 02 where sc1.s_score < sc2.s_score) sc0
    on stu.s_id = sc0.s_id;

2

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

select stu.s_id,stu.s_name,sc.avgscore from student stu
    inner join (select score.s_id,avg(s_score) avgscore from score group by s_id having avgscore >= 60) sc
    on stu.s_id = sc.s_id;

mysql经典50题(练习)_第2张图片

4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩

select stu.s_id,stu.s_name,sc.avgscore from student stu
    inner join (select score.s_id,avg(s_score) avgscore from score
    group by s_id having avgscore < 60) sc
    on stu.s_id = sc.s_id;

4

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

select stu.s_id,stu.s_name,count(sc.c_id),ifnull(sum(sc.s_score),0) sumscore from student stu
    left join score sc on stu.s_id = sc.s_id group by stu.s_id, stu.s_name;

mysql经典50题(练习)_第3张图片

6、查询"李"姓老师的数量

select count(*) 李姓老师的数量 from teacher where t_name like '李%';

6

7、询学过"张三"老师授课的同学的信息

select student.* from student inner join score s on student.s_id = s.s_id
    inner join
    (select teacher.t_id,c_id from teacher left join course c on teacher.t_id = c.t_id
    where t_name = '张三') t on s.c_id = t.c_id;

mysql经典50题(练习)_第4张图片

8、查询没学过"张三"老师授课的同学的信息

select stu.* from student stu where stu.s_id not in
    (select student.s_id from student inner join score s on student.s_id = s.s_id
    inner join
    (select teacher.t_id,c_id from teacher left join course c on teacher.t_id = c.t_id
    where t_name = '张三') t on s.c_id = t.c_id);

8

9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息

select s.* from score sc1 inner join score sc2 on sc1.s_id = sc2.s_id and sc1.c_id = 01
              and sc2.c_id = 02 left join student s on sc1.s_id = s.s_id;

mysql经典50题(练习)_第5张图片

10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息

select * from student where s_id in (select s_id from score where score.c_id = 01)
                        and s_id not in (select s_id from score where score.c_id = 02);

10

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

select stu.*,count(sc.c_id) stucount from score sc right join student stu on sc.s_id = stu.s_id
    group by stu.s_id, s_name, s_birth, s_sex having stucount < (select count(course.c_id) from course);

mysql经典50题(练习)_第6张图片

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

select distinct stu.* from student stu left join score sc on stu.s_id = sc.s_id
    where sc.c_id in(select sc1.c_id from score sc1 where sc1.s_id = 1);

mysql经典50题(练习)_第7张图片

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

select * from student inner join (select sc1.s_id from score sc1 inner join (select c_id from score where s_id = 1) s1
    on sc1.c_id = s1.c_id where s_id != 1
    group by sc1.s_id having count(sc1.c_id) = (select count(score.c_id) from score where s_id = 1)) t
    on student.s_id = t.s_id ;

13

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

select s.* from student s where s.s_id not in (select stu.s_id from student stu
    left join score sc on stu.s_id = sc.s_id
    left join course c on sc.c_id = c.c_id
    left join teacher t on c.t_id = t.t_id where t.t_name = '张三');

14

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

select stu.s_id,stu.s_name,avg(sc.s_score) from student stu
    left join score sc on stu.s_id = sc.s_id where sc.s_score < 60
    group by stu.s_id, stu.s_name having count(sc.c_id) >= 2;

15

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

select * from student stu left join score sc on stu.s_id = sc.s_id where sc.s_score < 60
    and sc.c_id = 1 order by sc.s_score desc;

16

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

select stu.s_name 学生姓名,
        sum(case sc.c_id when 1 then sc.s_score else 0 end ) 语文,
        sum(case sc.c_id when 2 then sc.s_score else 0 end ) 数学,
        sum(case sc.c_id when 3 then sc.s_score else 0 end ) 英语,
        ifnull(avg(sc.s_score),0) avgscore
from student stu left join score sc on stu.s_id = sc.s_id
group by stu.s_name order by avgscore desc ;

mysql经典50题(练习)_第8张图片

18、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率,及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90

select c.c_id 课程ID,c_name 课程name,
       max(sc.s_score) 最高分,min(sc.s_score) 最低分,
       avg(sc.s_score) 平均分,
       sum(case when sc.s_score > 60 then 1 else 0 end ) /count(sc.s_id) 及格率,
       sum(case when sc.s_score between 70 and 80 then 1 else 0 end ) /count(sc.s_id) 中等率,
       sum(case when sc.s_score between 80 and 90 then 1 else 0 end ) /count(sc.s_id) 优良率,
       sum(case when sc.s_score >= 90 then 1 else 0 end ) /count(sc.s_id) 优秀率
from score sc left join course c on sc.c_id = c.c_id group by c.c_id, c_name;

18

19、按各科成绩进行排序,并显示排名 开窗函数不会

select score.*,rank() over (partition by score.c_id order by s_score desc)
from score order by score.c_id,s_score desc ;

mysql经典50题(练习)_第9张图片

20、查询学生的总成绩并进行排名

select stu.s_name,sum(sc.s_score) sumscore from student stu
    left join score sc on stu.s_id = sc.s_id
group by stu.s_name,stu.s_id order by sumscore desc;

mysql经典50题(练习)_第10张图片

21、查询不同老师所教不同课程平均分从高到低显示

select t.t_name,c.c_name,avg(sc.s_score) avgscore from score sc
    left join course c on sc.c_id = c.c_id
    left join teacher t on c.t_id = t.t_id
    group by t.t_name, c.c_name order by avgscore desc ;

21

22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩

(select stu.*,sc.c_id,sc.s_score from student stu left join score sc on stu.s_id = sc.s_id
where c_id = 1 order by sc.s_score desc limit 2)
union (select stu.*,sc.c_id,sc.s_score from student stu left join score sc on stu.s_id = sc.s_id
where c_id = 2 order by sc.s_score desc limit 2)
union (select stu.*,sc.c_id,sc.s_score from student stu left join score sc on stu.s_id = sc.s_id
where c_id = 3 order by sc.s_score desc limit 2) ;

mysql经典50题(练习)_第11张图片

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

select sc.c_id 课程编号,c.c_name 课程名称,
       sum(case when s_score between 85 and 100 then 1 else 0 end) '100-85',
       sum(case when s_score between 85 and 100 then 1 else 0 end) /count(sc.s_id)*100 '100-85占比',
       sum(case when s_score between 70 and 85 then 1 else 0 end) '100-85',
       sum(case when s_score between 70 and 85 then 1 else 0 end) /count(sc.s_id)*100 '85-70占比',
       sum(case when s_score between 60 and 70 then 1 else 0 end) '100-85',
       sum(case when s_score between 60 and 70 then 1 else 0 end) /count(sc.s_id)*100 '70-60占比',
       sum(case when s_score between 0 and 60 then 1 else 0 end) '100-85',
       sum(case when s_score between 0 and 60 then 1 else 0 end) /count(sc.s_id)*100 '0-60占比'
       from score sc left join course c on sc.c_id = c.c_id group by sc.c_id, c.c_name;

23

24、查询学生平均成绩及其名次

select sc.s_id,stu.s_name,avg(sc.s_score) avgscore,
       rank() over (order by avg(sc.s_score) desc) 排名 from score sc
    left join student stu on sc.s_id = stu.s_id
    group by stu.s_name,sc.s_id order by avgscore desc ;

mysql经典50题(练习)_第12张图片

25、查询各科成绩前三名的记录

(select sc.s_id,stu.s_name,c.c_name,sc.s_score from score sc
    left join student stu on sc.s_id = stu.s_id
    left join course c on sc.c_id = c.c_id where c.c_id = 1
    group by sc.s_id, stu.s_name, c.c_name, sc.s_score order by sc.s_score desc limit 3)
union (select sc.s_id,stu.s_name,c.c_name,sc.s_score from score sc
    left join student stu on sc.s_id = stu.s_id
    left join course c on sc.c_id = c.c_id where c.c_id = 2
    group by sc.s_id, stu.s_name, c.c_name, sc.s_score order by sc.s_score desc limit 3)
union (select sc.s_id,stu.s_name,c.c_name,sc.s_score from score sc
    left join student stu on sc.s_id = stu.s_id
    left join course c on sc.c_id = c.c_id where c.c_id = 3
    group by sc.s_id, stu.s_name, c.c_name, sc.s_score order by sc.s_score desc limit 3);

mysql经典50题(练习)_第13张图片

26、查询每门课程被选修的学生数

select c.c_name,count(sc.c_id) 学生数 from score sc
    left join course c on sc.c_id = c.c_id group by c.c_name;

26

27、查询出只有两门课程的全部学生的学号和姓名

select stu.s_id,stu.s_name from score sc
    left join student stu on sc.s_id = stu.s_id 
group by stu.s_id, stu.s_name having count(sc.c_id) = 2;

27

28、查询男生、女生人数

select s_sex,count(*) 人数 from student group by s_sex;

28

29、查询名字中含有"风"字的学生信息

select * from student where s_name like '%风%';

29

30、查询同名同性学生名单,并统计同名人数

select count(*) 同名同性学生人数 from student stu1
    join student stu2 on stu1.s_id=stu2.s_id and stu1.s_sex = stu2.s_sex
    and stu1.s_id != stu2.s_id;

30

31、查询1990年出生的学生名单

select * from student where s_birth like '%1990%';

mysql经典50题(练习)_第14张图片

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

select sc.c_id,c.c_name, avg(sc.s_score) avgscore from score sc
left join course c on sc.c_id = c.c_id
group by sc.c_id,c.c_name order by avgscore desc ,sc.c_id asc;

32

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

select stu.s_id, s_name, avg(sc.s_score) avgscore from student stu
    left join score sc on stu.s_id = sc.s_id
    group by stu.s_id, s_name having avgscore >=85;

33

34、查询课程名称为"数学",且分数低于60的学生姓名和分数

select stu.s_name,sc.s_score from student stu
    left join score sc on stu.s_id = sc.s_id
    left join course c on sc.c_id = c.c_id
    where c_name = '数学' and s_score < 60;

34

35、查询所有学生的课程及分数情况;

select stu.s_id,s_name,
       sum(case sc.c_id when 1 then sc.s_score end ) 语文,
       sum(case sc.c_id when 2 then sc.s_score end ) 数学,
       sum(case sc.c_id when 3 then sc.s_score end ) 英语
    from student stu
    left join score sc on stu.s_id = sc.s_id
    left join course c on sc.c_id = c.c_id
    group by stu.s_id, s_name;

mysql经典50题(练习)_第15张图片

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

select stu.s_name,c.c_name,sc.s_score from student stu
    left join score sc on stu.s_id = sc.s_id
    left join course c on sc.c_id = c.c_id
    where s_score > 70;

mysql经典50题(练习)_第16张图片

37、查询课程不及格的学生

select stu.s_name,c.c_name,sc.s_score from student stu
    left join score sc on stu.s_id = sc.s_id
    left join course c on sc.c_id = c.c_id
    where s_score > 70;

mysql经典50题(练习)_第17张图片

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

select stu.s_id,stu.s_name from student stu
    left join score sc on stu.s_id = sc.s_id
    where sc.c_id = 1 and sc.s_score >= 80;

38

39、求每门课程的学生人数

select c.c_name,count(sc.s_id) 学生人数 from score sc
    left join course c on sc.c_id = c.c_id
    group by c.c_name;

39

40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩

select stu.*,select1.maxstu from student stu
    left join score sc on stu.s_id = sc.s_id
    left join course c on sc.c_id = c.c_id
    left join
    (select c.c_id,max(s_score) maxstu from student stu
    left join score sc on stu.s_id = sc.s_id
    left join course c on sc.c_id = c.c_id
    left join teacher t on c.t_id = t.t_id
    where t.t_name = '张三' group by c.c_id) select1 on c.c_id = select1.c_id
    where sc.s_score = select1.maxstu;

40

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

select sc1.s_id, sc1.c_id, sc1.s_score from score sc1 join score sc2
    where sc1.s_score = sc2.s_score and sc1.s_id != sc2.s_id and sc1.c_id != sc2.c_id;

mysql经典50题(练习)_第18张图片

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

(select stu.s_id,stu.s_name,c.c_name,sc.s_score from student stu
    left join score sc on stu.s_id = sc.s_id
    left join course c on sc.c_id = c.c_id where sc.c_id = 1
    group by stu.s_id, stu.s_name, c.c_name, sc.s_score
    having max(sc.s_score) order by sc.s_score desc limit 2)
union (select stu.s_id,stu.s_name,c.c_name,sc.s_score from student stu
    left join score sc on stu.s_id = sc.s_id
    left join course c on sc.c_id = c.c_id where sc.c_id = 2
    group by stu.s_id, stu.s_name, c.c_name, sc.s_score
    having max(sc.s_score) order by sc.s_score desc limit 2)
union (select stu.s_id,stu.s_name,c.c_name,sc.s_score from student stu
    left join score sc on stu.s_id = sc.s_id
    left join course c on sc.c_id = c.c_id where sc.c_id = 3
    group by stu.s_id, stu.s_name, c.c_name, sc.s_score
    having max(sc.s_score) order by sc.s_score desc limit 2);

mysql经典50题(练习)_第19张图片

43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列

select sc.c_id,count(sc.s_id) 选修人数 from score sc
    group by sc.c_id  having 选修人数 > 5 order by 选修人数 desc ,sc.c_id asc;

43

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

select s_id from score group by s_id having count(score.c_id) >= 2;

mysql经典50题(练习)_第20张图片

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

select stu.* from student stu
    left join score sc on stu.s_id = sc.s_id
    group by stu.s_id,s_name,s_birth,s_sex
    having count(sc.c_id) = (select count(*) from course);

mysql经典50题(练习)_第21张图片

46、查询各学生的年龄

select s_name,year(now())-year(s_birth) 年龄  from student

mysql经典50题(练习)_第22张图片

47、查询本周过生日的学生

select s_name from student where week(s_birth) = week(now());

47
没有本周过生日的学生

48、查询下周过生日的学生

select s_name from student where week(s_birth) = week(now()) + 1;

48

49、查询本月过生日的学生

select * from student where month(s_birth) = month(now());

49

50、查询下月过生日的学生

select * from student where month(s_birth) = month(now()) -11;

50

你可能感兴趣的:(SQL,#,Mysql,mysql,数据库,java)