group by:分组
A B
1 3
1 5
1 7
select A,avg(B) from table group by A;
得到:
A avg(B)
1,5
select的元素中,若非聚合,则必须出现在group by的后面
所谓聚合函数,是count(),avg(),sum(),max(),min()等函数,将多个数据通过运算合成一个数据。
having:查询条件中若有聚合元素,则要用having判断,不能用where
select A,avgB(B) from table group by A having avg(B)>3;
select * from table a where a.salary>( select avg(salary) from table b where b.department=a.department group by department);
找出同一department中salary大于平均值的数据项