AfterAcademy Tech
•
29 Feb 2020

What is the difference between HAVING and WHERE clause is one the most popular question asked in interviews especially to the freshers? Though these two clauses are very similar and they primarily restrict the tuples returned by the SELECT statement, the main difference arises when used with the GROUP BY clause. So, lets first learn about these two clauses and then their differences.
The WHERE clause is used to fetch the data which specify the given condition. It is used to filter records and select only necessary records. It is used with SELECT, UPDATE, DELETE, etc. query. The SQL also implements and, or, and not in the WHERE clause which is known as the boolean condition.
Example: Take an example of a table Transaction that has ‘Item’, 'Month' and ‘Amount’ as attributes.

If we want to calculate the total sale by two products TV and Fridge then our Query statement would be:
SELECT Item, sum(Amount) AS Net_amount
FROM Transaction
WHERE Item in ( ‘TV’, ‘Fridge’)
GROUP BY Item;
The output of the above query will be:

The HAVING clause is generally used along with the GROUP BY clause. This clause is used in the column operation and is applied to aggregate rows or groups according to given conditions.
Example: Consider the above example. Now, if we want to calculate the Net_amount of the total sale by two products TV and Fridge then our query statement would be:
SELECT Item, sum(Amount) AS Net_amount
FROM Transaction
GROUP BY Item
HAVING Item in (‘TV’, ‘Fridge’);
The output of the above query will be:

Now, if we want only those items which have the net_amount greater than 1200. The query can be written as:
SELECT Item, sum(Amount) AS Net_amount
FROM Transaction
GROUP BY Item
HAVING sum(Net_amount)>1200;
The output of the above query will be:

This cannot be done in the WHERE clause. It generates an error message when we use the WHERE clause with the aggregate functions.
Example: The following query has the same results. The WHERE clause uses the “age” index but the having clause will scan the full table instead of directly looking at the "age" column.
SELECT * FROM Table WHERE age = 10
2. HAVING clause
SELECT * FROM Table HAVING age = 10
This was a discussion on the differences between WHERE and HAVING clause. Hope you learned something new today.
Do share this blog with your friends to spread the knowledge. Visit our YouTube channel for more content. You can read more blogs from here.
Keep Learning 🙂
Team AfterAcademy!
AfterAcademy Tech
In this blog, we will learn the difference between SQL and MySQL. People use these terms intechangeably. But both are having different meanings.

AfterAcademy Tech
In this blog, we will learn what is RDBMS and how it is different from DBMS. People often use these words intechangeably. But there is a difference between these two terms. So, let's learn how.

AfterAcademy Tech
In this blog, we will learn about the difference between Mutex and Semaphore in the Operating System. We will first learn about Mutex and Semaphore and after that, we will find the difference between them.

AfterAcademy Tech
In this blog, we will learn the difference between SQL and NoSQL databases.
