关键词相关 关键词一共6个,一条查询语句最少要有2个关键词,最多6个关键词,每个关键词只能出现一次
select 选列
from 选表
where 选行
group by 分组
having 选分组后的行
order by 排序
书写顺序 :select — from — where — group by — having — order by执行顺序 :from — where — group by — having — select — order by
排序 order by asc 升序(从小到大),默认,可写可不写; desc 降序(从大到小),需在列名后标注
1 2 select * from emp order by sal select * from emp order by sal desc
order by 是唯一一个可以使用别名的关键词
1 select sal 工资, deptno 部门 from emp order by 工资 desc
排序后空值最大(升序排序时空值在最下面)
1 select * from emp order by comm desc
desc不共用(每列默认升序,需逐个设置) 多个列进行排序时,先全按照左边的列进行排序,在左边列排完的基础上,再排右边列
1 2 3 4 select * from emp order by deptno, sal desc select * from emp order by deptno desc , sal desc
数字指代能用但不常用
1 select deptno,ename,sal from emp order by 1 ,3 desc
分组 group by 单独使用时效果相当于去重,一般只搭配聚合函数一起使用
1 2 3 select distinct deptno from emp select deptno from emp group by deptno
分组后select后面只能跟分了组的列和聚合函数
聚合函数 多行聚合成一行的函数,如: 最大值max() 最小值min() 求和sum() 平均值avg() 计数count()
1 select max (sal),min (sal),sum (sal),avg (sal),count (1 ) from emp
count求的就是结果集里面有多少条有效数据(空值不计),可使用count(1)或者count(*)来求所有数据的计数
1 2 3 4 5 6 select deptno,max (sal) from emp group by deptnoselect deptno,count (1 ) from emp where job= 'MANAGER' group by deptnoselect deptno,job,avg (sal),count (1 ),sum (sal) from emp group by deptno,job
有分组的查询语句,那么聚合函数则在分组后产生
having 功能等同where:专门用于筛选聚合函数 与where的区别:
where是用来过滤行的,having是用来过滤分组的
having一般跟在group by子句后
where是在分组前进行过滤,而having是在分组后进行过滤
where后面不可以加聚合函数过滤条件,having可以
where效率比having高,因此只有出现聚合函数作为过滤条件时用having,其余所有情况都用where
where条件里为什么不能有聚合函数 :聚合函数是对行组进行计算,而执行顺序上是先where对单个行进行过滤,才分组,两者有个先后关系。
1 2 select deptno,count (1 ) from emp group by deptno having count (1 )> 4
having 只能筛选聚合函数和分组后的列
1 2 3 4 5 select deptno,count (1 ) from emp group by deptno having job= 'SALESMAN' select deptno,count (1 ) from emp where job= 'SALESMAN' group by deptno
练习 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 select enmae,sal from emp order by salselect * from emp order by deptno desc , salselect * from emp order by sal desc , deptno desc select deptno, count (1 ) from emp group by deptnoselect deptno, max (sal) from emp group by deptnoselect job, count (1 ) from emp group by jobselect max (sal) from empselect count (1 ) from deptselect count (1 ) from (select distinct deptno from emp)select deptno from emp group by deptno having avg (sal)> 1600 select deptno from emp where deptno!= 20 group by deptno having avg (sal)> 1600 select job from emp group by job having min (sal)> 1500 select deptno,count (1 ), avg (sal) from emp group by deptnoselect deptno,min (sal) from emp where job= 'MANAGER' group by deptnoselect deptno, job, sum (sal) from emp group by deptno, job order by deptnoselect deptno,min (sal),max (sal),count (1 ) from emp group by deptno having count (1 )> 2 select deptno,count (1 ),avg (sal) from emp group by deptno having avg (sal)> 2000 and count (1 )> 2 order by count (1 )select job,avg (sal) from emp where job not like 'SA%' group by job having avg (sal)> 2500 order by avg (sal) desc select job,sum (sal) from emp where job!= 'SALESMAN' group by job having sum (sal)>= 2500 select deptno,max (sal)- min (sal) from emp group by deptno