Difference between WHERE vs HAVING Clause in SQL
Tags: sql, database, programming, development
Published: 2024-04-21 10:38:07

---
title: Difference between WHERE vs HAVING Clause in SQL
published: true
description: While both WHERE and HAVING are used for filtering rows, condition in WHERE clause is applied before grouping of data and condition on HAVING is applied after grouping 
tags: sql, database, programming, development
# cover_image: https://direct_url_to_image.jpg
# Use a ratio of 100:42 for best results.
# published_at: 2024-04-21 10:20 +0000
---
*Disclosure: This post includes affiliate links; I may receive compensation if you purchase products or services from the different links provided in this article.*

![Difference between WHERE and HAVING Clause in SQL](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qr9tuh2nv5bz2gqju8bg.png)

Hello devs, SQL questions are quite common on programming interviews and one of the popular SQL question is "WHERE vs HAVING clause"? When it comes to filtering records in SQL query, there are two main options, either by using WHERE clause or by using HAVING clause. 

While both [WHERE and HAVING are used for filtering rows](https://www.sqlrevisited.com/2022/02/how-to-filter-data-in-sql-where-clause.html), condition in WHERE clause is applied before grouping of data and condition on HAVING is applied after grouping 

I mean, the main difference between them is that **you can use WHERE if you want to filter data before grouping, I mean before you group records using GROUP BY clause and use HAVING when you want to filter groups.**

This distinction is made by Query Engine on most of popular database like MySQL, Microsoft SQL Server, Oracle, and PostgreSQL

For example,

```sql
SELECT *
FROM BOOK
WHERE author="Joshua Bloch"
```

will only show books where author is "Joshua Bloch", here we have used WHERE clause because there is no grouping.

In case, we need grouping like authors with number of books we can use group by and having clause together and it will print only authors which have more than one book.

```sql
SELECT author, count(*) as NumberOfBooks
FROM BOOK
GROUP BY author
HAVING NumberOfBooks > 1
```

You can also use **WHERE and HAVING clause together in one query** and in that case WHERE clause will filter before grouping and HAVING clause will filter after grouping as shown in following example:

```sql
SELECT author, count(*) as NumberOfBooks
FROM BOOK
WHERE title like '%SQL%'
GROUP BY author\
HAVING NumberOfBooks > 1
```

This will only print author which have multiple books with title 'SQL in them.

By the way, if you are new to SQL, then you can also use websites like [Udemy](https://bit.ly/32yjDtr), [Coursera](https://bit.ly/3bRuFyD), [Educative](https://bit.ly/3AlbiqX), [ZTM Academy](https://bit.ly/3IvbmXF), freeCodeCamp, and [VladMihalcea' SQL course](https://bit.ly/3PCaAKB) to learn SQL in depth. 


## Difference between WHERE and HAVING clause in SQL
Now that you know what is WHERE and HAVING clause in SQL and what the do, here are more useful difference between WHERE and HAVING clause in SQL :

1\. `WHERE` clause can be used with SELECT, UPDATE and DELETE statements and clauses but HAVING clause can only be used with SELECT statements.

e.g.
```sql
SELECT * FROM Employee WHERE EmployeeId=3
```

This query will print details of **employee with id = 3.**

Similarly,

```sql
SELECT EmployeeName, COUNT(EmployeeName) AS NumberOfEmployee
FROM Employee
HAVING COUNT(EmployeeName) > 2;
```
this query will print duplicate employees from table.

2\. We can't use aggregate functions in the where clause unless it is in a sub query contained in a HAVING clause whereas we can use aggregate function in Having clause. We can use column name in Having clause but the column must be contained in the group by clause.

3\. WHERE clause is used on the individual records whereas Having Clause in conjunction with Group By Clause work on the record sets ( group of records ).

And, if you need more SQL questions like this then you can also see, [**Grokking the SQL Interview**](https://javinpaul.gumroad.com/l/grokking-the-sql-interview) book which covers key topics for SQL interviews

[![SQL Interview questions books](https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhynfkgkv3tl2lp8yhzsr.png)](https://javinpaul.gumroad.com/l/grokking-the-sql-interview)


That's all about **difference between WHERE and HAVING clause in SQL**. This is one of the [important SQL questions](https://www.sqlrevisited.com/2022/01/top-15-sql-query-interview-questions.html) and if you are preparing for Java developer interview, you should know the answer of this question. 
While its a very common concept and we used it on daily basis, not many people can answer it correctly on interview.

Mentioning about keywords like filtering and before and after grouping is key here.


By the way, this is also a common SQL question on Java interviews and if you are preparing for Java interviews, you can also see my earlier articles *like* [*35 Java Questions*](https://dev.to/somadevtoo/50-java-interview-questions-for-practice-1c96), [*15 Spring Framework Questions* ](https://medium.com/javarevisited/25-spring-framework-interview-questions-for-1-to-3-years-experienced-java-programmers-567f268ed897)and [*6 System Design Problem*](https://dev.to/somadevtoo/6-system-design-interview-problems-for-practice-4fl8) to prepare other topics.

All the best !!