It is used to filter groups of rows returned by the GROUP BY clause. This is why the HAVING clause is usually used with the GROUP BY clause. If there is no GROUP BY clause, the HAVING clause is applied to the entire result as a single group.
The HAVING clause is applied to each group of the grouped table, much as a WHERE clause is applied to a select list. For each group segregated by GROUP BY clause, having clause can be applied to filter that data.
HAVING can be used without GROUP BY but the utilization is pointless according to me. When to use having clause in SQL? What is having in SQL? You could also use the SQL SUM function to return the name of the department and the total sales (in the associated department).
You can use AND-s or OR-s with the conditions as the following is looking for lines with SUM(REVENUE) is 1and name is starting with “O“. The WHERE clause limits the rows evaluated. This article introduces you to some of the more commonly used SQL group functions, along with the GROUP BY and HAVING clauses.
Using the HAVING Clause: 3. Any conditions based on the outcome of a group function must be in the HAVING clause: 4.