In this post, We will discuss the difference between where and having clauses in SQL server with the examples.
we will be using the sales table. I’ve already created this table.
Here is the SQL script to create and populate with test data.
TblProducts Table has the product information and TblProductSale table contains sales-related data. So when any customer purchases any product, an entry will be made into the sale table.
If you look at the data that we have here, notice we have sold Books twice, Mobile Phone twice, Watches & Computer Accessories. Once.
Now we want to write a query that’s going to calculate the total sales by product. So the result of that query should be like the below image.
We want to calculate total sales by product. So we are using GROUP BY on the product column since we won’t the total sale amount by using the SUM() aggregate function on the sale amount column.
Now we want to find only those products where the total sales is greater than 200.
We can very easily achieve this using having Clause. Let’s look at that in action. So I am going to use the having clause and we want to filter based on this total sales column.
So when we execute this, we should get only those products where the total sales is greater than in 2000.
So here we are using the having clause with Sum() aggregate function to filter the groups.
Now, what do you think?what is going to happen if we use the where clause instead of having? When we execute it, we get a syntax error straightaway.
- So this is one of the primary differences between where and having clauses, where clauses cannot be used with the aggregates, whereas having can be used with the aggregates.
- There are other differences as well -where clause filters rows before aggregate calculations are performed, whereas having class filters rows after the aggregate calculations are performed.
Let’s understand what we mean by this. Now we want to calculate the total sales of Books and Mobile Phone so we know the products already.
We want to compute the total sales for those two products, there are two ways we can achieve this. We can achieve this either by using where or having clause.
Let’s look at that in action. So I’m going to use the where clause here and we want to compute the total sales for Books and Mobile Phone
So when we execute the above query, we get the total sales for Books and Mobile Phone.
Now we can achieve the same thing using having as well.
We get the same result. Now, what is the difference between these two queries?
The first query, is going to filter all the rows , It is going to retrieve only Books and Mobile Phone, and then it’s going to perform the aggregate calculation i.e Sum.
whereas with having, it’s going to retrieve all the products, it’s going to perform the aggregate calculation on all the products, and then it’s going to filter the groups.
So clearly we can see that we have a performance impact when we use having clause. So according to a performance standpoint, having is going to be slower than where and should be avoided when possible.
Because having is just going to filter the groups, whereas where filters, all the rows, it only gets Books and Mobile Phone and then performs the aggregation on those two products, whereas in having case it performs the aggregation for all the products and then filters Books and Mobile Phone groups.
So having is definitely slower than where in this case and should be avoided.