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. Unrecognized Windows Sockets error: 0: JVM_Bind异常

    根据端口查看 根据PID查看具体的进程 任务管理器->查看-选择列,选中PID 然后查看任务管理器.

  2. cojs 西瓜 解题报告

    首先我们要知道pick公式 设二维平面内任意多边形面积为S 设多边形内部整点数为a 设多边形边界的整点数为b 则满足S=a+b/2-1 变形得a=S-b/2+1 由期望的线性性质我们把问题转化为 1. ...

  3. Tomcat处理HTTP请求源码分析(下)

    转载:http://www.infoq.com/cn/articles/zh-tomcat-http-request-2 很多开源应用服务器都是集成tomcat作为web container的,而且对 ...

  4. Generic repository pattern and Unit of work with Entity framework

    原文 Generic repository pattern and Unit of work with Entity framework Repository pattern is an abstra ...

  5. 239. Sliding Window Maximum

    题目: Given an array nums, there is a sliding window of size k which is moving from the very left of t ...

  6. redhat 安装telnet服务

    系统默认不安装telnet服务的,所有要安装的话,可以加载redhat 光盘.我的操作是在VM上完成的 vm加载系统光盘:设备状态选择已连接,ISO映像文件选择完整的镜像文件路径,例如: D:\sof ...

  7. Android 动态改变布局属性RelativeLayout.LayoutParams.addRule()

    我们知道,在 RelativeLayout 布局中有很多特殊的属性,通常在载入布局之前,在相关的xml文件中进行静态设置即可. 但是,在有些情况下,我们需要动态设置布局的属性,在不同的条件下设置不同的 ...

  8. 创建支持复杂脚本Complex Scripts的WINCE6.0系统

    如果要创建支持复杂脚本(Complex Scripts)的系统,我们需要完成下面一系列步骤来确保系统包含所有需要支持的具体区域设置 (locale–specific). 1.     选择intern ...

  9. 面试题_89_to_92_单元测试 JUnit 面试题

    89)如何测试静态方法?(答案)可以使用 PowerMock 库来测试静态方法. 90)怎么利用 JUnit 来测试一个方法的异常?(答案) 91)你使用过哪个单元测试库来测试你的 Java 程序?( ...

  10. Enabling HierarchyViewer on Rooted Android Devices

    转自http://blog.apkudo.com/2012/07/26/enabling-hierarchyviewer-on-rooted-android-devices/. The Hierarc ...