侧边栏壁纸
博主头像
小鱼吃猫博客博主等级

你所热爱的,便是你的生活。

  • 累计撰写 115 篇文章
  • 累计创建 47 个标签
  • 累计收到 14 条评论

目 录CONTENT

文章目录

MySQL经典练习题及答案,常用SQL语句练习50题

小鱼吃猫
2023-11-02 / 0 评论 / 2 点赞 / 42 阅读 / 12449 字

MySQL经典练习题及答案,常用SQL语句练习50题,题目来源
https://www.cnblogs.com/Diyo/p/11424844.html

环境准备

  • 建表语句

#–1.学生表 
#Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别
CREATE TABLE `Student` (
    `s_id` VARCHAR(20),
    s_name VARCHAR(20) NOT NULL DEFAULT '',
    s_brith VARCHAR(20) NOT NULL DEFAULT '',
    s_sex VARCHAR(10) NOT NULL DEFAULT '',
    PRIMARY KEY(s_id)
);

#–2.课程表 
#Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号 
create table Course(
    c_id varchar(20),
    c_name VARCHAR(20) not null DEFAULT '',
    t_id VARCHAR(20) NOT NULL,
    PRIMARY KEY(c_id)
);

/*
–3.教师表 
Teacher(t_id,t_name) –教师编号,教师姓名 
*/
CREATE TABLE Teacher(
    t_id VARCHAR(20),
    t_name VARCHAR(20) NOT NULL DEFAULT '',
    PRIMARY KEY(t_id)
);

/*
–4.成绩表 
Score(s_id,c_id,s_score) –学生编号,课程编号,分数
*/
Create table Score(
    s_id VARCHAR(20),
    c_id VARCHAR(20) not null default '',
    s_score INT(3),
    primary key(`s_id`,`c_id`)
);
  • 插入数据

#--插入学生表测试数据
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('08' , '王菊' , '1990-01-20' , '女');

#--课程表测试数据
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');

#--教师表测试数据
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');

#--成绩表测试数据
insert into Score values('01' , '01' , 80);
insert into Score values('01' , '02' , 90);
insert into Score values('01' , '03' , 99);
insert into Score values('02' , '01' , 70);
insert into Score values('02' , '02' , 60);
insert into Score values('02' , '03' , 80);
insert into Score values('03' , '01' , 80);
insert into Score values('03' , '02' , 80);
insert into Score values('03' , '03' , 80);
insert into Score values('04' , '01' , 50);
insert into Score values('04' , '02' , 30);
insert into Score values('04' , '03' , 20);
insert into Score values('05' , '01' , 76);
insert into Score values('05' , '02' , 87);
insert into Score values('06' , '01' , 31);
insert into Score values('06' , '03' , 34);
insert into Score values('07' , '02' , 89);
insert into Score values('07' , '03' , 98);

数据预览

  • 学生表
s_ids_names_bins_sex
01赵雷1990-01-01
02钱电1990-12-21
03孙风1990-05-20
04李云1990-08-06
05周梅1991-12-01
06吴兰1992-03-01
07郑竹1989-07-01
08王菊1990-01-20
  • 课程表
c_idc_namet_id
01语文02
02数学01
03英语03
  • 教师表
t_idt_name
01张三
02李四
03王五
  • 成绩表
s_idc_ids_score
010180
010290
010399
020170
020260
020380
030180
030280
030380
040150
040230
040320
050176
050287
060131
060334
070289
070398

题目解析

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

select s.*,
       a.s_score as score_01,
       b.s_score as score_02
from score a,
     score b,
     student s
where a.s_id = b.s_id
  and a.c_id = '01'
  and b.c_id = '02'
  and a.s_score > b.s_score
  and s.s_id = a.s_id;

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

select s.*,
       a.s_score as score_01,
       b.s_score as score_02
from score a,
     score b,
     student s
where a.s_id = b.s_id
  and a.c_id = '01'
  and b.c_id = '02'
  and a.s_score < b.s_score
  and s.s_id = a.s_id;

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

select stu.*, avg(s_score) as avg_score
from score sc,
     student stu
where sc.s_id = stu.s_id
group by s_id
having avg(s_score) >= 60;

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

select stu.*, IFNULL(avg(s_score), 0) as avg_score
from student stu
    left join score sc
    on sc.s_id = stu.s_id
group by s_id
having IFNULL(avg(s_score), 0) < 60;

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

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

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

select count(1)
from teacher
where t_name like '李%';

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

select stu.*,c.c_name,t.t_name
from student stu,
    course c,
     score sc,
     teacher t
where c.c_id = sc.c_id
  and t.t_id = c.t_id
  and sc.s_id = stu.s_id
  and t.t_name = '张三';

8. 查询没学过"张三"老师授课的同学的信息
这个题目有个小技巧,就是用not in

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

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

select stu.*
from score a,
     score b,
     student stu
where a.c_id = '01'
  and b.c_id = '02'
  and a.s_id = b.s_id
  and stu.s_id = a.s_id;

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

# 长型变整型
select stu.* 
from (select sc.s_id,
             max(case when sc.c_id = '01' then s_score end) as score_01,
             max(case when sc.c_id = '02' then s_score end) as score_02,
             max(case when sc.c_id = '03' then s_score end) as score_03
      from score sc
      group by s_id) t,
     student stu
where t.score_01 is not null
  and t.score_02 is null
  and t.s_id = stu.s_id;

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

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

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 c_id from score where s_id = '01');

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


select *
from student
where s_id not in 
    (select s_id
    from (select t.*, sc.c_id cid2
         from (select *
               from student
                  , (select c_id from score where s_id = '01') t) t
                  left join score sc
                            on t.s_id = sc.s_id
                                and t.c_id = sc.c_id
         union
         select t.*, sc.c_id cid2
         from (select *
               from student
                  , (select c_id from score where s_id = '01') t) t
                  right join score sc
                             on t.s_id = sc.s_id
                                 and t.c_id = sc.c_id) tt
    where cid2 is null
    group by s_id)
and s_id != '01';

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

select stu.*
from student stu
where s_id not in 
  (select s_id
   from score
   where score.c_id in 
      (select c_id
      from course
      where t_id = (select t_id from teacher where t_name = '张三'))
      group by s_id);

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

select stu.s_id, stu.s_name, ifnull(avg(score.s_score), 0)
from student stu
    left join score
       on stu.s_id = score.s_id
group by stu.s_id
having sum(case when score.s_score >= 60 then 0 else 1 end) >= 2

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

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

18. 查询各科成绩最高分. 最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率

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. 查询不及格的课程

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

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

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

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

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

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

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

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

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

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

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

2

评论区