语法:
SELECT <列名1>,<列名2>,<列名3>,......
FROM <表名>
【WHERE筛选条件】
GROUP BY <列名1>,<列名2>,<列名3>,......
【ORDER BY 字句】
注意:
查询列表必须特殊,要求是分组函数和group by后出现的字段
特点:
1,分组查询中的筛选条件分为两类。数据源不一样,所放的位置不一样
数据源 位置 关键字
分组前筛选 原始表 GROUP BY子句的前面 WHERE
分组后筛选 分组后的结果集 GROUP BY子句的后面 HAVING
(1)分组函数做条件肯定是放在having子句中
(2)能用分组前筛选的(在分组前[表中]能找到,分组后[结果集,即:视图]中也能找到),就优先考虑使用分组前筛选(提升性能)
#案列:查询有奖金的领导编号>108的每个领导手下员工的最高工资
方法1:
SELECT MAX(salary),manager_id
FROM employees
WHERE manager_id >108
GROUP BY manager_id;
方法2:
SELECT MAX(salary),manager_id
FROM employees
GROUP BY manager_id
HAVING manager_id >108;
2.GROUP BY子句支持单个字段分组,多个字段分组(多个字段之间用逗号隔开,没有顺序要求) ,表达式或函数(用得较少)
3.也可以添加排序(排序放在整个分组查询的后面)
#案列1:查询每个工种的最高工资
SELECT MAX(salary),job_id
FROM employees
GROUP BY job_id;
#案列2:查询每个位置上的部门个数
SELECT COUNT(*) as 部门个数,location_id
FROM departments
GROUP BY location_id;
#添加分组前的筛选条件
#案列1:查询邮箱中包含a字符的,每个部门的平均工资
SELECT AVG(salary) as 部门平均工资,department_id
FROM employees
WHERE email like ‘%a%‘
GROUP BY department_id;
#案列2:查询有奖金的每个领导手下员工的最高工资
SELECT MAX(salary),manager_id
FROM employees
WHERE commission_pct IS NOT NULL
GROUP BY manager_id;
#添加分组后的筛选条件
#案列1:查询哪个部门的员工个数>2
#(1)查询每个部门的员工个数
SELECT COUNT(*),department_id #COUNT(*)可以写为COUNT(employee_id)
FROM employees
GROUP BY department_id;
#(2)根据(1)的结果进行筛选,查询哪个部门的员工个数>2
错误1:
SELECT COUNT(*),department_id #报错:Invalid use of group function(对分组函数COUNT()无效的使用)。
FROM employees #因为关键字WHERE是从表employees查找COUNT(*)的,
WHERE COUNT(*)>2 #然而表employees中没有COUNT(*)这个字段的
GROUP BY department_id;
错误2:
SELECT COUNT(*),department_id #题的意思是根据执行完GROUP BY之后的结果进行筛选。
FROM employees #那么首先想到的是应该在GROUP BY 子句后追加条件,来进行筛选
GROUP BY department_id #用WHERE不行,因为必须用在GROUP BY前面,要连着FROM 表
WHERE COUNT(*)>2;
正解:
SELECT COUNT(*),department_id
FROM employees
GROUP BY department_id
HAVING COUNT(*)>2; #用HAVING实现分组后的筛选
#案列2:查询每个工种有奖金的员工的最高工资>12000的工种编号和最高工资
#(1)查询每个工种有奖金的员工的最高工资
SELECT MAX(salary),job_id # 如果一下子看不准筛选条件在GROUP BY前还是后,那么先把比较死的代码先写上
FROM employees #写上之后再加筛选
WHERE commission_pct IS NOT NULL #有奖金(commission_pct IS NOT NULL),涉及到的字段为commission_pct,
GROUP BY job_id #它在员工表employees里,那么,根据它来筛选就行。放在FROM employees后面,用WHERE关键字进行筛选
#(2)根据(1)的结果继续筛选,最高工资>12000
SELECT MAX(salary),job_id
FROM employees
WHERE commission_pct IS NOT NULL
GROUP BY job_id
HAVING MAX(salary)>12000;
#案列3:查询领导编号>102的每个领导手下员工的最低工资>5000的领导编号是哪个,以及其最低工资
#(1)查询每个领导手下员工的最低工资
SELECT MIN (salary)
FROM employees
GROUP BY manager_id
#(2)添加筛选条件:编号>102
SELECT MIN (salary)
FROM employees
WHERE manager_id >102 # manager_id在原始表中就有,因此用WHERE
GROUP BY manager_id
#(3)添加筛选条件:最低工资>5000
SELECT manager_id,MIN(salary)
FROM employees
WHERE manager_id >102
GROUP BY manager_id
HAVING MIN(salary)>5000;
#按表达式分组
#案列:按员工姓名的长度分组,查询每一组的员工个数,筛选员工个数>5的有哪些?
#(1)查询每个长度的员工个数
SELECT COUNT(employee_id),LENGTH(CONCAT(first_name,last_name))
FROM employees
GROUP BY LENGTH(CONCAT(first_name,last_name))
#(2)添加筛选条件
SELECT COUNT(employee_id) AS 员工个数,LENGTH(CONCAT(first_name,last_name))
FROM employees
GROUP BY LENGTH(CONCAT(first_name,last_name)) #Oracle不支持GROUP BY之后放别名
HAVING 员工个数>5;
#按多个字段分组
#案列:查询每个部门每个工种的平均工资
SELECT AVG(salary),department_id,job_id
FROM employees
GROUP BY department_id,job_id #原则:department_id,job_id 都一样才是一个小组。顺序可以调换,结果一样
#添加排序
#案列:查询每个部门每个工种的员工的平均工资,并且按平均工资的高低显示(DESC)
SELECT AVG(salary),department_id,job_id
FROM employees
GROUP BY department_id,job_id
ORDER BY AVG(salary) DESC;
#案列
1.查询各job_id的员工的工资的最大值,最小值,平均值,总和,并按job_id升序
SELECT MAX(LOWER(SALARY)),MIN(LOWER(SALARY)),AVG(LOWER(SALARY)),SUM(LOWER(SALARY)),job_id
FROM employees
GROUP BY job_id
ORDER BY job_id;
2.查询员工最高工资和最低工资的差距(DIFFRENCE)
SELECT MAX(salary)-MIN(salary) AS DIFFRENCE
FROM employees
3.查询各个管理者手下员工的最低工资,其中最低工资不能低于6000,没有管理者的员工不计算在内
SELECT MIN(salary),manager_id,CONCAT(first_name,‘-‘,last_name) AS 姓名
FROM employees
WHERE manager_id IS NOT NULL
GROUP BY manager_id
HAVING MIN(salary)>=6000;
4.查询所有部门的编号,员工数量和工资平均值,并按平均工资降序
SELECT department_id,COUNT(employee_id),AVG(salary)
FROM employees
GROUP BY department_id
ORDER BY AVG(salary) DESC;
5.选择具有各个job_id的员工人数
SELECT COUNT(employee_id),job_id
FROM employees
GROUP BY job_id;
原文地址:https://www.cnblogs.com/tan-y-q/p/10486204.html