use MySchoolTwo   
-- 简单查询   
select * from Student   
-- 话说这种查询的效率要比 * 要高级点   
select sId , sName , sAge , sNo , sBirthday , sClassId , sSex from Student   
select sName from student   
-- 给列改名三种方式   
select sName as '' 姓名'' , sAge as '' 年龄 '', sNo as ''学号 '' from student   
select sName ''姓名 '' ,sAge ''年龄 '' ,sNo ''学号 '' from student   
select '' 姓名 ''= sName ,'' 年龄 ''= sAge ,'' 学号 ''= sNo from student   
-- 添加where 查询条件   
select sName as '' 姓名'' , sAge as '' 年龄 '', sNo as ''学号 '' from student   
where sSex ='' 女 ''  
select sName as '' 姓名'' , sAge as '' 年龄 '', sNo as ''学号 '' from student   
where sAge >= 20   
-- 还可以直接 select调用 sql server 的内置函数入 newid()  getdate()   
select 2* 3   
select GETDATE ()   
--top distinict order by   
--Top 获取前几条数据   
select top 2 * from Student where sAge < 20   
order by sAge   
--Distinct 去除重复数据 DISTINCT是对整个结果集进行数据重复处理的,而不是针对某一个列   
select distinct sName from Student   
-- 排除相同是必须满足 distinct之后所有的字段条件的 sName 与sAge 是且的关系   
select distinct sName, sAge from Student   
-- 百分之截取前几条数据 即使都按算   
select top 10 percent sName ,sAge , sNo   
from Student   
  
-- 聚合函数   
--max min avg sum count   
select MAX ( english) from Score   
select min ( english) from Score   
-- 要注意的是 avg是不对 null 值进行计算的如果需要计算缺考人员在内计算平均分的话使用 sum/count   
select avg ( english) from Score   
select SUM ( english)/ COUNT (*) from Score   
select Count (*) from Student where sSex = ''女 ''  
  
-- 带条件查询   
select studentId from Score where english>= 60   
select sName from Student where sAge>= 20 and sAge <=30 and sSex = ''男 ''  
--between...and....   
select sName from Student where sAge between 20 and 30 and sSex ='' 男 ''  
select * from Student where sClassId = 1 or sClassId = 4   
--in....   
select * from Student where sClassId in( 1, 4 )   
  
-- 模糊查询   
--left 从左向右截取位   
select LEFT( ''2334'' ,2 )   
-- 检索姓张的   
select * from Student where left(sName , 1)= '' 张''  
--like 的百分号代表一个或多个字符—下滑杠只代表一个字符 [] 代表满足其中的一个字符   
-- 同样检索姓张的同上   
select * from Student where sName like ''张 %''  
-- 这个检索名字中出现亮的   
select * from Student where sName like ''%亮 %''  
-- 只检索名称为张某的 名称两个字的   
select * from Student where sName like ''张 __''  
-- 检索张飞某或者张亮某   
select * from Student where sName like ''张 [ 飞亮]%''  
-- 测试数据   
insert into Student ( sClassId, sName ,sAge , sSex, sNo ,sBirthday , sPhone) values (4 , ''诸葛亮 '' ,20 , ''男 '' ,223156788011 , ''1989-8-8'', ''123456'' )   
insert into Student ( sClassId, sName ,sAge , sSex, sNo ,sBirthday , sPhone) values (4 , ''张亮颖 '' ,20 , ''女 '' ,22315678834 , ''1989-8-8'', ''123456'' )   
-- 使用like 通配符限制 sphone 为六位数字   
alter table student   
add constraint CK_Student_sPhone check (sPhone like ''[0-9][0-9][0-9][0-9][0-9][0-9]'')   
  
-- 空值处理null   
-- 数据库中,一个列如果没有指定值,那么值就为 null ,这个null 和 C#中的 null ,数据库中的 null表示“不知道”,   
-- 而不是表示没有。因此 select null+1结果是 null ,因为“不知道”加的结果还是“不知道”。   
select null+ 123 --返回 null   
--select * from score where english != null ;都没有任何返回结果,因为数据库也“不知道”。   
--SQL 中使用is null 、 is not null来进行空值判断   
select * from student where sPhone is null  
select * from student where sPhone is not null  
  
--ORDER BY 子句位于SELECT 语句的末尾,它允许指定按照一个列或者多个列进行排序,   
-- 还可以指定排序方式是升序(从小到大排列, ASC )还是降序(从大到小排列, DESC )。   
-- 先按英语升序英语相同再按数学升序   
select * from Score   
order by english, math   
-- 指定前后的排序规则   
-- 按照英语成绩从大到小排序,如果英语成绩相同则按照数学成绩从小到大排序   
select * from Score   
order by english desc ,math asc  
--order by 一定要出现在 where之后   
select * from Student   
where sSex = ''男 ''  
order by sAge   
  
-- 分组Group by   
-- 分组就是把相同值的那些行 合并成为一行   
-- 当查询条件含有 ''每个 '' 的时候会使用到分组   
-- 每个班有多少个学生   
-- 第一个问题 select之后的列必须出现在 group by 子句聚合函数除外   
select COUNT (*), sClassId from Student   
group by sClassId   
-- 每个班中男同学的个数   
select COUNT (*) as '' 男生个数'' , sClassId as '' 班级 '' from Student   
where sSex = ''男 ''  
group by sClassId   
-- 每个班的平均年龄   
select COUNT (*) as '' 人数'' , sClassId as '' 班级 '', AVG( sAge )as ''平均年龄 '' from Student   
group by sClassId   
-- 错误 聚合函数是不能出现在 where 子句中的   
-- 必须使用Having , Having要位于 Group By 之后   
-- 求每个班平均年龄大于的总人数   
select COUNT (*), sClassId, AVG (sAge ) from Student   
where AVG ( sAge)>= 20   
group by   sClassId   
-- 正确 使用 having对 group by 分组后的数据进行筛选   
--Having 是Group By 的条件对分组后的数据进行筛选   
-- 求每个班平均年龄大于的总人数   
select COUNT (*) as '' 总人数'' , sClassId as '' 班级 '', AVG (sAge ) as '' 平均年龄'' from Student   
group by sClassId   
having AVG ( sAge)>= 20   
-- 注意Having 中不能使用未参与分组的列, Having 不能替代where 。作用不一样, Having 是对组进行过滤。   
-- 求每个班年龄大于平均年龄的总人数   
select count (*), sClassId from Student   
where sAge >=( select AVG (sAge ) from Student)   
group by sClassId   
select * from Student   
  
-- 分组练习   
-- 求男生女生分别有多少人   
select COUNT (*), sSex from Student   
group by sSex   
-- 求每个班有多少男生   
select COUNT ( sId), sClassId from Student   
where sSex = ''男 ''  
group by sClassId   
-- 每个班中男同学的平均年龄   
select COUNT ( sId), sClassId ,AVG ( sAge) as '' 平均年龄 '' from Student   
where sSex = ''男 ''  
group by sClassId   
-- 求平均年龄小于的那些班   
select sClassId , AVG( sAge ) from Student   
group by sClassId   
having AVG ( sAge)< 22   
  
--union 联合结果集   
-- 基本的原则:每个结果集必须有相同的列数;每个结果集的列必须类型相容   
-- 排序 去除重复数据   
select tSex , tName from teacher union  
select sSex , sName from Student   
--union all 与union 的区别在于 union 会去除重复行而且会重新组合排序   
--UNION 合并两个查询结果集,并且将其中完全重复的数据行合并为一条   
--Union 因为要进行重复值扫描,所以效率低,因此如果不是确定要合并重复行,那么就用 UNION ALL   
select tSex , tName from teacher union all  
select sSex , sName from Student   
--union 的另一种用法一次插入多条数据   
insert into teacher ( tName, tSex ,tAge , tSalary)   
select '' 凤姐 '', '' 女'' , 18, 2400 union  
select '' 月月 '', '' 女'' , 19, 3000   
  
-- 要求在一个表格中查询出学生的英语最高成绩、最低成绩、平均成绩   
select '' 最高成绩 '', MAX (english ) from Score union  
select '' 最低成绩 '', min (english ) from Score union  
select '' 平均成绩 '', avg (english ) from Score   
-- 查询每位老师的信息,包括姓名、工资,并且在最后加上平均工资和最高工资   
select tName , tSalary from teacher union all  
select '' 平均工资 '', AVG (tSalary ) from teacher union all  
select '' 最高工资 '', Max (tSalary ) from teacher   
-- 把现有表中的数据插入到新表中 (newStudent 表不能存在 )   
select * into NewStudent from Student   
-- 基于以上逻辑可以把 newStudent作为零时表实现去除表中重复数据   
select distinct sAge into newStudent from student   
  
-- 把现有表的数据复制到一个已存在的表   
truncate table newStudent   
-- 复制数据会出现两个问题第一 NewStudent 没有标识列 没有主键   
insert into newStudent select * from Student   
insert into newStudent select sName , sAge , sNo , sBirthday , sClassId , sSex , sPhone from Student

SQL你必须知道的-查询聚合分组排序的更多相关文章

  1. [SQL基础教程] 3-4 对查询结果进行排序/ORDER BY

    [SQL基础教程] 3-4 对查询结果进行排序/ORDER BY ORDER BY SELECT <列名1>,<列名2>,<列名2>... FROM ORDER B ...

  2. Django 多表查询练习题 Q查询 F查询 聚合 分组

    -------------------------------------------------自己偷的懒,或许用加倍时间也补不回来,珍惜现在的拥有的时光,把我现在! 上节回顾 基于对象的跨表查询( ...

  3. day056-58 django多表增加和查询基于对象和基于双下划线的多表查询聚合 分组查询 自定义标签过滤器 外部调用django环境 事务和锁

    一.多表的创建 from django.db import models # Create your models here. class Author(models.Model): id = mod ...

  4. Oracle总结【SQL细节、多表查询、分组查询、分页】

    前言 在之前已经大概了解过Mysql数据库和学过相关的Oracle知识点,但是太久没用过Oracle了,就基本忘了...印象中就只有基本的SQL语句和相关一些概念....写下本博文的原因就是记载着Or ...

  5. TP多条件sql查询,分组排序

    $k=M('order a'); $bj=$k->join("left join __CHANGE__ b on b.tb_name='order'and a.order_id=b.t ...

  6. sql中筛选第一条记录【分组排序】

    问题描述 我们现在有一张表titles,共有4个字段,分别是emp_no(员工编号),title(职位),from_date(起始时间),to_date(结束时间),记录的是员工在某个时间段内职位名称 ...

  7. sql server 中判断分组排序的使用示例

    现在需要查询一组数据,是对一列字段(column01)的数据分范围查询后分组排序: select (case when [column01] >0 AND [column01]<= 500 ...

  8. python 之 Django框架(orm单表查询、orm多表查询、聚合查询、分组查询、F查询、 Q查询、事务、Django ORM执行原生SQL)

    12.329 orm单表查询 import os if __name__ == '__main__': # 指定当前py脚本需要加载的Django项目配置信息 os.environ.setdefaul ...

  9. Django框架08 /聚合查询、分组、F/Q查询、原生sql相关

    Django框架08 /聚合查询.分组.F/Q查询.原生sql相关 目录 Django框架08 /聚合查询.分组.F/Q查询.原生sql相关 1. 聚合查询 2. 分组 3. F查询和Q查询 4. o ...

随机推荐

  1. Ubuntu 12.04LTS 找不到eth0网卡

    我的机器是DELL 14R INSPRION 7420 笔记本.试了好多方法都不行,比如这个教程: . sudo ifconfig -a //查看所有网卡现状,看eth0是否存在,在结果列表应该找不到 ...

  2. web服务器和应用服务器概念比较

    转自:http://hi.baidu.com/lclkathy/blog/item/dae3be36763a47370b55a970.html 一 常见的WEB服务器和应用服务器 在UNIX和LINU ...

  3. SpringMVC,MyBatis商品的增删改查

    一.需求 商品的增删改查 二.工程结构 三.代码 1.Mapper层 (1) ItemsMapperCustom.java package com.tony.ssm.mapper; import ja ...

  4. 前端必杀技之Javascript 第1天

    学习了javascript基本语法和使用DOM进行简单操作   1.引用javascript方法: a.在<script></script>标签中加入js代码,如: <s ...

  5. Spring两种实现AOP的方式

    有两种实现AOP的方式:xml配置文件的方式和注解的形式 我们知道通知Advice是指对拦截到的方法做什么事,可以细分为 前置通知:方法执行之前执行的行为. 后置通知:方法执行之后执行的行为. 异常通 ...

  6. linux系统的文件类型学习

    linux是一个文件型操作系统,在linux下一切皆文件. 目录.字符设备.块设备.管道.套接字.符号连接文件等在linux下统统都是文件. linux下的文件类型分为以下几种类型: 1. 正规文件, ...

  7. JVM学习笔记(三)------内存管理和垃圾回收

    JVM内存组成结构 JVM栈由堆.栈.本地方法栈.方法区等部分组成,结构图如下所示: 1)堆 所有通过new创建的对象的内存都在堆中分配,其大小可以通过-Xmx和-Xms来控制.堆被划分为新生代和旧生 ...

  8. Zookeeper集群和HBase集群

    1.部署Zookeeper集群(hadoop0\hadoop1\hadoop2) 1.1.在hadoop0上解压缩Zookeeper-3.4.5.tar.gz 1.2.执行命令 cp conf/zoo ...

  9. hadoop2 环境的搭建(手动HA)

    1.手工切换ha的环境的搭建(比hadoop1多出来journalnode的配置) namenode:hadoop110和hadoop111 datanode:hadoop112.hadoop113. ...

  10. Ubuntu 安装mod_python配置Apache2

    在Ubuntu上搭建Python运行环境,mod_python是不可少的(据说mod_swgi也是可以的,没有亲测).使用命令安装mod_python. 安装: apt-get install lib ...