- SQL - Discussion
- SQL - Useful Resources
- SQL - Useful Functions
- SQL - Quick Guide
- SQL - Questions and Answers
- SQL - Datatype Functions
- SQL - Conversion Functions
- SQL - JSON Functions
- SQL - Cursor Functions
- SQL - Logical Functions
- SQL - Statistical Functions
- SQL - Text & Image Functions
- SQL - Numeric Functions
- SQL - Aggregate Functions
- SQL - String Functions
- SQL - Date Functions
- SQL - Database Tuning
- SQL - IN vs EXISTS
- SQL - Group By vs Order By
- SQL - Common Table Expression
- SQL - Cursors
- SQL - Date & Time
- SQL - Auto Increment
- SQL - Using Sequences
- SQL - Handling Duplicates
- SQL - Sub Queries
- SQL - Transactions
- SQL - NULL Values
- SQL - Stored Procedures
- SQL - Default Constraint
- SQL - Check Constraint
- SQL - Null Functions
- SQL - Min & Max
- SQL - Hosting
- SQL - Injection
- SQL - Comments
- SQL - Wildcards
- SQL - Non-Clustered Index
- SQL - Clustered Index
- SQL - Unique Index
- SQL - Primary Key
- - 工会诉Join
- SQL - Inner Join
- SQL - Using Joins
- SQL - Aliases
- SQL - EXCEPT Operator
- SQL - INTERSECT Operator
- SQL - UNION vs UNION ALL
- SQL - UNION Operator
- SQL - BETWEEN Operator
- SQL - NOT NULL
- SQL - IS NOT NULL
- SQL - IS NULL
- SQL - NOT EQUAL
- SQL - NOT Operator
- SQL - CASE
- SQL - EXISTS Operator
- SQL - ANY, ALL Operators
- SQL - IN Operator
- SQL - LIKE Operator
- SQL - BOOLEAN (BIT) Operator
- SQL - AND & OR
- SQL - Having Clause
- SQL - Group By Clause
- SQL - Order By Clause
- SQL - Distinct Clause
- SQL - Top Clause
- SQL - Where Clause
- SQL - Rename Views
- SQL - Drop Views
- SQL - Update Views
- SQL - Create Views
- SQL - Sorting Results
- SQL - Delete Query
- SQL - Update Query
- SQL - Insert Into Select
- SQL - Select Into
- SQL - Select Query
- SQL - Insert Query
- SQL - Constraints
- SQL - Delete Table
- SQL - Drop Table
- SQL - Alter Tables
- SQL - Temporary Tables
- SQL - Clone Tables
- SQL - Truncate Table
- SQL - Rename Table
- SQL - Show Tables
- SQL - Create Table
- SQL - Backup Database
- SQL - Show Database
- SQL - Rename Database
- SQL - Select Database
- SQL - Drop Database
- SQL - Create Database
- SQL - Expressions
- SQL - Operators
- SQL - Data Types
- SQL - Syntax
- SQL - Databases
- SQL - RDBMS Concepts
- SQL - Overview
- SQL - Home
5. 图瓦卢
- 页: 1
- 页: 1
- 结构-创建指数
- 页: 1
- 页: 1
- 页: 1
- SQL - Foreign Key
- 文 件
- ∗ E/CN.6/2009/1。
- 页: 1
- 页: 1
- 文 件
- 页: 1
- 页: 1
- 文 件
- 页: 1
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
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 pke 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