MySQL必会的28条经典查询

 

原创作品。转载请注明出处https://blog.csdn.net/kk123k

表结构及测试数据请看我上一篇文章:学生选修成绩表测试数据

Student(Sno,Sname,Ssex) 学生表
Teacher(Tno,Tname) 教师表
Course(Cno,Cname,Tno) 选修课程表

SC(Sno,Cno,score) 成绩表

问题:

1、查询课程编号“001”课程比“002”课程成绩高的所有学生的学号

select a.sno from (select sno,score from SC where Cno='001') a,(select sno,score from SC where Cno='002') b
where a.score>b.score and a.sno=b.sno;

2、查询没学过“叶平”老师课的同学的学号、姓名

select sno,sname from student where sno not in
(select distinct sc.sno from sc,course,teacher where sc.cno=course.cno and course.tno=teacher.tno and teacher.tname='叶平')

select sno,sname from student where not exists
(select sc.* from sc,course,teacher where sc.cno=course.cno and course.tno=teacher.tno and teacher.tname='叶平'
and sc.sno=student.sno)

(注:数据量很大的时候not exists比not in效率高一点点)

3、查询选修“叶平”老师所授课程的学生中,成绩最高的学生姓名及其成绩(不考虑成绩并列情况)

select s.sname,max(sc.score) from student s,sc,course c,teacher t where s.sno=sc.sno and c.cno=sc.cno and c.tno=t.tno
and t.tname='叶平' 

4、查询选修“叶平”老师所授课程的学生中,成绩最高的学生姓名及其成绩(若有并列全部列出)

select s.sname,sc.score from student s,sc,course c,teacher t where s.sno=sc.sno and c.cno=sc.cno and c.tno=t.tno
and t.tname='叶平' and sc.score=
(select max(sc1.score) from sc sc1,course c1,teacher t1 where c1.cno=sc1.cno and c1.tno=t1.tno and t1.tname='叶平')

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

select s.sno,s.sname,count(sc.cno),sum(sc.score) from student s left join sc on s.sno=sc.sno group by s.sno

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

select student.sno,student.sname from student where student.sno in
(select sc.sno from sc,course,teacher where sc.cno=course.cno and course.tno=teacher.tno and teacher.tname='叶平'
group by sc.sno having count(sc.cno)=
(select count(course.cno) from course,teacher where course.tno=teacher.tno and tname='叶平'))

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

select sno,sname from student where sno in
(select sc1.sno from sc sc1,sc sc2 where sc1.sno=sc2.sno and sc1.cno='001' and sc2.cno='002')

8、查询课程编号“002”课程的成绩比“001”课程低的所有同学的学号、姓名  (比上面第1条多了个姓名)

select s.sno,s.sname from
student s,(select sno,score from sc where cno='001') a,(select sno,score from sc where cno='002') b
where a.score>b.score and a.sno=b.sno and s.sno=a.sno

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

select distinct s.sno,s.sname from student s,sc where s.sno=sc.sno and sc.sno not in
(select ss.sno from student ss, SC where ss.sno=sc.sno and sc.score>=60)

(注意:没有任何选修的人不应该列出来)

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

select s.sno,s.sname from student s left join sc
on s.sno=sc.sno group by s.sno having count(sc.cno)<(select count(cno)from course)

(注意:这里要包括没有任何选修的人)、

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

select distinct s.sno,s.sname from student s,sc where s.sno=sc.sno and s.sno!='1007' and sc.cno in
(select cno from sc where sno='1007')

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

select s.sno,s.sname from student s,sc where s.sno=sc.sno and s.sno!='1002' and sc.cno in
(select cno from sc where sno='1002') group by s.sno having count(sc.cno)=(select count(cno) from sc where sno='1002')

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

select s.sno as '学生ID',
(select score from sc sc1 where sc.sno=sc1.sno and sc1.cno='001')as '高等数学',
(select score from sc sc2 where sc.sno=sc2.sno and sc2.cno='003')as '大学英语',
(select score from sc sc3 where sc.sno=sc3.sno and sc3.cno='004')as '数据库',
count(cno)as '有效课程数',avg(sc.score)as '有效平均分'
from student s left join (select * from sc where cno in(001,003,004)) sc on s.sno=sc.sno
group by s.sno order by avg(sc.score) desc

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

select count(*) from (select distinct sno from sc) ct

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

select sname,count(sname) from student group by Sname having count(sname)>1

16、查询每门课程的平均成绩和被选人数,结果按平均成绩降序排列,平均成绩相同时,按课程号升序排列

select cno,avg(score),count(sno) from SC group by cno order by avg(score) desc,cno asc

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

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

18、查询课程名称为“数据库”且分数低于60的学生姓名和分数

select s.sno,s.sname from student s,sc,course c where s.sno=sc.sno and c.cno=sc.cno and c.cname='数据库' and sc.score<60

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

select s.sname,c.cname,sc.score from student s,sc,course c where s.sno=sc.sno and c.cno=sc.cno and sc.score>70

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

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

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

select sno,avg(score) from sc group by sno having avg(score)>60

22、查询至少选修两门课程的学生学号、姓名、选修数

select s.sno,s.sname,count(sc.cno) from student s,sc where s.sno=sc.sno group by sc.sno having count(sc.cno)>=2

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

select c.cno,c.cname,count(sc.cno) from course c,sc where c.cno=sc.cno
group by c.cno having count(sc.cno)=(select count(*) from student)

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

select sno,avg(score) from sc where score<60 group by sno having count(cno)>=2

25、查询各科成绩前三名记录的学号、课程号、分数。(不考虑成绩并列情况)

select sc.sno,sc.cno,sc.score from sc where
(select count(sc1.cno) from sc sc1 where sc1.cno=sc.cno and sc1.score>sc.score)<3
order by sc.cno,score desc

26、统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60]

select sc.cno as '课程ID', c.cname as '课程名称'
,sum(case when score between 85 and 100 then 1 else 0 end) as '[100 - 85]'
,sum(case when score between 70 and 85 then 1 else 0 end) as '[85 - 70]'
,sum(case when score between 60 and 70 then 1 else 0 end) as '[70 - 60]'
,sum(case when score < 60 then 1 else 0 end) as '[60 -]'
from sc,course c where sc.cno=c.cno group by sc.cno;

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

select cno as '课程ID',max(score) as '最高分',min(score) as '最低分' from sc group by cno

28、查询课程号“002”的成绩排名第3-第6(不考虑成绩并列情况)的同学的学号、姓名和分数

select s.sno,s.sname,sc.score from student s,sc where s.sno=sc.sno and sc.cno='002'
order by sc.score desc limit 2,4

MySQL必会的28条经典查询的更多相关文章

  1. 《MySQL必知必会》[01] 基本查询

    <MySQL必知必会>(点击查看详情) 1.写在前面的话 这本书是一本MySQL的经典入门书籍,小小的一本,也受到众多网友推荐.之前自己学习的时候是啃的清华大学出版社的计算机系列教材< ...

  2. MySQL必知必会:组合查询(Union)

        MySQL必知必会:组合查询(Union) php mysqlsql  阅读约 8 分钟 本篇文章主要介绍使用Union操作符将多个SELECT查询组合成一个结果集.本文参考<Mysql ...

  3. 《MySQL必知必会》[02] 多表联合查询

    1.基本连接 不同类型的数据,存储在多个表中,而所谓多表连接,就是将多个表联合返回一组输出. 1.1 等值连接 基本的连接方式非常简单,只需要在WHERE子句中规定如何关联即可,如下: SELECT ...

  4. MySQL 笔记整理(2) --日志系统,一条SQL查询语句如何执行

    笔记记录自林晓斌(丁奇)老师的<MySQL实战45讲> 2) --日志系统,一条SQL查询语句如何执行 MySQL可以恢复到半个月内任意一秒的状态,它的实现和日志系统有关.上一篇中记录了一 ...

  5. MySQL 笔记整理(1) --基础架构,一条SQL查询语句如何执行

    最近在学习林晓斌(丁奇)老师的<MySQL实战45讲>,受益匪浅,做一些笔记整理一下,帮助学习.如果有小伙伴感兴趣的话推荐原版课程,很不错. 1) --基础架构,一条SQL查询语句如何执行 ...

  6. mysql数据库系统学习(一)---一条SQL查询语句是如何执行的?

    本文基于----MySQL实战45讲(极客时间----林晓斌 )整理----->https://time.geekbang.org/column/article/68319 一.第一节:一条sq ...

  7. 《mysql必知必会》笔记2(子查询、联接、组合查询、全文本搜索)

    十四:使用子查询 1:子查询是嵌套在其他查询中的查询. 2:需要列出订购TNT2的所有客户信息,需要下面几步: a:从orderitems表中检索出包含物品TNT2的所有订单号: b:根据上一步得出的 ...

  8. mysql系列-⼀条SQL查询语句是如何执⾏的?

    ⼀条SQL查询语句是如何执⾏的? ⼤体来说,MySQL 可以分为 Server 层和存储引擎层两部分 Server 层 Server 层包括连接器.查询缓存.分析器.优化器.执⾏器等,涵盖 MySQL ...

  9. (转)Linux运维MySQL必会面试题100道

    老男孩教育Linux运维班MySQL必会面试题100道 (1)基础笔试命令考察 (要求:每两个同学一组,一个口头考,一个上机实战作答,每5个题为一组,完成后换位) 1.开启MySQL服务 2.检测端口 ...

随机推荐

  1. [水煮 ASP.NET Web API2 方法论](1-4)从 MVC Controller 链接到 API Controller 以及反向链接

    问题 想创建一个从 ASP.NET MVC controller 到 ASP.NET Web API controller 的直接链接,或者反向链接. 解决方案 可以使用 System.Web.Htt ...

  2. autoit v3安装

  3. ecshop,大商创后台支付系统修改模板

    初始模板 要求修改 增加了多个账户可供用户进行选择 解决方法 一找到要修改模板路径:即:http://dsctest.cn/admin/payment.php?act=edit&code=ba ...

  4. My blog in AI ---神经网络,网络架构

    上一篇博文中,我们介绍了神经网络中的神经元,那么该如何组织起来这些神经元,才能发挥出最好的效果去解决现实中的问题呢? 这是一个复杂的问题,在工程中,神经网络的架构也是训练的也是一种超参数,本节先在理论 ...

  5. JAVAEE——宜立方商城04:图片服务器FastDFS、富文本编辑器KindEditor、商品添加功能完成

    1. 学习计划 1.图片上传 a) 图片服务器FastDFS b) 图片上传功能实现 2.富文本编辑器的使用KindEditor 3.商品添加功能完成 2. 图片服务器的安装 1.存储空间可扩展. 2 ...

  6. sublime3176注册码破解汉化及常用插件

    官方网站下载地址:https://www.sublimetext.com/3 破解软件下载地址:https://www.lanzous.com/i1a7zfi 破解软件下载地址备用:https://d ...

  7. 线性表之顺序栈C++实现

    线性表之顺序栈 栈是限定仅在表尾(栈顶)进行插入删除操作的线性表,FILO:先进后出 一.顺序栈的头文件:SeqStack.h //顺序栈头文件 #include<iostream> us ...

  8. 「WC2016」挑战NPC

    「WC2016」挑战NPC 解题思路 这个题建图非常厉害,带花树什么的只会口胡根本写不动,所以我写了机房某大佬教我的乱搞. 考虑把一个筐 \(x\) 拆成 \(x1,x2,x3\) 三个点,且这三个点 ...

  9. 【20181031T1】一串数字【分解质因数+贪心】

    题面 [错解] 立方就是所有质因子次数都是3的倍数嘛 发现1e5的三次根很小,可以枚举所有和这个数乘起来是完全立方数的(flag*1) 然后--连条边跑最大独立集? 不对啊是NP问题(实际上是个二分图 ...

  10. Codeforces Round #502 (in memory of Leopoldo Taravilse, Div. 1 + Div. 2) G. The Tree

    G. The Tree time limit per test 3 seconds memory limit per test 256 megabytes input standard input o ...