查询工资总和大于9000的部门的编号以及工资和
SELECT deptno, SUM(sal) FROM employee GROUP BY deptno HAVING SUM(sal) > 9000;
+--------+----------+
| deptno | SUM(sal) |
+--------+----------+
| 20 | 10875.00 |
| 30 | 9400.00 |
+--------+----------+
HAVING和WHERE区别
- WHERE 对原表数据进行筛选
- HAVING 对过滤之后表进行筛选
书写MySQL的顺序
SELECT --> FROM --> WHERE --> GROUP BY --> HAVING --> ORDER BY --> LIMIT
查询工资大于1500的,工资总和大于6000的部门编号以及工资的和
SELECT deptno, SUM(sal) FROM employee WHERE sal>1500 GROUP BY deptno HAVING SUM(sal) > 6000 ORDER BY SUM(sal);
+--------+----------+
| deptno | SUM(sal) |
+--------+----------+
| 20 | 8975.00 |
+--------+----------+