select 语句(T-SQL) ,单表查询总结

时间:2024-04-02 19:31:56

1.材料

 

select 语句(T-SQL) ,单表查询总结

学生表 (学号,姓名,性别,出生日期,专业,班级,总学分)

课程表 (课程号,课程名,学分,教师编号)

教师表 (教师编号,姓名,性别,出生日期,职称,学院)

成绩表(学生编号,课程编号,成绩)

二,select语句语法格式

select <列>

from <表或视图>

group by <分组表达式>

having <分组表达式>

order by <排序表达式> [ASC | DESC]

单表查询

1.投影查询

    投影查询用于选择列,*号选择全部列

         1.查询student表中所以的学生的sno,sname和speciality, 并将结果中的各列的标题分别修改为学号,姓名,专业,

           并去掉重复记录(distinct),只要前5条记录;

                select  distinct  top(5) sno as 学号, sname 姓名,  speciality as 专业

                from student;

2.选择查询

        1.选择查询通过where子句实现, where 子句通过条件表达式给出查询条件,该子句紧跟from 子句后

     比较运算符:

       共有7个运算符,

           =   ,  <  ,   >   ,  !=, <> (不等于)  , >=   ,  <=    ;

        eg:    在student表中查询姓名是 '小黄' 的 学生信息;   

                 select * from student where sname='小黄';

    逻辑运算符:

          or(或)     and(与)    not (非)

   指定范围:

    between   and   ,   not between  and  ,   in  , not   in

       1.查询student表中不在1992年出生的学生情况;

       --convert ( data_type [ ( length ) ], expression ,[ style ] )    //数据类型转换函数

       --year()  ,   month() ,  day()    ;       //   提取data数据类型中的年月日, 返回int  数据类型

            select   *  from student

               where   student.sbirthday not   between    convert(date,'19920101')     and   convert(date,'19921231');

(我去,不用转换函数也行,我估计存在自动类型转换,date 自动转换为字符串类型数据)

  或;

             select * from  student

             where  year(sbirthday)  not in (1992);

或  

          select * from student
          where           student.sbirthday     not like '1992%';

3.模式匹配

   like 谓词用于指出一个字符串是否与指定的字符串向匹配;

  %: 代表0个或多个字符串,

  _: 代表一个字符

 eg:  在student 表查询姓刘的同学;

      select * from student where  sname like '刘%';

 

4.空值判断

     is null  : 是空值

     is not null: 不是空值

eg: 在成绩表中查询没有成绩的记录;

  select * from score where grade is null;

   

分组查询和统计查询(数据汇总)

     1.聚合函数

           1.count   ,  sum  , avg , max,  min  函数

               其中sum,avg函数会忽略 null值;

                讲个故事,  高三语文期中考试, 有个傻逼同学没有参加考试,  语文老师骂他,班主任也骂他,

              他被罚站一个星期,为什么呢?  以为,每次考试各个班级都会统计平均分, 用于排名。

              他没去考,也算在平均分统计内,拉了班级平均分后腿,影响老师前途. 

               而avg函数求平均数会忽略null值;

  eg:   在score表中求课程编号cno='1201'的平均分, 选课人数,课程总分;

select 

        课程编号=1201, count(*)  as 人数,     sum(grade) as  课程总分,

                    avg(grade)  as  avg平均分  ,  sum(grade) / count(*)  as 平均分
from score
where cno='1201'

  select 语句(T-SQL) ,单表查询总结            avg函数的平均分和课程总分/选课人数 不一样, 因为有一个人没有考试,成绩是null值;

 

    2.  group by 子句

              group by 用于指定需要分组的列

             注意: 如果select 子句的列名表,包含聚合函数,则该 列名表,只能包含聚合函数指定的列名和group by 子句指定的列名,

            聚合函数常与group by 子句一起使用;

  eg:    在学生表student中,查询各个班级的人数;

               select   student.sclass  as 班号 , count(*) as 人数

                from  student

                 group by student.sclass;

3.     Having子句

       having子句用于对分组按指定条件进一步筛选,过滤出满足指定条件的分组;

    eg: 查询平均成绩在90分以上的学生的学号和平均成绩;

                select  s.sno as   学号  ,  avg(grade)  as 平均成绩
                from score  as s
                group by  s.sno
                having  avg(grade)  > 90;

当where 子句,group by 子句, having 子句 和order by 子句在一起,

select 语句执行顺序如下;

  (1)执行where 子句,在表中选择行

 (2)执行group by 子句,对选择的行进行分组.

 (3) 执行聚合函数.

(4)执行having子句,筛选出满足条件的分组

(5) 执行order by 子句, 进行排序;

3.排序查询order by 子句

      order by 子句用于对查询结果进行排序; asc  身序,  desc 降列

     注意, order by 出现在整个select 语句最后

eg:   查询至少有5名学生选修的课程号和平均成绩, 按平均成绩从高到低显示;

   select     s.cno   as 课程号,   avg(grade) as 平均成绩

  from score as s

 group by s.cno

 having count(*) > 5

order by  平均成绩  desc;