Sql 简明教程
SQL - Having Clause
The SQL HAVING Clause
SQL HAVING clause 类似于 WHERE 子句;两者都用于根据指定条件过滤表中的行。然而,HAVING 子句用于过滤分组行而非单行。这些行由 GROUP BY 子句分组在一起,因此 HAVING 子句必须始终在 GROUP BY 子句后面。
The SQL HAVING clause is similar to the WHERE clause; both are used to filter rows in a table based on specified criteria. However, the HAVING clause is used to filter grouped rows instead of single rows. These rows are grouped together by the GROUP BY clause, so, the HAVING clause must always be followed by the GROUP BY clause.
此外,HAVING 子句可与聚合函数(如 COUNT()、SUM()、AVG() 等)配合使用,而 WHERE 子句不能与它们一起使用。
Moreover, the HAVING clause can be used with aggregate functions such as COUNT(), SUM(), AVG(), etc., whereas the WHERE clause cannot be used with them.
Syntax
以下是 SQL HAVING 子句的基本语法 −
Following is the basic syntax of the SQL HAVING clause −
SELECT column1, column2, aggregate_function(column)
FROM table_name
GROUP BY column1, column2
HAVING condition;
以下代码块显示了 HAVING 子句在查询中的位置 −
The following code block shows the position of the HAVING Clause in a query −
SELECT
FROM
WHERE
GROUP BY
HAVING
ORDER BY
HAVING with GROUP BY Clause
我们可以将 HAVING 子句与 GROUP BY 子句一起使用来过滤满足特定条件的行组。它用于在执行聚合后对结果集应用过滤器。
We can use the HAVING clause with the GROUP BY clause to filter groups of rows that meet certain conditions. It is used to apply a filter to the result set after the aggregation has been performed.
Example
假设我们创建了一个名为 CUSTOMERS 的表,其中包含客户的个人详细信息,包括他们的姓名、年龄、地址和薪水,请使用以下查询:
Assume we have created a table named CUSTOMERS, which contains the personal details of customers including their name, age, address and salary, using the following query −
CREATE TABLE CUSTOMERS (
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25),
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);
现在使用 INSERT 语句向该表中插入值,如下所示:
Now insert values into this table using the INSERT statement as follows −
INSERT INTO CUSTOMERS VALUES
(1, 'Ramesh', 32, 'Ahmedabad', 2000.00),
(2, 'Khilan', 25, 'Delhi', 1500.00),
(3, 'Kaushik', 23, 'Kota', 2000.00),
(4, 'Chaitali', 25, 'Mumbai', 6500.00),
(5, 'Hardik', 27, 'Bhopal', 8500.00),
(6, 'Komal', 22, 'Hyderabad', 4500.00),
(7, 'Muffy', 24, 'Indore', 10000.00);
创建的表如下所示:
The table created is as shown below −
ID |
NAME |
AGE |
ADDRESS |
SALARY |
1 |
Ramesh |
32 |
Ahmedabad |
2000.00 |
2 |
Khilan |
25 |
Delhi |
1500.00 |
3 |
Kaushik |
23 |
Kota |
2000.00 |
4 |
Chaitali |
25 |
Mumbai |
6500.00 |
5 |
Hardik |
27 |
Bhopal |
8500.00 |
6 |
Komal |
22 |
Hyderabad |
4500.00 |
7 |
Muffy |
24 |
Indore |
10000.00 |
现在,我们基于 ADDRESS 和 AGE 列对 CUSTOMERS 表中的记录进行分组,并过滤其中 AGE 值小于 25 的组。
Now, we are grouping the records of the CUSTOMERS table based on the columns ADDRESS and AGE and filtering the groups where the AGE value is less than 25.
SELECT ADDRESS, AGE, MIN(SALARY) AS MIN_SUM
FROM CUSTOMERS
GROUP BY ADDRESS, AGE HAVING AGE > 25;
HAVING with ORDER BY Clause
ORDER BY 子句用于根据特定列(升序或降序)排列/排序 SELECT 查询结果的记录。如果我们将 ORDER BY 子句与 HAVING 子句一起使用,我们可以按所需顺序对过滤的组进行排序。
The ORDER BY clause is used to arrange/sort the records of the result of a SELECT query based on a specific column (either in ascending order or in descending order). If we use the ORDER BY clause with the HAVING clause we can sort the filtered groups in the desired order.
Example
以下查询基于 AGE 和 ADDRESS 列对 CUSTOMERS 表中的记录进行分组,过滤 SALARY 值小于 5000 的组,并按每组的总工资降序排列剩余的组。
Following query groups the records of the CUSTOMERS table based on the columns AGE and ADDRESS, filters the groups where the SALARY value is less than 5000 and, arranges the remaining groups in descending order based the total salaries of each group.
SELECT ADDRESS, AGE, SUM(SALARY) AS TOTAL_SALARY
FROM CUSTOMERS
GROUP BY ADDRESS, AGE HAVING TOTAL_SALARY >=5000
ORDER BY TOTAL_SALARY DESC;
HAVING Clause with COUNT() Function
HAVING 子句可与 COUNT() 函数一起使用来根据组包含的行数过滤组。
The HAVING clause can be used with the COUNT() function to filter groups based on the number of rows they contain.
HAVING Clause with AVG() Function
HAVING 子句还可以与 AVG() 函数一起使用来根据指定列的平均值过滤组。
The HAVING clause can also be used with the AVG() function to filter groups based on the average value of a specified column.
HAVING Clause with MAX() Function
我们还可以使用 HAVING 子句与 MAX() 函数一起,根据指定列的最大值来过滤组。
We can also use the HAVING clause with MAX() function to filter groups based on the maximum value of a specified column.