(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语句的更多相关文章

  1. 剑指架构师系列-MySQL调优

    介绍MySQL的调优手段,主要包括慢日志查询分析与Explain查询分析SQL执行计划 1.MySQL优化 1.慢日志查询分析 首先需要对慢日志进行一些设置,如下: SHOW VARIABLES LI ...

  2. 剑指架构师系列-MySQL的安装及主从同步

    1.安装数据库 wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm rpm -ivh mysql-commun ...

  3. 剑指架构师系列-Hibernate需要掌握的Annotation

    1.一对多的关系配置 @Entity @Table(name = "t_order") public class Order { @Id @GeneratedValue priva ...

  4. 剑指架构师系列-spring boot的logback日志记录

    Spring Boot集成了Logback日志系统. Logback的核心对象主要有3个:Logger.Appender.Layout 1.Logback Logger:日志的记录器 主要用于存放日志 ...

  5. 剑指架构师系列-Nginx的安装与使用

    Nginx可以干许多事情,在这里我们主要使用Nginx的反向代理与负载均衡功能. 1.Nginx的下载安装 在安装Nginx前需要安装如下软件: GCC  Nginx是C写的,需要用GCC编译 PCR ...

  6. 剑指架构师系列-持续集成之Maven+Nexus+Jenkins+git+Spring boot

    1.Nexus与Maven 先说一下这个Maven是什么呢?大家都知道,Java社区发展的非常强大,封装各种功能的Jar包满天飞,那么如何才能方便的引入我们项目,为我所用呢?答案就是Maven,只需要 ...

  7. 剑指架构师系列-tomcat6通过IO复用实现connector

    由于tomcat6的配置文件如下: <Connector port="80" protocol="org.apache.coyote.http11.Http11Ni ...

  8. 剑指架构师系列-Struts2构造函数的循环依赖注入

    Struts2可以完成构造函数的循环依赖注入,来看看Struts2的大师们是怎么做到的吧! 首先定义IBlood与BloodImpl类: public interface IBlood { } pub ...

  9. 剑指架构师系列-tomcat6通过伪异步实现connector

    首先在StandardService中start接收请求的线程,如下: synchronized (connectors) { for (int i = 0; i < connectors.le ...

随机推荐

  1. python/SQLAchemy

    python/SQLAchemy SQLAlchemy是Python编程语言下的一款ORM框架,该框架建立在数据库API之上,使用关系对象映射进行数据库操作,简言之便是:将对象转换成SQL,然后使用数 ...

  2. python 资产管理

    python 资产管理 一.Agent 方式 1.这个方法的优点:使用简单,速度快,适合服务器较多场景使用,缺点:服务器比较占资源,性能会变低. 2.使用Agent的前提条件是客户端(服务器)特别多的 ...

  3. 初识 SpringMVC

    1.Spring MVC 的工作流程 1.web请求被 前端控制器(DispatcherServlet)拦截 2.DispatcherServlet调用 映射处理器(HandelerMapping)查 ...

  4. 使用Vertx构建微服务

    Vertx Vert.x is a tool-kit for building reactive applications on the JVM.(Vertx是运行在JVM上用来构建reactive ...

  5. C++中 return,break,continue的用法

    引用:https://blog.csdn.net/smf0504/article/details/51315835 https://blog.csdn.net/ting_junhui/article/ ...

  6. [Ubuntu 16.04 LTS ]屏幕分辨率 Unknown display

    新装完Ubuntu 16.04 LTS 通过System settings-->Displays 设置屏幕分辨率 显示"Unknown display",选择后无反应,并且屏 ...

  7. Python 爬取猫眼电影最受期待榜

     主要爬取猫眼电影最受期待榜的电影排名.图片链接.名称.主演.上映时间. 思路:1.定义一个获取网页源代码的函数: 2.定义一个解析网页源代码的函数: 3.定义一个将解析的数据保存为本地文件的函数: ...

  8. [NOIp 2012]同余方程

    Description 求关于 x 的同余方程 ax ≡ 1 (mod b)的最小正整数解. Input 输入只有一行,包含两个正整数 a, b,用一个空格隔开. Output 输出只有一行,包含一个 ...

  9. [Apio2012]dispatching 左偏树

    题目描述 在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依据自己的工作获取报偿.在这个帮派里,有一名忍者被称之为 Master.除了 Master以外,每名忍者都有且仅有一个上级.为保密,同时增 ...

  10. hdu 5489(LIS最长上升子序列)

    题意:一个含有n个元素的数组,删去k个连续数后,最长上升子序列        /*思路参考GoZy 思路: 4 2 3 [5 7 8] 9 11 ,括号表示要删掉的数, 所以  最长上升子序列  = ...