AVG()
SUM()
MAX()
MIN()
COUNT()
#求平均工资和工资总和 SELECT AVG(salary),SUM(salary) FROM employees;
注:不能处理字符串
#求最高工资和最低工资 SELECT AVG(salary),SUM(salary) FROM employees;
注:可以处理字符串,数值类型,日期时间类型的字段(或变量)
COUNT():计算指定字段在查询结果中出现的个数
SELECT COUNT('*') FROM employees
注:不包括null
#求各个部门的平均工资 SELECT department_id,AVG(salary) FROM employees GROUP BY department_id;
#求各个部门同一工种的平均工资 SELECT department_id,job_id,AVG(salary) FROM employees GROUP BY department_id,job_id;#没有先后之分
非组函数的字段必须声明在GROUP BY中
GROUP BY在FROM和WHERE的后面,在ORDER BY和LIMIT的后面
会多出一行(在最后),是总的平均值
不能用ORDER BY 排序
#求各个部门的平均工资 SELECT department_id,AVG(salary) FROM employees GROUP BY department_id WITH ROLLUP;
#查部门id是10,20,30,40四个部门最高工资超过10000的部门信息 方式一:推荐(效率高) SELECT department_id,sum(salary) FROM employees WHERE department——id IN(10,20,30,40) GROUP BY department_id HAVING sum(salary) > 10000; 方式二: SELECT department_id,sum(salary) FROM employees GROUP BY department_id HAVING sum(salary) > 10000 and department——id IN(10,20,30,40);
#SQL92 SELECT...,...(存在组函数) FROM ...,... WHERE 多表的连接 AND 不包含聚合函数的过滤条件 GROUP BY...,... HARING BY包含聚合函数的过滤条件 ORDER BY...,...(ASC \ DESC) LIMIT...,... #SQL99 SELECT...,...(存在组函数)#2 FROM ... (LEFT \ RIGHT)JOIN ...#1 ON多表的连接条件 (LEFT \ RIGHT)JOIN ... ON 多表的连接条件 WHERE 不包含聚合函数的过滤条件 GROUP BY...,... HARING BY包含聚合函数的过滤条件 ORDER BY...,..(ASC \ DESC)#3 LIMIT...,...
FROM->ON->(LEFT \ RIGHT JOIN)->WHERE->GROUP BY->HAVING->SELECT->DISTINCT->ORDER BY->LIMIT
同上