## group by 字段名
*案例:統計每個部門的員工個數
```
select count(*) 員工個數,department_id from employees
group by department_id
```
使用group by 分組查詢后,select 后面能查詢的字段
只能是分組字段本身或分組函數(sum() avg() max() min() count())
## where,having的區別:
where用于對原始表中已有的字段進行篩選
having用于對原始表中沒有的字段(通過操作才有)進行篩選
where通常放在group by 之前,having通常放在group by 之后,且一般只用在有group by的地方
```
SELECT job_id,max(salary) m
from employees
WHERE commission_pct is not null
GROUP BY job_id
HAVING m>6000
ORDER BY max(salary)
LIMIT 0,1; //limit用于分頁(第一頁限制1條數據)
```
------------------------------
結果為:
|job_id|m
| --- | --- |
|SA_REP|11500
------------------------------