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 API 2:Action的返回类型

    Web API控制器中的Action方法有如下几种返回类型: void HttpResponseMessage IHttpActionResult 其它类型 基于上面几种不同的返回类型,Web API ...

  2. USACO 4.4 Pollutant Control (网络流求最小割割集)

    Pollutant ControlHal Burch It's your first day in Quality Control at Merry Milk Makers, and already ...

  3. doc元素select 取值

  4. CentOS 升级Python3.X和pip3

    目的:实现python3 and python2 共存,pip2 and pip3共存 一.安装依赖 yum install openssl-devel -y yum install zlib-dev ...

  5. select 1 from dual

    begin 2018年7月14日15:06:29 select 1 from dual Oracle下的select 1 from dual 今天在看公司代码的时候,发现有这一句SQL: select ...

  6. Power OJ 2605 SPFA+dp思想

    题目链接[https://www.oj.swust.edu.cn/problem/show/2605] 题意:给出包含N(N <= 5000)个点M条边的有向图,然后求1 - N在满足距离小于T ...

  7. Codeforces Round #448 C. Square Subsets

    题目链接 Codeforces Round #448 C. Square Subsets 题解 质因数 *质因数 = 平方数,问题转化成求异或方程组解的个数 求出答案就是\(2^{自由元-1}\) , ...

  8. 【spfa】【动态规划】zoj3847 Collect Chars

    转载自:http://blog.csdn.net/madaidao/article/details/42616743 Collect Chars Time Limit: 2 Seconds       ...

  9. SQL SERVER 扩展属性的操作方法

    将数据库迁移到 Azure SQL 数据库时出现错误,不支持扩展属性“MS_Description”,因此就如何操作扩展属性进行在此记录. 查询扩展属性 SELECT *,OBJECT_NAME(ma ...

  10. [Cocos2dx] CCCamera照相机 详解

    前言 在3D游戏当中,我们经常会使用到照相机这个东西,无论你使用的是哪一款引擎,都会用到,同时,照相机这个东西涉及到的东西比较多,基础知识需要扎实一些才可以. 如何使用 很久之前做项目的时候用到过一次 ...