## 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
------------------------------