GROUP BY
    分组函数
    添加筛选条件

    查询邮箱中包含a字符的,每个部门的平均工资

    1. SELECT
    2. avg(salary),
    3. department_id
    4. from
    5. employees
    6. where
    7. email like('%a%')
    8. group by
    9. department_id;

    image.png

    分组前筛选:

    例:
    查询有奖金的每个领导手下员工的最高工资

    1. #查询有奖金的每个领导手下员工的最高工资
    2. SELECT
    3. MAX( salary ),
    4. manager_id
    5. FROM
    6. employees
    7. WHERE
    8. commission_pct
    9. GROUP BY
    10. manager_id;

    分组后筛选
    查询哪个部门的员工个数>2

    1. SELECT
    2. count(*),
    3. department_id
    4. FROM
    5. employees
    6. GROUP BY
    7. department_id
    8. HAVING
    9. COUNT(*)>2;