zoukankan      html  css  js  c++  java
  • SQL课堂笔记聚合函数


         2017.11.13

    聚合函数:
     COUNT(*)统计表中元组个数
     COUNT(属性名)统计一列中列值的个数 //不统计空值
     SUM计算一列值的总和(此列必须是数值行)
     AVG计算一列的平均值(此列必须是数值行)
     MAX求一列值中的最大值
     MIN求一列值中的最小值
        元组:每一条数据
     
     例1.求计算机专业学生的平均年龄:
      select avg(sage) as 平均年龄 from student where sdept='计算机'
     
     例2.统计学校共开设课多少们课程:
      select count(*) from course
      select count(cno) from course

     例3.统计交了学费的学生人数:
      select count(*) from course --统计所有的记录:交了的没交的都被统计了
      select count(fees) from course  --统计交了的学生个数,没交的不统计

     例4:如何去掉重复的项目再统计:
      如:查询选修了课程的学生人数
       select count(distinct sno) from sc
        注:用distinct以避免重复计算学生人数
         (在access中不能用这种结构,在SQL server2008中可以)

     ex:查询选修课成绩最高分,最低分和平均分,(查询的列注意重命名为:最高分,最低分,平均分,
        结果存放在新表abc中)
      select MAX(grade) as 最高分 ,MIN (grade)最低分,AVG(grade) as 平均分 into abc from sc
     
    order by 子句: 

      例5:将课程按学分升序排序:
      select * from course order by credit
      select * from course order by ccredit desc(降序)

     例6:查询选修了2课程的学生的学号和成绩,查询结果按成绩降序排序:
      select cno, sno,grade from sc where cno=2 order by grade desc
     
     例7:查询电子,计算机专业学生的姓名,专业名,结果按专业名升序,姓名降序排序:
      order by后面可以跟多个字段进行排序:
           用A1代表第一个字段,A2代表第二个字段
       1.order by A1,A2 desc    --A1升序,A2降序
       2.order by A1 asc,A2 desc     --A1升序,A2降序   asc可省略
       3.order by A1 desc,A2 desc    --A1,A2降序
      select sname AS 姓名,sdept AS 系名 from student where sdept in('电子','计算机') order by sdept,sname desc
      
    group by 子句(分组之后进行统计):

     例8:查询各门课程的选修人数:
      select cno,count(*) from sc group by cno

     例9:统计男女生各有多少人:
      select ssex,count(ssex) as 人数 from student group by ssex
     
     例10:求学生的总分:
      select sno,sum(grade) as 总分 from sc group by sno

    having 子句(已经分组后的元组,并不针对整张表,一般接到group by后使用):

     例11:求选课在2门以上的学生,并统计平均成绩:
      select sno, avg(grade) as average from sc group by sno having count(*)>=2
      
     例12:求选课在2门以上并且成绩及格的学生,并统计平均成绩:
      select sno,avg(grade) as average from sc where grade>=60 group by sno having count(*)>=2  --这是错误的写法
      select sno,avg(grade) as average from sc group by sno having count(*)>=2 and min(grade)>=60 --对的
      

    ----所用的表是博客文件"student表"

    练习:

    -- 一、在商场销售数据库“Customer”中书写SQL代码进行查询:
    use Customer
    go

    --1、查询顾客姓名以及联系方式。
    select 姓名,联系方式 from 顾客

    --2、查询所有商品类别(结果中不能有重复的商品类别)。
    select distinct 商品类别 from 商品

    --3、查询单价在100到200之间的商品名。
    select 商品名 from 商品 where 单价>100 and 单价<200

    --4、查询单价在300以上的鞋类的商品名。
    select 商品名 from 商品 where 商品类别='鞋子' and 单价>300

    --5、查询购买了C101,D201或E301商品号的顾客号(结果中顾客号不能有重复)。
    select distinct 顾客号 from 购买 where 商品号 in('C101','D201','E301')

    --6、查询姓王的的顾客姓名和联系方式。
    select 姓名,联系方式 from 顾客 where 姓名 like '王%'

    --7、查询除姓刘的以外的顾客姓名和联系方式。
    select 姓名,联系方式 from 顾客 where 姓名 not like '刘%'


    --二.在students数据库中(已给出)用SQL查询完成下面查询:(书写sql语句)
    use students
    go
    --1、查询考试成绩有不及格学生的学号。
    select sno from sc where grade<60

    --2、将课程按学分升序排序。
    select * from course order by ccredit

    --3、查询2号课程的成绩在前3名学生的学号 。
    select top 3 sno from sc  where cno=2  order by grade desc

    --4、查询各门课程的选修人数。
    select cno,count(*)as 人数 from sc group by cno

    --5、统计男女生各有多少人。
    select ssex,count(ssex) as 人数 from student group by ssex

    --6、求各学生的总分。
    select sno,sum(grade) as 总分 from sc group by sno

    --7、求选课在2门以上并且成绩及格的学生,并统计平均成绩。
    select sno,avg(grade) as average from sc group by sno having count(*)>=2 and min(grade)>=60

    --8、查询选修了2课程的学生的学号、课程号和成绩,查询结果按成绩降序排序。
    select sno,cno,grade from sc where cno=2 order by grade desc

    --9、查询专业人数少于2人的专业名称及人数。
    select sdept,COUNT(*) as 人数 from student group by sdept having COUNT(sdept)<2

    --10、查询女学生的姓名、专业名,结果按姓名升序排序,专业名降序。
    select sname AS 姓名,sdept as 专业名 from student where ssex in('女')order by sname,sdept desc

  • 相关阅读:
    vs2013配置opencv2.4.13(txt中复制粘贴即可,一次配置永久使用)
    描述性统计量
    Ubuntu创建、删除文件与目录
    Linux下服务器端开发流程及相关工具介绍(C++)
    TCP 协议如何保证可靠传输
    真实记录疑似Linux病毒导致服务器 带宽跑满的解决过程
    Windbg程序调试--转载
    原来问题在这里-我的memory leak诊断历程
    用WinDbg分析Debug Diagnostic Tool生成的Userdump文件
    一个内存增长问题的分析和处理(三)
  • 原文地址:https://www.cnblogs.com/TuringShine/p/7843715.html
Copyright © 2011-2022 走看看