剑指架构师系列-MySQL常用SQL语句
(1)分清HAVING与WHERE的区别:
HAVING 子句使你能够指定过滤条件,从而控制查询结果中哪些组可以出现在最终结果里面。WHERE 子句对被选择的列施加条件,而 HAVING 子句则对 GROUP BY 子句所产生的组施加条件。
以下的SQL语句都是基于MySQL5.6.30版本。
1、查询“1”课程比“2”课程成绩高的所有学生的学号
select a.Sno from (select Sno,score from SC where Cno=1 ) as a, (select Sno,score from SC where Cno=2 ) as b where a.score>b.score and a.Sno=b.Sno;
2、查询平均成绩大于60分的同学的学号和平均成绩
SELECT Sno,AVG(score) FROM SC GROUP BY Sno HAVING AVG(score) >60;
3、查询所有同学的学号、姓名、选课数、总成绩
SELECT Student.Sno,Student.Sname,COUNT(SC.Cno),SUM(SC.score) FROM Student LEFT OUTER JOIN SC ON Student.Sno=SC.Sno GROUP BY Student.Sno,Sname;
group by是在左外连接的基础上进行分组。注意是查询所有同学的,使用如下就会使用内连接,如果一些学生无成绩将不显示。与题目不符合
4、查询姓“李”的老师的个数
SELECT COUNT(DISTINCT(Tname)) FROM Teacher WHERE Tname LIKE '李%';
5、查询没学过“李小风”老师课的同学的学号、姓名
SELECT Student.Sno,Student.Sname FROM Student WHERE Sno NOT IN ( SELECT DISTINCT(SC.Sno) FROM SC,Course,Teacher WHERE SC.Cno=Course.Cno AND Teacher.Tno=Course.Tno AND Teacher.Tname='李小风' );
6、查询学过“1”并且也学过编号“2”课程的同学的学号、姓名
SELECT Student.Sno,Student.Sname FROM Student,SC WHERE Student.Sno=SC.Sno AND SC.Cno=1 AND EXISTS( SELECT * FROM SC AS SC_2 WHERE SC_2.Sno=SC.Sno AND SC_2.Cno=2 ) -- 查询同时学过1、2、3的课程 -- AND EXISTS( SELECT * FROM SC AS SC_3 WHERE SC_3.Sno=SC.Sno AND SC_3.Cno=3 )
7、查询学过“李小风”老师所教的“所有”课的同学的学号、姓名
SELECT Sno,Sname FROM Student WHERE Sno IN ( SELECT Sno FROM SC,Course ,Teacher WHERE SC.Cno=Course.Cno AND Teacher.Tno=Course.Tno AND Teacher.Tname='李小风' GROUP BY Sno HAVING COUNT(SC.Cno)=( SELECT COUNT(Cno) FROM Course,Teacher WHERE Teacher.Tno=Course.Tno AND Tname='李小风' ) );
8、查询所有课程成绩小于60分的同学的学号、姓名(无选课无成绩的也将显示出来);
SELECT Sno,Sname FROM Student WHERE Sno NOT IN ( SELECT Student.Sno FROM Student,SC WHERE Student.Sno=SC.Sno AND score>60 );
9、查询没有学全所有课的同学的学号、姓名;
SELECT Student.Sno,Student.Sname FROM Student,SC WHERE Student.Sno=SC.Sno GROUP BY Student.Sno,Student.Sname HAVING COUNT(SC.Cno) <(SELECT COUNT(Course.Cno) FROM Course );
10、查询至少有一门课与学号为“1”的同学所学相同的同学的学号和姓名;
SELECT s.Sno,s.Sname FROM Student s GROUP BY s.Sno,s.Sname HAVING s.Sno IN ( SELECT Course.Cno FROM Course,SC WHERE SC.Sno=1 ) SELECT s.Sno,s.Sname FROM Student s,SC sc WHERE s.Sno=sc.Sno AND sc.Cno IN ( SELECT Course.Cno FROM Course,SC WHERE SC.Sno=1 ) GROUP BY s.Sno
11、把“SC”表中“李力”老师教的课的成绩都更改为此课程的平均成绩;
UPDATE SC,Course,Teacher SET SC.score=( SELECT AVG(SC_2.score) FROM (SELECT * FROM SC) AS SC_2 WHERE SC_2.Cno=SC.Cno ) WHERE Course.Cno=SC.Cno AND Course.Tno=Teacher.Tno AND Teacher.Tname='李力';
12、查询和“2”号的同学学习的课程完全相同的其他同学学号和姓名;
SELECT Sno FROM SC WHERE Cno IN(SELECT Cno FROM SC WHERE Sno=2) GROUP BY Sno HAVING COUNT(*)=(SELECT COUNT(*) FROM SC WHERE Sno=2);
13、删除学习“李力”老师课的SC表记录;
DELETE SC FROM Course,Teacher,SC WHERE Course.Cno=SC.Cno AND Course.Tno=Teacher.Tno AND Tname='李力';
14、向SC表中插入一些记录,这些记录要求符合以下条件:没有上过编号“3”课程的同学学号、2号课的平均成绩;
INSERT SC SELECT Sno,2, ( SELECT AVG(score) FROM SC WHERE Cno=2 ) FROM Student WHERE Sno NOT IN (SELECT Sno FROM SC WHERE Cno=3);
15、按平均成绩从高到低显示所有学生的“高数”、“C语言”、“Java高级程序设计”三门的课程成绩,按如下形式显示: 学生ID,高数,C语言,Java高级程序设计,有效课程数,有效平均分
SELECT Sno AS 学生ID ,(SELECT score FROM SC WHERE SC.Sno=t.Sno AND Cno=4) AS 高数 ,(SELECT score FROM SC WHERE SC.Sno=t.Sno AND Cno=1) AS C语言 ,(SELECT score FROM SC WHERE SC.Sno=t.Sno AND Cno=6) AS Java程序高级设计 ,COUNT(*) AS 有效课程数, AVG(t.score) AS 平均成绩 FROM SC AS t GROUP BY Sno ORDER BY AVG(t.score)
16、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分
SELECT SC.Cno,MAX(score),MIN(score) FROM SC GROUP BY Cno
17、按各科平均成绩从低到高和及格率的百分数从高到低顺序
SELECT t.Cno AS 课程号, MAX(c.Cname) AS 课程名, IFNULL(AVG(t.score),0) AS 平均成绩, 100 * SUM(CASE WHEN IFNULL(t.score,0)>=60 THEN 1 ELSE 0 END)/COUNT(*) AS 及格百分数 FROM SC t,Course c WHERE t.Cno=c.Cno GROUP BY t.Cno ORDER BY 及格百分数 DESC
18、查询不同老师所教不同课程平均分从高到低显示
SELECT MAX(Z.Tno) AS 教师ID, MAX(Z.Tname) AS 教师姓名, C.Cno AS 课程ID, C.Cname AS 课程名称, AVG(Score) AS 平均成绩 FROM SC AS T,Course AS C ,Teacher AS Z WHERE T.Cno=C.Cno AND C.Tno=Z.Tno GROUP BY C.Cno ORDER BY 平均成绩 DESC
19、查询如下课程成绩第3名到第6名的学生成绩单:
高数(1),C语言(2),Java高级程序设计(3),数据结构与算法(4)
学生ID,学生姓名,高数,C语言,Java高级程序设计,数据结构与算法,平均成绩
SELECT SC.Sno AS 学生学号, Student.Sname AS 学生姓名, T1.score AS 高数, T2.score AS C语言, T3.score AS Java高级程序设计, T4.score AS 数据结构与算法, IFNULL(T1.score,0) +IFNULL(T2.score,0) +IFNULL(T3.score,0) +IFNULL(T4.score,0) AS 总分 FROM Student,SC LEFT JOIN SC AS T1 ON SC.Sno = T1.Sno AND T1.Cno =1 LEFT JOIN SC AS T2 ON SC.Sno = T2.Sno AND T2.Cno =2 LEFT JOIN SC AS T3 ON SC.Sno = T3.Sno AND T3.Cno =3 LEFT JOIN SC AS T4 ON SC.Sno = T4.Sno AND T4.Cno =4 WHERE Student.Sno=SC.Sno GROUP BY SC.Sno ORDER BY 总分 DESC LIMIT 3,3
20、统计列打印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60]
SELECT SC.Cno AS 课程ID, 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 WHERE SC.Cno=Course.Cno GROUP BY SC.Cno,Cname;
21、查询学生平均成绩及其名次
SELECT 1+( SELECT COUNT(DISTINCT 平均成绩) FROM ( SELECT Sno,AVG(score) AS 平均成绩 FROM SC GROUP BY Sno ) AS T1 WHERE 平均成绩 > T2.平均成绩 ) AS 名次, Sno AS 学生学号,平均成绩 FROM (SELECT Sno,AVG(score) AS 平均成绩 FROM SC GROUP BY Sno) AS T2 ORDER BY 平均成绩 DESC;
22、查询各科成绩前三名的记录:(不考虑成绩并列情况)
SELECT t1.Sno AS 学生ID,t1.Cno AS 课程ID,Score AS 分数 FROM SC t1 WHERE score IN ( SELECT score FROM SC WHERE t1.Cno=Cno ORDER BY score DESC LIMIT 0,3 ) ORDER BY t1.Cno;
不支持子查询中使用limit关键字,报错如下:
This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
23、查询每门课程成绩最好的前两名
24、查询出只选修了一门课程的全部学生的学号和姓名
SELECT SC.Sno,Student.Sname,COUNT(Cno) AS 选课数 FROM SC ,Student WHERE SC.Sno=Student.Sno GROUP BY SC.Sno ,Student.Sname HAVING COUNT(Cno)=1;
25、1981年出生的学生名单(注:Student表中Sage列的类型是datetime)
SELECT Sname,CAST(DATE_FORMAT(Sbirthday,'%Y-%m-%d') AS CHAR(13)) AS birthday FROM Student WHERE CAST(DATE_FORMAT(Sbirthday,'%Y-%m-%d') AS CHAR(13))='2015-09-16' ;
26、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按
课程号降序排列
SELECT Cno,AVG(score) FROM SC GROUP BY Cno ORDER BY AVG(score),Cno DESC ;
27、查询选修“李小风”老师所授课程的学生中,成绩最高的学生姓名及其成绩
SELECT Student.Sname,score FROM Student,SC,Course C,Teacher WHERE Student.Sno=SC.Sno AND SC.Cno=C.Cno AND C.Tno=Teacher.Tno AND Teacher.Tname='李小风' AND SC.score=(SELECT MAX(score) FROM SC WHERE Cno=C.Cno ); -- 如下的语句是错误的 SELECT Student.Sname,score FROM Student,SC,Course C,Teacher WHERE Student.Sno=SC.Sno AND SC.Cno=C.Cno AND C.Tno=Teacher.Tno AND Teacher.Tname='李小风' ORDER BY SC.score DESC LIMIT 1
第二条语句当李小风老师教授多个课程时,查询出来的结果应该是多条的。
28、选修至少两门课程的同学的Sno与Sname
select std.Sno,std.Sname from student std,SC sc where std.Sno=sc.Sno group by std.Sno having count(distinct(sc.Cno))>2
count()中可以使用distinct()函数
剑指架构师系列-MySQL常用SQL语句的更多相关文章
- 剑指架构师系列-MySQL调优
介绍MySQL的调优手段,主要包括慢日志查询分析与Explain查询分析SQL执行计划 1.MySQL优化 1.慢日志查询分析 首先需要对慢日志进行一些设置,如下: SHOW VARIABLES LI ...
- 剑指架构师系列-MySQL的安装及主从同步
1.安装数据库 wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm rpm -ivh mysql-commun ...
- 剑指架构师系列-Hibernate需要掌握的Annotation
1.一对多的关系配置 @Entity @Table(name = "t_order") public class Order { @Id @GeneratedValue priva ...
- 剑指架构师系列-spring boot的logback日志记录
Spring Boot集成了Logback日志系统. Logback的核心对象主要有3个:Logger.Appender.Layout 1.Logback Logger:日志的记录器 主要用于存放日志 ...
- 剑指架构师系列-Nginx的安装与使用
Nginx可以干许多事情,在这里我们主要使用Nginx的反向代理与负载均衡功能. 1.Nginx的下载安装 在安装Nginx前需要安装如下软件: GCC Nginx是C写的,需要用GCC编译 PCR ...
- 剑指架构师系列-持续集成之Maven+Nexus+Jenkins+git+Spring boot
1.Nexus与Maven 先说一下这个Maven是什么呢?大家都知道,Java社区发展的非常强大,封装各种功能的Jar包满天飞,那么如何才能方便的引入我们项目,为我所用呢?答案就是Maven,只需要 ...
- 剑指架构师系列-tomcat6通过IO复用实现connector
由于tomcat6的配置文件如下: <Connector port="80" protocol="org.apache.coyote.http11.Http11Ni ...
- 剑指架构师系列-Struts2构造函数的循环依赖注入
Struts2可以完成构造函数的循环依赖注入,来看看Struts2的大师们是怎么做到的吧! 首先定义IBlood与BloodImpl类: public interface IBlood { } pub ...
- 剑指架构师系列-tomcat6通过伪异步实现connector
首先在StandardService中start接收请求的线程,如下: synchronized (connectors) { for (int i = 0; i < connectors.le ...
随机推荐
- jvascript变量提升
javascript变量提升 首先我们来看两个例子 var a = 1; function test(){ if(!a) var a = 10 alert(a) } test() //结果是10 是不 ...
- C# QQ & 163 邮件发送
这篇文章的目的并不是说明如果进行右键的发送,因为在.net 坝坝的怀抱下邮件发送的功能实现并不会很难,当然邮件发送的代码,还是会贴上的,昨天在写一个邮件发送的功能,我直接找到了原来的代码,想着直接就可 ...
- Frame
Frame意为框架,是在屏幕上的一个矩形区域. Frame主要作为其他组件的框架基础,或为其他组件提供间距补充. 何时使用Frame组件呢? Frame组件主要用于在复杂的布局中奖其他组件分组,也用于 ...
- webstorm中github的配置
1.申请一个github账号,我这里的操作是已经有了账号的情况之下进行的. 打开webstorm,File-->settings,弹出settings框,输入git,得到以下界面,输入githu ...
- 个人建站&mac下安装hexo
title: 个人建站&mac下安装hexo date: 2018-04-18 16:34:02 tags: [mac,blog,个人建站,markdown] --- 这两天使用了markdo ...
- poj 1046 ——Color Me Less
提交地址:http://poj.org/problem?id=1046 Color Me Less Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- [SHOI2011]双倍回文
Description Input 输入分为两行,第一行为一个整数,表示字符串的长度,第二行有个连续的小写的英文字符,表示字符串的内容. Output 输出文件只有一行,即:输入数据中字符串的最长 ...
- 【网络流】【BZOJ1006】【SCOI2007】蜥蜴
学弟@lher在周末训练赛中出的题目的原题(这个人拿省选题来当作提高组模拟,太丧了...) 题意简析:看题目:) 解题思路:题目显然是最大流. 首先拆点将点权变为边权,然后按照题意对于所有有跳板的点向 ...
- 【NOIP模拟赛】总结
题目描述 输入 第一行是5个正整数,n,m,k,S,T,分别代表无向图点数,边数,蝙蝠的数量,二小姐所在起点的编号,目标点的编号. 第二行是k个正整数,分别代表大小姐每个蝙蝠所在的起点的编号.接下来有 ...
- ubuntu上的附件-终端和用快捷键ctrl+alt+f1 有啥区别
ctrl +alt +Fn 打开的是模拟终端,简单说来,linux系统一开机会自动打开6个模拟终端,然后自动切换到其中一个(一般来说是切换到图形界面的那个也就是说窗口管理器是在这6个模拟终端中运行的) ...