Important differences between SQL HAVING vs WHERE Clause

1. Introduction

In this article, We’ll discuss the difference between SQL Having vs Where Clauses. Additionally let’s also see “How to use”, “when to use” these Clauses.

2. SQL Having

Having clause is used with Select statement in combination with the GROUP BY Clause.

The aggregate functions (Average, Count, Count (*), Max, Min, Sum) summarizes the data and enables us to organize it into categories and subgroups with the help of Having Clause. This Clause retrieves the data from the Group rows not on individual rows which enables it to interact easily with Aggregate functions.

SELECT column1, 
FROM   table_name 
GROUP  BY column1 
HAVING column1 = 1; 

3. SQL Where

Where clause fetches the data from an individual row where it specifies a search condition of a Select Statement.

SELECT column1, 
FROM   table_name 
WHERE  column1 = 101; 

4. When to Use Having vs Where

Use Where Clause retrieve data from an individual row and it applies on each and every row.

Use Having Clause to filter data from group of rows alongside the GROUP BY Clause.

NOTE: Both of these Clauses can be used in the same SELECT query with an aggregate function. Before returning the data Where Clause will be applied first on individual rows to filter and results in creating groups only for which have been passed the condition then comes the Having Clause to filter Groups based upon the condition specified.

5. SQL Having vs Where clause

It is processed before the groups are createdIt is processed after the groups are created
SQL evaluates the condition before the aggregates take placeSQL evaluates the condition after the aggregates take place

6. Conclusion

To summarize, we have seen differences between SQL Having vs where clause and also when to use it appropriately.

If you still have questions, feel free to ask in our SQL forum.

Frequently asked questions

What are Oracle Reserved Keywords?

Oracle reserved keywords can be found in this Link.

How to use Oracle reserved keywords for object names?

Enclose the Oracle reserved keyword inside a “double-quotes” to indicate that is being used for object names.

Can we just use Having instead of Where?

Well, sometimes we can and sometimes we can’t. Since they both filter records it depends on what we’re filtering on.