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. 表单插件——form

    表单插件——form 通过表单form插件,调用ajaxForm()方法,实现ajax方式向服务器提交表单数据,并通过方法中的options对象获取服务器返回数据,调用格式如下: $(form). a ...

  2. lintcode 中等题:Intersection of Two Linked Lists 两个链表的交叉

    题目 两个链表的交叉 请写一个程序,找到两个单链表最开始的交叉节点. 样例 下列两个链表: A: a1 → a2 ↘ c1 → c2 → c3 ↗ B: b1 → b2 → b3 在节点 c1 开始交 ...

  3. lintcode 中等题:Evaluate Reverse Polish notation逆波兰表达式求值

    题目 逆波兰表达式求值 在逆波兰表达法中,其有效的运算符号包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰计数表达. 样例 ["2", "1&q ...

  4. lintcode 中等题:Submatrix sum is 0 和为零的子矩阵

    和为零的子矩阵 给定一个整数矩阵,请找出一个子矩阵,使得其数字之和等于0.输出答案时,请返回左上数字和右下数字的坐标. 样例 给定矩阵 [ [1 ,5 ,7], [3 ,7 ,-8], [4 ,-8 ...

  5. 【nginx运维基础(2)】Nginx的配置文件说明及虚拟主机配置示例

    配置文件说明 #定义Nginx运行的用户和用户组 user www www; #nginx进程数,建议设置为当前主机的CPU总核心数. worker_processes 8; #全局错误日志定义类型, ...

  6. 在对话框上拖动按钮并移动该按钮(改写CXXButton::PreTranslateMessage,然后MoveWindow)

    // 派生自CButton类,主要过滤WM_LBUTTONDOWN .WM_LBUTTONUP和WM_MOUSEMOVE消息. BOOL m_bFlag = FALSE; // 成员变量,用来标示鼠标 ...

  7. Android关于实现EditText中加多行下划线的的一种方法

    1. 重写EditText public class LinedEditText extends EditText { private Paint linePaint; private float m ...

  8. Android 显示原理简介

    作者:yearzhu,2011年进入腾讯公司,从事过Web端及移动端的测试工作,喜爱新鲜事物及新技术,目前在SNG开放平台测试组负责的移动互联SDK的测试工作. 现在越来越多的应用开始重视流畅度方面的 ...

  9. Android ActionBar中的下拉菜单

    在ActionBar中添加下拉菜单,主要有一下几个关键步骤: 1. 生成一个SpinnerAdapter,设置ActionBar的下拉菜单的菜单项 2. 实现ActionBar.OnNavigatio ...

  10. testNG小试牛刀

    testNG是一个测试框架,其灵感来自JUnit和NUnit的,但引入了一些新的功能,使其功能更强大,使用更方便. testNG是一个开源自动化测试框架:testNG表示下一代. testNG是类似于 ...