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

#1.1查询01课程与02课程的课程表:
select student_id, score as c1_score from score where course_id='01';
select student_id, score as c2_score from score where course_id='02'; #1.2使用内连接,将两个课程表进行连接,并从中选出01
select c1.student_id,c1_score,c2_score from
(select student_id, score as c1_score from score where course_id='01') as c1 inner join
(select student_id, score as c2_score from score where course_id='02') as c2
on c1.student_id=c2.student_id
where c1_score>c2_score;

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

select c1.student_id,c1_score,c2_score from
(select student_id, score as c1_score from score where course_id='01') c1 inner join
(select student_id, score as c2_score from score where course_id='02') c2
on c1.student_id=c2.student_id; select c1.student_id, c1.score, c2.score
from score c1 join score c2 on c1.student_id=c2.student_id
where c1.course_id='01' and c2.course_id='02';

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

select c1.student_id,c1_score,c2_score from
(select student_id, score as c1_score from score where course_id='01') c1 left join
(select student_id, score as c2_score from score where course_id='02') c2
on c1.student_id=c2.student_id;

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

select * from
(select student_id, score as c1_score from score where course_id='01') c1 right join
(select student_id, score as c2_score from score where course_id='02') c2
on c1.student_id=c2.student_id
where c1.student_id is null;

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

select student.id,name,avg(score) from student left join score on student.id=student_id
group by student_id
having avg(score)>=60;

6、查询在score表存在成绩的学生信息

select distinct student.* from student right join score on id=student_id;

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

select id, name, count(course_id),sum(score) from student left join score on id=student_id
group by id;

8、查有成绩的学生信息

select distinct student.* from score left join student on id=student_id;

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

select count(id) from teacher
where name like '李%';

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

select student.* from student
left join score on student.id=student_id
left join course on course_id=course.id
left join teacher on teacher_id=teacher.id
where teacher.name='张三';

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

select student.* from student left join
(select student_id, count(course_id) as courseNum from score group by student_id)
as studentCourse on student.id=studentCourse.student_id
where courseNum<(select count(id) as allNum from course);

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

select distinct student.* from student
left join score on student.id=student_id
right join (select course_id from score where student_id='01') as No01Course on score.course_id=No01Course.course_id
where student.id!='01';

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

#和01选了完全相同课程的同学,首先选出所选课程是01所选课程的子集的同学,再从中筛选出选课数量与01同学相同的。

#13.1 01同学选的所有课程:
select course_id from score where student_id='07'; #13.2 选了01没选的课的同学
select student_id from score
where course_id not in
(select course_id from score where student_id='07'); #13.3 排除掉这些人,就是01选课列表的子集的同学
select student_id from score
where student_id!='07' and student_id not in
(select student_id from score
where course_id not in
(select course_id from score where student_id='07')); #13.4 选出(01选课列表的子集的同学)选课数量与01选课数量相同的人
select student_id from
(select student_id from score
where student_id!='07' and student_id not in
(select student_id from score
where course_id not in
(select course_id from score where student_id='07')
)
) as subset
group by student_id
having count(*)=
(select count(course_id) from score where student_id='07');

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

#14.1 查询学过张三教过的课的同学:
select student.id from student
left join score on student.id=student_id
left join course on course_id=course.id
left join teacher on teacher_id=teacher.id
where teacher.name='张三'; #14.2 排除这些同学
select name from student
where id not in (
select student.id from student
left join score on student.id=student_id
left join course on course_id=course.id
left join teacher on teacher_id=teacher.id
where teacher.name='张三'
);

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

#15.1 选出不及格的课程和同学
select student_id from score
where score<60; #15.2 从中选出count>=2的学生id
select student_id from
(select student_id, score from score
where score<60
) as sc
group by student_id
having count(score)>=2; #15.3 从中选出学号姓名和平均成绩
select student.id, student.name, avg(score) from
student left join score on student.id=student_id
where student.id in (
select student_id from
(select student_id, score from score
where score<60
) as sc
group by student_id
having count(score)>=2
)
group by student.id;

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

select student.* from
student left join score on student.id=student_id
where course_id='01' and score<60
order by score desc;

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

#选出所有同学各自的平均成绩
select student_id, avg(score) from score
group by student_id; #连接至学生-成绩表
select student.id, student.name, score, avg_score from
(student left join score on student.id=student_id) left join
(select student_id, avg(score) as avg_score from score
group by student_id
) as avgsc on student.id=avgsc.student_id
order by avg_score desc;

MySQL经典50题的更多相关文章

  1. 【不断更新】mysql经典50道题自我练习

    mysql经典50道题自我练习 测试数据和练习题均转载自CSDN博主@启明星的指引的文章sql语句练习50题(Mysql版),用于mysql的每日自我练习 表名和字段 –1.学生表 Student(s ...

  2. 转:sql 经典50题--可能是你见过的最全解析

    题记:从知乎上看到的一篇文章,刚好最近工作中发现遇到的题目与这个几乎一样,可能就是从这里来的吧.^_^ 里面的答案没有细看,SQL求解重在思路,很多时候同一种结果可能有多种写法,比如题中的各科成绩取前 ...

  3. MySQL练习50题

    介绍一个学习SQL的网站:https://sqlbolt.com/ 习题来源于网络,SQL语句是自己的练习答案,部分参考了网络上的答案. 花了一晚上的时间做完,个人认为其中的难点有:分组提取前几名的数 ...

  4. 小菜菜mysql练习50题解析——数据准备

    附上数据准备: 学生表 create table Student(SId varchar(10),Sname varchar(10),Sage datetime,Ssex varchar(10)); ...

  5. sql语句练习50题(Mysql版-详加注释)

    表名和字段 1.学生表       Student(s_id,s_name,s_birth,s_sex) --学生编号,学生姓名, 出生年月,学生性别 2.课程表       Course(c_id, ...

  6. JAVA经典算法50题(转)

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/51097928 JAVA经典算法50题 [程序1]   题目:古典问题:有一对兔子, ...

  7. java经典算法题50道

    原文 JAVA经典算法50题[程序1]   题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?1.程序 ...

  8. sql 经典查询50题 思路(一)

    因为需要提高一下sql的查询能力,当然最快的方式就是做一些实际的题目了.选择了这个sql的50题,这次大概做了前10题左右,把思路放上来,也是一个总结. 具体题目见: https://zhuanlan ...

  9. 经典算法题每日演练——第十七题 Dijkstra算法

    原文:经典算法题每日演练--第十七题 Dijkstra算法 或许在生活中,经常会碰到针对某一个问题,在众多的限制条件下,如何去寻找一个最优解?可能大家想到了很多诸如“线性规划”,“动态规划” 这些经典 ...

随机推荐

  1. 详解MySQL索引

    原文链接详解MySQL索引 索引介绍 索引是帮助MySQL高效获取数据的数据结构.在数据之外,数据库系统还维护着一个用来查找数据的数据结构,这些数据结构指向着特定的数据,可以实现高级的查找算法. 本文 ...

  2. 使用 Python 来自动回微信

    准备 Python3 Python Itchat库(可以通过pip install itchat来安装) (可选)Python Pymongo库(可以通过pip install pymongo来安装) ...

  3. 分享我做Dotnet9博客网站时积累的一些资料

    从2019年使用WordPress搭建Dotnet9网站,到现在手撸代码开发,介绍中间使用的一些资源,绝无保留,希望对大家有用. 1. 申请域名.搭建WordPress网站 时间点:2019年11月 ...

  4. Mac 系统用mx master3遇到的问题

    买 master3 之前上网看到的资料都是夸的不行,提到的问题也都是无足轻重,然而就我个人来说,在 Mac 下实际使用 master3 的感受很糟糕,写这篇文章分享一下遇到的问题,如果有想买的人看到这 ...

  5. 微信新菜单类型 article_id 设置教程

    前不久, Senparc.Weixin SDK 跟随微信更新的步伐,上线了新的素材管理接口,其中也涉及到了 article_id 类型的自定义菜单接口. 本文将演示如何使用新的菜单类型. 官方文档传送 ...

  6. MySQL8新增降序索引

    MySQL8新增降序索引 桃花坞里桃花庵,桃花庵里桃花仙.桃花仙人种桃树,又摘桃花卖酒钱. 一.MySQL5.7 降序索引 MySQL在语法上很早就已经支持降序索引,但实际上创建的却仍然是升序索引,如 ...

  7. 关于 MyBatis-Plus 分页查询的探讨 → count 都为 0 了,为什么还要查询记录?

    开心一刻 记得上初中,中午午休的时候,我和哥们躲在厕所里吸烟 听见外面有人进来,哥们猛吸一口,就把烟甩了 进来的是教导主任,问:你们干嘛呢? 哥们鼻孔里一边冒着白烟一边说:我在生气 环境搭建 依赖引入 ...

  8. java高级用法之:绑定CPU的线程Thread-Affinity

    目录 简介 Java Thread Affinity简介 AffinityLock的使用 使用API直接分配CPU 总结 简介 在现代计算机系统中,可以有多个CPU,每个CPU又可以有多核.为了充分利 ...

  9. git rename branch

    git 不能直接重命名远程分支,如果需要重命名则执行以下步骤操作: 重命名本地分支 删除远程分支 推送本地分支(重命名后的)到远程 额外说明: 1. 重命名后的分支也会保留历史 commit(应该是本 ...

  10. net core天马行空系列-微服务篇:全声明式http客户端feign快速接入微服务中心nacos

    1.前言 hi,大家好,我是三合,距离上一篇博客已经过去了整整两年,这两年里,博主通关了<人生>这个游戏里的两大关卡,买房和结婚.最近闲了下来,那么当然要继续写博客了,今天这篇博客的主要内 ...