📜  Have 子句和 Group by 子句的区别

📅  最后修改于: 2021-09-08 15:51:56             🧑  作者: Mango

1. 有条款:
拥有子句基本上就像带有 GROUP BY 子句的聚合函数。使用 HAVING 子句代替 WHERE 与聚合函数。而GROUP BY子句将具有相同值的行分组为汇总行。 have 子句与 where 子句一起使用,以查找具有特定条件的行。总是在 group By 子句之后使用ifying 子句。

SELECT COUNT (SALARIES) AS COUNT_SALARIES, EMPLOYEES
FROM EMPLOYEES
GROUP BY SALARIES
HAVING COUNT(SALARIES) > 1; 

2. 按条款分组:
GROUP BY 子句通常与聚合函数(MAX、SUM、AVG)一起使用,以按一列或多列对结果进行分组,或者简单地说,我们可以说 GROUP BY 子句与 SELECT 语句配合使用来排列所需的数据成组。
GROUP BY 语句对具有相同值的行进行分组。该语句用在 where 子句之后。此语句通常与某些聚合函数一起使用,例如 SUM、AVG、COUNT atc。按一列或多列对结果进行分组。

SELECT COUNT (SALARIES) AS COUNT_SALARIES, EMPLOYEES
FROM EMPLOYEES
GROUP BY SALARIES; 

Have 子句和 Group by 子句的区别:

S.No. Having Clause GroupBy Clause
1. It is used for applying some extra condition to the query. The groupby clause is used to group the data according to particular column or row.
2. Having cannot be used without groupby clause. groupby can be used without having clause with the select statement.
3. The having clause can contain aggregate functions. It cannot contain aggregate functions.
4. It restrict the query output by using some conditions It groups the output on basis of some rows or columns.