标签:ike 技巧 ast 包含 字符 分组查询 员工 manager email
select 查询的字段,分组函数
from 表
where 筛选条件
group by 分组的字段
having 分组后的字段筛选
order by 子句
分组查询中的筛选条件分为两类
group by分组
分组筛选
针对的表 位置 关键字
可以支持排序,排序是放在整个分组排序的后面
having后可以支持别名
select max(salary), job_id
from employees
group by job_id;
select count(*),location_id
from employees
group by location_id;
select max(email), 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 null
group by manager_id;
# 查询每个部门的员工个数
select count(*) ,department_id
from employees
group by department_id
# 根据1的结果进行筛选,查询那个部门的员工个数>2
# 根据分组筛选后的结果集,得出最后的结果
having count(*)>2;
# 查询每个工种有奖金的员工的最高工资
select max(salary), job_id
from employees
where commission_pct is not null
group by job_id
# 根据1的结果继续查询
having max(salary)>12000;
# 查询领导编号>102的每个领导手下的员工的最低工资
# 添加筛选条件>102
select min(salary), manager_id
from employees
where manager_id > 102
group by manager_id
# 最低工资>5000的领导编号
having min(salary)>5000;
# 查询每个长度的员工个个数
select count(*), length(last_name) len_name
from employees
group by length(last_name)
# 添加筛选条件
having count(*)>5;
select avg(salary),department_id,job_id
from employees
where department_id is not null
group by department_id,job_id
having a>10000
order by avg(salary) desc;
标签:ike 技巧 ast 包含 字符 分组查询 员工 manager email
原文地址:https://www.cnblogs.com/qifanren/p/14814500.html