SQL – Having Clause
Mục Lục
SQL – Having Clause
SQL HAVING clause is similar to the WHERE clause; they are both used to filter rows in a table based on conditions. However, the HAVING clause was included in SQL 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. It can be used with aggregate functions, whereas the WHERE clause cannot.
HAVING clause can also contain multiple conditions in it to filter the grouped results. These conditions are separated by various operators like AND, OR etc.
Syntax
Following is the basic syntax of the HAVING clause in SQL −
SELECT column1, column2, aggregate_function(column) FROM table_name GROUP BY column1, column2 HAVING condition;
The following code block shows the position of the HAVING Clause in a query.
SELECT FROM WHERE GROUP BY HAVING ORDER BY
HAVING clause with ORDER BY clause
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
Assume we have created a table with name CUSTOMERS in SQL database using CREATE TABLE statement as shown below −
CREATE TABLE CUSTOMERS ( ID INT NOT NULL, NAME VARCHAR (20) NOT NULL, AGE INT NOT NULL, GENDER VARCHAR (25), SALARY DECIMAL (18, 2), PRIMARY KEY (ID) );
Following query inserts values into this table using the INSERT statement −
insert INTO CUSTOMERS VALUES(1, 'Ramesh', 25, 'Male', 2000.00); insert INTO CUSTOMERS VALUES(2, 'Ramesh', 25, 'Male', 1500.00); insert INTO CUSTOMERS VALUES(3, 'kaushik', 25, 'Female', 2000.00); insert INTO CUSTOMERS VALUES(4, 'kaushik', 20, 'Male', 6500.00); insert INTO CUSTOMERS VALUES(5, 'Hardik', 25, 'Male', 8500.00); insert INTO CUSTOMERS VALUES(6, 'Komal', 20, 'Female', 4500.00); insert INTO CUSTOMERS VALUES(7, 'Muffy', 25, 'Male', 10000.00);
If we verify the contents of the CUSTOMERS table using the SELECT statement, we can observe the inserted records as shown below −
SELECT * from CUSTOMERS; +----+---------+-----+--------+----------+ | ID | NAME | AGE | GENDER | SALARY | +----+---------+-----+--------+----------+ | 1 | Ramesh | 25 | Male | 2000.00 | | 2 | Ramesh | 25 | Male | 1500.00 | | 3 | kaushik | 25 | Female | 2000.00 | | 4 | kaushik | 20 | Male | 6500.00 | | 5 | Hardik | 25 | Male | 8500.00 | | 6 | Komal | 20 | Female | 4500.00 | | 7 | Muffy | 25 | Male | 10000.00 | +----+---------+-----+--------+----------+
Now, we are trying to retrieve all records from the CUSTOMERS table where the sum of their salary is less than 4540, ordered by their name in ascending order −
SELECT NAME, SUM(SALARY) as total_salary FROM CUSTOMERS GROUP BY NAME HAVING SUM(SALARY) < 4540 ORDER BY NAME
Output
The result produced is as follows −
+--------+--------------+ | NAME | total_salary | +--------+--------------+ | Komal | 4500.00 | | Ramesh | 3500.00 | +--------+--------------+
HAVING clause with COUNT() function
The HAVING clause can be used with the COUNT() function to filter groups based on the number of rows they contain.
Example
Following is the query, which would display a record where count of similar age is greater than or equal to 3.
SELECT AGE FROM CUSTOMERS GROUP BY age HAVING COUNT(age) >= 3
Output
This would produce the following result −
+-----+ | AGE | +-----+ | 25 | +-----+
HAVING clause with AVG() function
The HAVING clause can also be used with the AVG() function to filter groups based on the average value of a specified column.
Example
Now, we are trying to retrieve the gender of the customers whose average salary is greater than 5240 −
SELECT gender, AVG(salary) as avg_salary FROM customers GROUP BY gender HAVING AVG(salary) > 5240
Output
Following is the output of the above query −
+--------+-------------+ | gender | avg_salary | +--------+-------------+ | Male | 5700.000000 | +--------+-------------+
HAVING clause with MAX() function
We can also use the HAVING clause with MAX() function to filter groups based on the maximum value of a specified column.
Example
In here, we are trying to retrieve the gender of the customers whose maximum salary is less than 5240 −
SELECT gender, MAX(salary) as max_salary FROM customers GROUP BY gender HAVING MAX(salary) < 5240
Output
The result obtained is as follows −
+--------+-------------+ | gender | max_salary | +--------+-------------+ | Female | 4500.00 | +--------+-------------+
Advertisements