Mysql41道练习题

1、自行创建测试数据

2、查询“生物”课程比“物理”课程成绩高的所有学生的学号。ps:针对的是自己的生物成绩比物理成绩高,再把符合条件的学生的学号查出来;

复制代码
# 查到 生物 和 物理的 id:
    select course.cid from score left join course on score.course_id = course.cid where course.cname = '生物' or course.cname = '物理' group by course.cid;

    # 将上面的id作为子查询的表 用来查, 所有有 生物 和物理的 表:
    select score.student_id,group_concat(num),count(1) from score where course_id in (select course.cid from score left join course on score.course_id = course.cid where course.cname = '生物' or course.cname = '物理' group by course.cid) group by score.student_id;

    # 获取所有的生物成绩的表
    select * from score where course_id in (select course.cid from score left join course on score.course_id = course.cid where course.cname = '生物' or course.cname = '物理' group by course.cid) limit 0,11;

    # 获取所有的物理成绩的表
    select * from score where course_id in (select course.cid from score left join course on score.course_id = course.cid where course.cname = '生物' or course.cname = '物理' group by course.cid) limit 11,11;

    # 做比较查询:
    select shwu_num.student_id,shwu_num.sw,ty from (select student_id,num as sw from score where course_id in (select course.cid from score left join course on score.course_id = course.cid where course.cname = '生物' or course.cname = '物理' group by course.cid) limit 0,11) as shwu_num left join (select student_id,num as ty from score where course_id in (select course.cid from score left join course on score.course_id = course.cid where course.cname = '生物' or course.cname = '物理' group by course.cid) limit 11,11) as wuli_num on shwu_num.student_id = wuli_num.student_id where shwu_num.sw>wuli_num.ty;
复制代码

 

3、查询平均成绩大于60分的同学的学号和平均成绩;

select student_id,avg(num) from score group by student_id having avg(num) > 60;   #  终于碰到简单了 我的天

 

4、查询所有同学的学号、姓名、选课数、总成绩;

# 将学生表和成绩表关联, 为了获取学生姓名:
    select * from student left join score on student.sid = score.student_id;

    # 将上面的表按照学生姓名分组
    select student.sid,group_concat(student.sname),count(score.course_id),sum(num) from student left join score on student.sid = score.student_id group by student.sid;

 

5、查询姓“李”的老师的个数;

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

 

6、查询没学过“李平”老师课的同学的学号、姓名;

此题和38题 类似, 结果一样:

   select sid, sname from student where sid not in(select student_id from score where course_id in (
        select cid from course left join teacher on course.teacher_id = teacher.tid where teacher.tname = '李平老师') group by student_id)

 

7、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;

  

复制代码
# 等同于 学过生物和物理的学生的学号和姓名 且不需要和 课程连表:

    # 1, 获取所有学过001的表
    select * from score where course_id = 1; 

    # 2, 获取所有学过002的表
    select * from score where course_id = 2; 

    # 3, 两张表关联, 并且找出, 既有001 也有002的表:
    select s1.student_id from (select * from score where course_id = 1) as s1 left join (select * from score where course_id = 2 ) as s2 on s1.student_id = s2.student_id where s1.course_id = 1 and s2.course_id = 2;

    # 4, 用学生表 和上面的表关联 拿到姓名:
    select student.sid, student.sname from student right join (select s1.student_id from (select * from score where course_id = 1) as s1 left join (select * from score where course_id = 2 ) as s2 on s1.student_id = s2.student_id where s1.course_id = 1 and s2.course_id = 2) as s3 on student.sid = s3.student_id;
复制代码

 

8、查询学过“叶平”老师所教的所有课的同学的学号、姓名;

复制代码
# 和第6题 差不多:

    # 获取选择了李平老师所教的课程的 学生id表:
    select student_id from score where course_id in (
        select cid from course left join teacher on course.teacher_id = teacher.tid where teacher.tname = '李平老师') group by student_id;

    # 用学生表 和上面的表 关联,获取学生姓名:
    select student.sid,student.sname from student right join (select student_id from score where course_id in (
        select cid from course left join teacher on course.teacher_id = teacher.tid where teacher.tname = '李平老师') group by student_id) as s1 on student.sid = s1.student_id;
复制代码

 

9、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名;

复制代码
    # 此题需要从第七题的基础上开始写:
    # 1, 获取所有学过001的表
    select * from score where course_id = 1; 

    # 2, 获取所有学过002的表
    select * from score where course_id = 2; 

    # 3, 两张表关联, 并且找出, 既有001 也有002的表:
    select s1.student_id from (select * from score where course_id = 1) as s1 left join (select * from score where course_id = 2 ) as s2 on s1.student_id = s2.student_id where s1.num > s2.num;

    # 4, 用学生表 和上面的表关联 拿到姓名:
    select student.sid, student.sname from student right join (select s1.student_id from (select * from score where course_id = 1) as s1 left join (select * from score where course_id = 2 ) as s2 on s1.student_id = s2.student_id where s1.num > s2.num) as s3 on student.sid = s3.student_id;
复制代码

 

10、查询有课程成绩小于60分的同学的学号、姓名;

    # 1, 从 score表中找到所有小于60分的学生id 并且去重
    select distinct student_id from score where num < 60;

    # 2, 用子查询的方法 从学生表中找到学生id 在上表中的 学生id 和姓名
    select sid,sname from student where sid in (select distinct student_id from score where num < 60);

 

11、查询没有学全所有课的同学的学号、姓名;

    # 1, 从score表中 按照学生id分组后找到 count(course_id) < 4 的学生id的表:
    select student_id from score group by student_id having count(course_id) < 4;

    # 2, 和学生表关联, 找到学生姓名:
    select student.sid,student.sname from student right join (select student_id from score group by student_id having count(course_id) < 4) as s1 on student.sid = s1.student_id;

 

12、查询至少有一门课与学号为“001”的同学所学相同的同学的学号和姓名;

复制代码
    # 1, 从score表中找到student_id = 学号001同学 所学的课程:
    select course_id from score where student_id =1;

    # 2, 从score表中 找到除了001 之外的所有学生 course_id in (上表) 中的所有信息:
    select * from score where student_id != 1 and course_id in (select course_id from score where student_id =1);

    # 3, 上表和学生表 关联, 获取姓名:
    select distinct student.sid, student.sname from student right join (select * from score where student_id != 1 and course_id in (select course_id from score where student_id =1)) as s1 on student.sid = s1.student_id order by student.sid;
复制代码

 

13、查询至少学过学号为“001”同学所选课程中任意一门课的其他同学学号和姓名;

复制代码
    # 我觉的和12题一模一样:

    # 1, 从score表中找到student_id = 学号001同学 所学的课程:
    select course_id from score where student_id =1;

    # 2, 从score表中 找到除了001 之外的所有学生 course_id in (上表) 中的所有信息:
    select * from score where student_id != 1 and course_id in (select course_id from score where student_id =1);

    # 3, 上表和学生表 关联, 获取姓名:
    select distinct student.sid, student.sname from student right join (select * from score where student_id != 1 and course_id in (select course_id from score where student_id =1)) as s1 on student.sid = s1.student_id order by student.sid;
复制代码

 


14、查询和“002”号的同学学习的课程完全相同的其他同学学号和姓名;

复制代码
    # 1, 获取 002 同学的课程id
    select course_id from score where student_id = 2;

    # 2, 获取 002 同学选择的课程的 个数:
    select count(1) from score where student_id = 2 group by student_id;

    # 3, 获取和 002 学的课程数目一样的同学ID:
    select student_id from score group by student_id having count(course_id) = (select count(1) from score where student_id = 2 group by student_id);

    # 4, 获取除了 002 之外所有同学的课程id 在#1表中并且个数一样的 信息:
    select * from score where student_id !=2 and course_id in (select course_id from score where student_id = 2) and student_id = (select student_id from score group by student_id having count(course_id) = (select count(1) from score where student_id = 2 group by student_id));
复制代码

 

15、删除学习“叶平”老师课的SC表记录;

16、向SC表中插入一些记录,这些记录要求符合以下条件:①没有上过编号“002”课程的同学学号;②插入“002”号课程的平均成绩;

复制代码
    # insert 支持语法:
        inset into tb1(xx,xx) select x1,x2 from tb2;

    # 1, 找出没上过 002 课程的同学:
    select student_id from score where student_id not in (select student_id from score where course_id =2) group by student_id;

    # 2, 算出 002 课程的平均值:
    select avg(num) from score where course_id = 2;

    # 3, 插入 这些数据:
    insert into score(student_id, course_id, num) select student_id,2,(select avg(num) from score where course_id = 2) from score where student_id not in (select student_id from score where course_id =2) group by student_id;e_id =2) group by student_id;
复制代码

 

17、按平均成绩从低到高显示所有学生的“语文”、“数学”、“英语”三门的课程成绩,按如下形式显示: 学生ID,语文,数学,英语,有效课程数,有效平均分;

复制代码
# 1,  获取各个科目的成绩:
    select num from score left join course on score.course_id = course.cid where course.cname='生物';

    select num from score left join course on score.course_id = course.cid where course.cname='物理';

    select num from score left join course on score.course_id = course.cid where course.cname='体育';

and sc.student_id = score.student_id
    # 2, 创建表:
    select sc.student_id,(select num from score left join course on score.course_id = course.cid where course.cname='生物' and sc.student_id = score.student_id) as sw, (select num from score left join course on score.course_id = course.cid where course.cname='物理' and sc.student_id = score.student_id) as wl, (select num from score left join course on score.course_id = course.cid where course.cname='体育' and sc.student_id = score.student_id) as ty, avg(sc.num) from score as sc group by sc.student_id desc;
复制代码

 

18、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;

# 1, 创建表:
    select course_id, max(num), min(num) from score group by course_id;

 

19、按各科平均成绩从低到高和及格率的百分数从高到低顺序;

select course_id, avg(num) from score group by course_id order by avg(num) desc;

 

20、课程平均分从高到低显示(现实任课老师);

复制代码
# 在上一题的基础上 接着写:
    # 与老师表连接:
    select * from course right join (select course_id, avg(num) from score group by course_id order by avg(num) desc) as s1 on course.cid = s1.course_id;

    select tname,cname,avgnum from teacher right join (select * from course right join (select course_id, avg(num) as avgnum from score group by course_id order by avgnum desc) as s1 on course.cid = s1.course_id) as s2 on teacher.tid = s2.teacher_id order by avgnum desc;
复制代码

 

21、查询各科成绩前三名的记录:(不考虑成绩并列情况)

 

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

    select course_id,count(1) from score group by course_id;

 

23、查询出只选修了一门课程的全部学生的学号和姓名;

select student_id,count(course_id) from score group by student_id having count(course_id) = 1;

 

24、查询男生、女生的人数;

    select gender,count(1) from student group by gender;

 

25、查询姓“张”的学生名单;

    select * from student where sname like '张%';

 

26、查询同名同姓学生名单,并统计同名人数;

    select * from student as s1 left join student as s2 on s1.sid = s2.sid where s1.sid != s2.sid and s1.sname = s2.sname;

 

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

    select course_id,avg(num) from score group by course_id order by avg(num) asc, course_id desc;

 

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

复制代码
# 1, 查询大于85的 学生 id
    select student_id, avg(num) from score group by student_id having avg(num) >85;

    # 2, 和学生表匹配 拿到名字
    select student.sid, student.sname, s2.avgnum student right join (select student_id, avg(num) as avgnum from score group by student_id having avgnum >85) as s2 on student.sid = s2.student_id;
复制代码

 

29、查询课程名称为“生物”,且分数低于60的学生姓名和分数;

复制代码
    # 1,  找到低于60分的生物;
    select * from score left join course on score.course_id = course.cid where course.cname = '生物' and score.num<60;

    # 2, 和学生表关联 拿到名字:
    select sname,num from student right join (select * from score left join course on score.course_id = course.cid where course.cname = '生物' and score.num<60) as s2 on student.sid = s2.student_id;
 
复制代码

 

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

    # 1, 从score 中找到上面的条件的id
    select * from score where course_id = 3 and num>80;

    # 2, 和学生表关联:
    select student.sid, sname, num from student right join (select * from score where course_id = 3 and num>80) as s2 on student.sid = s2.student_id;

 

31、求选了课程的学生人数

32、查询选修“刘海燕”老师所授课程的学生中,成绩最高的学生姓名及其成绩;

复制代码
# 先把课程表和老师表 关联:
    select course.cid from course left join teacher on course.teacher_id = teacher.tid where teacher.tname = '刘海燕老师';
    # 再把这个虚拟表和成绩表关联
    select * from score right join (select course.cid from course left join teacher on course.teacher_id = teacher.tid where teacher.tname = '刘海燕老师') as x_teacher on score.course_id = x_teacher.cid;
    # 最后在把这个虚表和学生表关联 拿到学生的名字:
    select * from student right join (select * from score right join (select course.cid from course left join teacher on course.teacher_id = teacher.tid where teacher.tname = '刘海燕老师') as x_teacher on score.course_id = x_teacher.cid) as x_tea_sco on student.sid = x_tea_sco.student_id;
    # 按照成绩 逆序 并且只显示一个学生:
    select sname,num from student right join (select * from score right join (select course.cid from course left join teacher on course.teacher_id = teacher.tid where teacher.tname = '刘海燕老师') as x_teacher on score.course_id = x_teacher.cid) as x_tea_sco on student.sid = x_tea_sco.student_id order by num desc limit 0,1;
复制代码

 

33、查询各个课程及相应的选修人数;

    select course_id, count(1) from score group by course_id;

 

34、查询不同课程但成绩相同的学生的学号、课程号、学生成绩;

select s1.student_id, s1.course_id, s2.course_id, s1.num from score as s1, score as s2 where s1.num = s2.num and s1.course_id != s2.course_id order by s1.student_id;

 

35、查询每门课程成绩最好的前两名;

复制代码
# 拿课程表和分数表进行关联:
        select * from course left join score on course.cid = score.course_id;
        # 按照分数降序排序:
        select * from (select * from course left join score on course.cid = score.course_id) as oded_tb order by num desc;
        # 按照分数分组, 查看所有:
        select num, group_concat(cou_sco_tb.cname) from(select * from (select * from course left join score on course.cid = score.course_id) as oded_tb order by num desc) as cou_sco_tb group by cou_sco_tb.num;


###  目前的结果还需要靠自己手动找找 暂时跳过;
复制代码

 

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

select student_id from score group by student_id having count(course_id)>2;


# 注意: 当MySQL 通过语句: set global sql_mode='ONLY_FULL_GROUP_BY'; 之后更改了 SQL的全局模式, 所以在分组以后只能查看当前字段,如果想查看组内信息,需要借助于聚合函数

 

37、查询全部学生都选修的课程的课程号和课程名;

 

38、查询没学过“李平”老师讲授的任一门课程的学生姓名;

复制代码
    # 1, 先关联老师表和课程表, 获取到"李平老师"教的课程的id:
        select * from course,teacher; # 查看着两个表的 笛卡尔积;
        select cid from course left join teacher on course.teacher_id = teacher.tid where teacher.tname = '李平老师';   # 获取李平老师 教的课程id

    # 2, 从score中找出选了李平老师的 学生的id
        select student_id from score where course_id in (
        select cid from course left join teacher on course.teacher_id = teacher.tid where teacher.tname = '李平老师') group by student_id;

    # 3, 从student表中找到不在上面表中的学生的 id和姓名:
        select sid, sname from student where sid not in(select student_id from score where course_id in (
        select cid from course left join teacher on course.teacher_id = teacher.tid where teacher.tname = '李平老师') group by student_id);
复制代码

 

39、查询两门以上不及格课程的同学的学号及其平均成绩;

select student_id, avg(num) from score where num<60 group by student_id having count(student_id)>1;

 

40、检索“004”课程分数小于60,按分数降序排列的同学学号;

select student_id from score where course_id = 4 and num < 60;

 

41、删除“002”同学的“001”课程的成绩;

delete from score where student_id = 2 and course_id = 1;
posted @ 2018-11-19 17:32  九月江  阅读(435)  评论(0编辑  收藏  举报