---
title: "What is the difference between the WHERE clause and the HAVING clause in SQL?"  
description: "What is the difference between the WHERE clause and the HAVING clause in SQL?"  
author: "Revati S Misra"  
published: 2023-06-30  
updated: 2023-07-02  
canonical: https://www.mindstick.com/forum/158903/what-is-the-difference-between-the-where-clause-and-the-having-clause-in-sql  
category: "mssql server"  
tags: ["mssql server", "sql server", "sql"]  
reading_time: 2 minutes  

---

# What is the difference between the WHERE clause and the HAVING clause in SQL?

What is the [difference](https://www.mindstick.com/articles/157114/good-news-or-bad-news-and-the-difference-is) between the WHERE [clause and the HAVING](https://www.mindstick.com/forum/160080/what-is-the-difference-between-the-sql-where-clause-and-the-having-clause-in-search-queries) [clause in SQL](https://www.mindstick.com/forum/33663/use-of-group-by-and-having-clause-in-sql-server)?

## Replies

### Reply by Aryan Kumar

The WHERE and HAVING clauses in [SQL](https://www.mindstick.com/articles/13115/types-of-keys-in-sql-or-oracle-database) are both used to filter the results of a SELECT statement. However, they work in different ways.

The [WHERE clause](https://www.mindstick.com/interview/1909/when-do-you-use-where-clause-and-when-do-you-use-having-clause) is used to filter the rows of data before any grouping is done. It is used to filter individual rows, based on certain conditions. For example, the following WHERE clause will only return rows where the `customer_id` is equal to 100:

SQL

```plaintext
SELECT * FROM customers
WHERE customer_id = 100;
```

The HAVING clause is used to filter the groups of data after grouping has been done. It is used to filter groups of rows, based on conditions involving aggregate functions. For example, the following HAVING clause will only return groups where the average order amount is greater than $100:

SQL

```plaintext
SELECT customer_id, AVG(total_amount) AS average_order_amount
FROM orders
GROUP BY customer_id
HAVING AVG(total_amount) > 100;
```

In other words, the WHERE clause filters the rows before they are grouped, and the HAVING clause filters the groups after they are grouped.

Here is a table that summarizes the key differences between the WHERE and HAVING clauses:

| Clause | Description |
| --- | --- |
| WHERE | Filters the rows of data before any grouping is done. |
| HAVING | Filters the groups of data after grouping has been done. |
| Used to filter individual rows | Used to filter groups of rows |
| Can be used with any SELECT statement | Can only be used with SELECT statements that have a GROUP BY clause |


---

Original Source: https://www.mindstick.com/forum/158903/what-is-the-difference-between-the-where-clause-and-the-having-clause-in-sql

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
