#count
#作用:计算指定字段在查询结果中出现的个数
SELECT count(employee_id),count(salary),count
(salary*12)
FROM employees;
SELECT count(1)
FROM employees;
SELECT count(4)
FROM employees;
SELECT count(*)
FROM employees;
#计算表中有多少条记录,如何实现?
#方式一:count(常数)
#方式二:count(*)
#方式三:count(具体的字段)不一定对
#注意:计算指定字段出现的个数时是不计算null的
SELECT count(commission_pct)
FROM employees;
SELECT commission_pct
FROM employees
WHERE commission_pct IS NOT NULL;
#AVG = SUM / COUNT
SELECT AVG(salary),SUM(salary)/COUNT(salary),
AVG(commission_pct),SUM(commission_pct)/COUNT( commission_pct),
SUM(commission_pct)/107
FROM employees;
#直观感觉
#需求:查询公司中的平均奖金率
SELECT AVG(commission_pct)
FROM employees;
#换一个场景:查询每一个中国人的工资多少,其中两亿人没有工资,这两亿人需不需要计算在内?
#需要
#以下正确107,上边写的是错误的35
SELECT SUM(commission_pct)/count(1)
FROM employees;
#GROUP BY
#需求:查询各部门的平均工资,最高工资
#以下不对,这是把所有107个员工看成了一个部门
SELECT AVG(salary),MAX(salary)
FROM employees;
#以下正确
SELECT department_id,AVG(salary),MAX(salary)
FROM employees
GROUP BY department_id;
#需求:查询各个job_id的平均工资
SELECT job_id,avg(salary)
FROM employees
GROUP BY job_id;
#需求:查询各个部门各个job_id的平均工资
SELECT department_id,job_id,AVG(salary)
FROM employees
GROUP BY department_id,job_id;
#结论1:SELECT中出现的非组函数的字段必须声明在GROUP BY里
#反之,GROUP BY 中声明的字段可以不出现在SELECT里
#结论2:GROUP BY声明在from后面,在where的后面,ORDER BY的前面,在LIMIT的前面