select 分组函数,列(要求出现在group by的后面)
from 表
【where 筛选条件】
group by 分组的列表
【having 分组后的筛选】
【order by 子句】
? 数据源 位置 关键字
分组前筛选 原始表 group by子句的前面 where
分组后筛选 分组后的结果集 group by子句的后面 having
SELECT
MAX(salary),
job_id
FROM
employees
GROUP BY job_id ;
SELECT
COUNT(*),
location_id
FROM
departments
GROUP BY location_id ;
SELECT
AVG(salary),
department_id
FROM
employees
WHERE email LIKE ‘%a%‘
GROUP BY department_id ;
SELECT
MAX(salary),
manager_id
FROM
employees
WHERE commission_pct IS NOT NULL
GROUP BY manager_id ;
SELECT
COUNT(*) AS 员工个数,
department_id
FROM
employees
GROUP BY department_id ;
SELECT
COUNT(*) AS 员工个数,
department_id
FROM
employees
GROUP BY department_id
HAVING 员工个数 > 2 ;
SELECT
MAX(salary),
job_id
FROM
employees
WHERE commission_pct IS NOT NULL
GROUP BY job_id ;
SELECT
MAX(salary) AS 最高工资,
job_id
FROM
employees
WHERE commission_pct IS NOT NULL
GROUP BY job_id
HAVING 最高工资 > 12000 ;
SELECT
MIN(salary) AS 最低工资,
manager_id
FROM
employees
WHERE manager_id > 102
GROUP BY manager_id
HAVING 最低工资 > 5000 ;
SELECT
COUNT(*) 员工个数,
LENGTH(last_name) 姓名长度
FROM
employees
GROUP BY 姓名长度 ;
SELECT
COUNT(*) 员工个数,
LENGTH(last_name) 姓名长度
FROM
employees
GROUP BY 姓名长度
HAVING 员工个数 > 5 ;
SELECT
AVG(salary),
department_id,
job_id
FROM
employees
GROUP BY department_id,
job_id ;
SELECT
AVG(salary) AS 平均工资,
department_id,
job_id
FROM
employees
GROUP BY department_id,
job_id
ORDER BY 平均工资 DESC ;
SELECT
MAX(salary),
MIN(salary),
AVG(salary),
SUM(salary),
job_id
FROM
employees
GROUP BY job_id
ORDER BY job_id ;
SELECT
MAX(salary) - MIN(salary) AS DIFFERENCE
FROM
employees ;
SELECT
MIN(salary) AS 最低工资
FROM
employees
WHERE manager_id IS NOT NULL
GROUP BY manager_id
HAVING 最低工资 >= 6000 ;
SELECT
department_id,
COUNT(*) AS 员工数量,
AVG(salary) AS 工资平均值
FROM
employees
GROUP BY department_id
ORDER BY 工资平均值 DESC ;
SELECT
COUNT(*),
job_id
FROM
employees
GROUP BY job_id ;
原文链接:https://blog.csdn.net/qq_21579045/article/details/98111827
原文:https://www.cnblogs.com/hxl-learning-space/p/14551793.html