分組
之前都是單條記錄數據篩選分析,分組就可以對一組記錄數據進行篩選分析。
group by 字段
用來對數據進行分組
-- 每個部門有幾個員工
select deptno,count(1) from emp group by deptno
運行結果 |
---|
Tips:
- 如果出現了分組,select後就不能在出現非分組信息
group by 字段
關鍵字不能出現在select
中
組過濾
having
關鍵字是組過濾
where:過濾行信息,不能使用組顧慮
having:過濾組,可以使用組函數
-- 每個部門平均工資高於2000的
select deptno,count(1) from emp group by deptno having avg(sal) > 2000
運行結果 |
---|