MySQL必会的28条经典查询
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条经典查询的更多相关文章
- 《MySQL必知必会》[01] 基本查询
<MySQL必知必会>(点击查看详情) 1.写在前面的话 这本书是一本MySQL的经典入门书籍,小小的一本,也受到众多网友推荐.之前自己学习的时候是啃的清华大学出版社的计算机系列教材< ...
- MySQL必知必会:组合查询(Union)
MySQL必知必会:组合查询(Union) php mysqlsql 阅读约 8 分钟 本篇文章主要介绍使用Union操作符将多个SELECT查询组合成一个结果集.本文参考<Mysql ...
- 《MySQL必知必会》[02] 多表联合查询
1.基本连接 不同类型的数据,存储在多个表中,而所谓多表连接,就是将多个表联合返回一组输出. 1.1 等值连接 基本的连接方式非常简单,只需要在WHERE子句中规定如何关联即可,如下: SELECT ...
- MySQL 笔记整理(2) --日志系统,一条SQL查询语句如何执行
笔记记录自林晓斌(丁奇)老师的<MySQL实战45讲> 2) --日志系统,一条SQL查询语句如何执行 MySQL可以恢复到半个月内任意一秒的状态,它的实现和日志系统有关.上一篇中记录了一 ...
- MySQL 笔记整理(1) --基础架构,一条SQL查询语句如何执行
最近在学习林晓斌(丁奇)老师的<MySQL实战45讲>,受益匪浅,做一些笔记整理一下,帮助学习.如果有小伙伴感兴趣的话推荐原版课程,很不错. 1) --基础架构,一条SQL查询语句如何执行 ...
- mysql数据库系统学习(一)---一条SQL查询语句是如何执行的?
本文基于----MySQL实战45讲(极客时间----林晓斌 )整理----->https://time.geekbang.org/column/article/68319 一.第一节:一条sq ...
- 《mysql必知必会》笔记2(子查询、联接、组合查询、全文本搜索)
十四:使用子查询 1:子查询是嵌套在其他查询中的查询. 2:需要列出订购TNT2的所有客户信息,需要下面几步: a:从orderitems表中检索出包含物品TNT2的所有订单号: b:根据上一步得出的 ...
- mysql系列-⼀条SQL查询语句是如何执⾏的?
⼀条SQL查询语句是如何执⾏的? ⼤体来说,MySQL 可以分为 Server 层和存储引擎层两部分 Server 层 Server 层包括连接器.查询缓存.分析器.优化器.执⾏器等,涵盖 MySQL ...
- (转)Linux运维MySQL必会面试题100道
老男孩教育Linux运维班MySQL必会面试题100道 (1)基础笔试命令考察 (要求:每两个同学一组,一个口头考,一个上机实战作答,每5个题为一组,完成后换位) 1.开启MySQL服务 2.检测端口 ...
随机推荐
- input文本框 输入限制三则
其一,只允许输入数字和小数点. <input onKeypress="return (/[\d.]/.test(String.fromCharCode(event.keyCode))) ...
- 【AtCoder】AGC023 A-F题解
可以说是第一场AGC了,做了三道题之后还有30min,杠了一下D题发现杠不出来,三题滚粗了 rating起步1300+,感觉还是很菜... 只有三题水平显然以后还会疯狂--啊(CF的惨痛经历) 改题的 ...
- linux添加桌面快捷方式
linux通过软件中心安装的程序快捷图标都放在/usr/share/applications/目录下.点选一个图标右键,选择复制到,目录选择桌面即可. 但是有一些软件自行安装后,在那个目录里没有 ...
- thinkphp _complex 复合查询 where多个子组实现
SELECT * FROM `user` WHERE ( `mobile` = '13824653465' OR `nickname` = 'evan' OR `openid` = '14545-fd ...
- Hive分区和桶的概念
Hive 已是目前业界最为通用.廉价的构建大数据时代数据仓库的解决方案了,虽然也有 Impala 等后起之秀,但目前从功能.稳定性等方面来说,Hive 的地位尚不可撼动. 其实这篇博文主要是想聊聊 S ...
- Bootstrap入门六:表单
表单主要包含表单域.输入框.下拉框.单选框.多选框和按钮等控件. 1.基本实例 单独的表单控件会被自动赋予一些全局样式.所有设置了 .form-control 类的 <input>.< ...
- Hadoop错误1(Text类型与String类型)
在此类的博客中,博主主要记录的是在Hadoop实践过程中遇到的一些错误,先上一个代码 protected void map(Object key,Text value, Context context ...
- Django的Form机制小问题
使用Django,我们可以以声明式的方式来定义一个Form,如下: 1 2 3 4 5 # -*- coding: utf-8 -*- from django import forms class S ...
- python实现括号匹配
1.用一个栈[python中可以用List]就可以解决,时间和空间复杂度都是O(n) # -*- coding: utf8 -*- # 符号表 SYMBOLS = {'}': '{', ']': '[ ...
- codevs 5971 打击犯罪
5971 打击犯罪 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 某个地区有n(n<=1000)个犯罪团伙,当地警方按照他们 ...