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. Web中的监听器【Listener】

    Servlet监听器:Servlet规范中定义的一种特殊类,它用于监听Web应用程序中的ServletContext.HttpSession和ServletRequest等域对象的创建与销毁事件,以及 ...

  2. 毕向东JAVA视频讲解(第六课)

    用java语言对现实生活中的事物进行描述. 通过类的形式来体现的. 怎么描述呢? 对于事物描述通常只关注两方面. 一个是属性,一个是行为. 只要明确该事物的属性和行为并定义在类中即可. 对象:其实就是 ...

  3. JavaWeb项目开发案例精粹-第2章投票系统-005实体层

    1. package com.sanqing.bean; /** * * 投票选项类 * */ public class VoteOption { private int voteOptionID; ...

  4. PHP程序员的40点陋习,我几乎全部中枪

    1.不写注释 2.不使用可以提高生产效率的IDE工具 3.不使用版本控制 4.不按照编程规范写代码 5.不使用统一的方法 6.编码前不去思考和计划 7.在执行sql前不执行编码和安全检测 8.不使用测 ...

  5. Storm集群的搭建

    storm的环境和hadoop的环境没有任何关系 1.安装Zookeeper集群 2.解压storm 3.修改文件conf/storm.yaml 3.1.配置zookeeper服务器 storm.zo ...

  6. Java 包装类 自动装箱和拆箱

    包装类(Wrapper Class) 包装类是针对于原生数据类型的包装. 因为有8个原生数据类型,所以对应有8个包装类. 所有的包装类(8个)都位于java.lang下. Java中的8个包装类分别是 ...

  7. Redis系列文章导读

    1. Redis简介 1.1 Redis VS Memcached 2. Redis安装教程 3. Redis数据类型 4. Redis常用命令 4.1 key 4.2 string 4.3 hash ...

  8. fzu Problem 2148 Moon Game(几何 凸四多边形 叉积)

    题目:http://acm.fzu.edu.cn/problem.php?pid=2148 题意:给出n个点,判断可以组成多少个凸四边形. 思路: 因为n很小,所以直接暴力,判断是否为凸四边形的方法是 ...

  9. Android软件开发需要学什么

    首先,需要学习哪些Android开发技术? Android的开发技术很多,在开始学习的时候不可能一次性全部学会,也没有必要一开始都全部学会,但是有些技术是非常常用的,需要在开始时打好基础,这些技术时: ...

  10. Asp.Net生成RSS方法

    一.RSS简介 什么是RSS? RSS是一种网页内容联合格式(web content sydication format). 它的名字是Really Simple Syndication的缩写. RS ...