分组查询知识点以及案例

  1 #进阶5:分组查询
  2 /*
  3 语法:    SELECT 分组函数,列(要求出现在group by的后面)
  4           from 表
  5                     GROUP BY 分组的列表
  6                     [order by子句]
  7 注意:    查询列表必须特殊,要求是分组函数和group by后出现的字段
  8 特点:
  9           1、分组查询中断筛选条件为两类
 10                        数据源              位置                     关键字
 11           分组前筛选   原始表              GROUP BY子句的前面      WHERE
 12                     分组后筛选   分组后的结果集      GROUP BY子句的后面      HAVING
 13                     ①分组函数做条件肯定是放在having子句中
 14                     ②能用分组前筛选的,就优先考虑使用分组.
 15                     2.GROUP BY子句支持单个字段分组,多个字段分组(多个字段之间用逗号隔开没有顺序要求),表达式或函数(用得较少)
 16                     3、也可以添加排序(排序放在整个分组查询的最后)
 17 */
 18 #引入:查询每个部门的平均工资
 19 SELECT avg(salary ) FROM employees;
 20 
 21 #简单的分组查询
 22 #案例1:查询每个工种的最高工资
 23 SELECT MAX(salary),job_id
 24 FROM employees
 25 GROUP BY job_id;
 26 
 27 #案例2、查询每个位置上的部门个数
 28 SELECT COUNT(*),location_id
 29 FROM departments
 30 GROUP BY location_id;
 31 
 32 #添加筛选条件
 33 #案例1:查询有幸中包含a字符的,每个部门的平均工资
 34 
 35 SELECT avg(salary),department_id
 36 FROM employees
 37 WHERE email LIKE '%a%'
 38 GROUP BY department_id;
 39 
 40 #案例2:查询有奖金的每个领导手下员工的最高工资
 41 SELECT MAX(salary),manager_id
 42 FROM employees
 43 WHERE commission_pct is not NULL
 44 GROUP BY manager_id;
 45 
 46 #添加复杂的筛选条件
 47 
 48 #案例1:查询那个部门的员工个数>2
 49 #①查询每个部门的员工个数
 50 SELECT COUNT(*),department_id
 51 FROM employees
 52 GROUP BY department_id;
 53 
 54 #②根据①的结果进行筛选,查询那个部门的员工个数>2
 55 SELECT COUNT(*),department_id
 56 FROM employees
 57 GROUP BY department_id
 58 HAVING COUNT(*)>2;
 59 
 60 #案例2:查询每个工种有奖金的员工的最高工资>12000的工种变化
 61 #① 查询每个工种有奖金的员工的最高工资
 62 
 63 SELECT MAX(salary),job_id
 64 FROM employees
 65 WHERE commission_pct is not NULL
 66 GROUP BY job_id;
 67 
 68 #②根据①结果继续筛选,最高工资>12000
 69 SELECT MAX(salary),job_id
 70 FROM employees
 71 WHERE commission_pct is not NULL
 72 GROUP BY job_id
 73 HAVING MAX(salary)>12000;
 74 
 75 #案例3:查询领导编号>102的每个领导手下的最低工资>5000的领导编号是哪个,以及最低工资
 76 SELECT manager_id,MIN(salary)
 77 FROM employees
 78 WHERE manager_id>102
 79 GROUP BY manager_id
 80 HAVING MIN(salary)>5000;
 81 
 82 #按表达式或函数分组
 83 
 84 #案例:按员工姓名的长度分组,查询每一组的员工个数,筛选员工个数>5的有哪些
 85 SELECT COUNT(*) c,LENGTH(last_name) len_name
 86 FROM employees
 87 GROUP BY len_name
 88 HAVING C>5;
 89 
 90 #按多个字段分组
 91 
 92 #案例:查询每个部门每个工种的员工的平均工资
 93 SELECT avg(salary) ,department_id,job_id
 94 FROM employees
 95 GROUP BY job_id,department_id;
 96 
 97 #添加排序
 98 #案例:查询每个部门每个工种的员工的平均工资,按平均工资的高低来排序
 99 SELECT avg(salary) ,department_id,job_id
100 FROM employees
101 WHERE department_id is not NULL
102 GROUP BY job_id,department_id
103 HAVING avg(salary)>10000
104 ORDER BY avg(salary) desc;
105 
106 #1.查询各job_id的员工工资的最大值,最小值,平均值,总和,并按job_id升序
107 SELECT MAX(salary) a,MIN(salary) i,avg(salary) avg,SUM(salary) sum
108 FROM employees
109 GROUP BY job_id
110 ORDER BY job_id ;
111 
112 
113 #2.查询员工最高工资和最低工资的差距( DIFFERENCE114 SELECT  MAX(salary)-MIN(salary) difference
115 FROM employees;
116 
117 
118 #3.查询各个管理者手下员工的最低工资,其中最低工资不能低于6000,没有管理者的员工不计算在内
119 SELECT min(salary),manager_id
120 FROM employees
121 WHERE manager_id is not null
122 GROUP BY manager_id
123 HAVING MIN(salary)>=6000;
124 
125 #4.查询所有部门的编号,员工数量和工资平均值,并按平均工资降序
126 SELECT department_id, COUNT(*),avg(salary) a
127 FROM employees
128 GROUP BY department_id
129 ORDER BY a DESC;
130 
131 
132 #5.选择具有各个job_id的员工人数
133 SELECT COUNT(*),job_id
134 FROM employees
135 WHERE job_id is not NULL
136 GROUP BY job_id;

 

posted @ 2020-07-05 11:15  自律即自由-  阅读(383)  评论(0编辑  收藏  举报