Understanding the HAVING Clause in SQL
Q: What is the purpose of the HAVING clause in SQL?
- SQL
- Senior level question
Explore all the latest SQL interview questions and answers
ExploreMost Recent & up-to date
100% Actual interview focused
Create SQL interview for FREE!
The HAVING clause in SQL is used to filter data based on a specified condition after the GROUP BY clause is applied. It is used to limit the groups of records returned in a query by grouping them according to the values of one or more columns. The HAVING clause is used only with the SELECT statement.
For example, let's say you have a table called "sales_data" with the following columns:
- Salesperson_id
- Date
- Sales
You can use the HAVING clause to find out the total sales for each salesperson in a certain month (e.g. June).
The query would look like this:
SELECT salesperson_id, SUM(sales)
FROM sales_data
WHERE Date = 'June'
GROUP BY salesperson_id
HAVING SUM(sales) > 1000;
This query will return the salesperson_id and total sales for each salesperson who had more than 1000 in sales for the month of June.
In summary, the purpose of the HAVING clause in SQL is to filter data based on a specified condition after the GROUP BY clause is applied. It is used to limit the groups of records returned in a query by grouping them according to the values of one or more columns.
For example, let's say you have a table called "sales_data" with the following columns:
- Salesperson_id
- Date
- Sales
You can use the HAVING clause to find out the total sales for each salesperson in a certain month (e.g. June).
The query would look like this:
SELECT salesperson_id, SUM(sales)
FROM sales_data
WHERE Date = 'June'
GROUP BY salesperson_id
HAVING SUM(sales) > 1000;
This query will return the salesperson_id and total sales for each salesperson who had more than 1000 in sales for the month of June.
In summary, the purpose of the HAVING clause in SQL is to filter data based on a specified condition after the GROUP BY clause is applied. It is used to limit the groups of records returned in a query by grouping them according to the values of one or more columns.


