---
title: "what is the difference between having clause and  group by statement in sql server"  
description: "what is the difference between having clause and  group by statement in sql server"  
author: "Anonymous User"  
published: 2015-12-09  
updated: 2015-12-09  
canonical: https://www.mindstick.com/forum/33706/what-is-the-difference-between-having-clause-and-group-by-statement-in-sql-server  
category: "database"  
tags: ["sql server", "sql", "sql server 2008", "sql server 2012"]  
reading_time: 2 minutes  

---

# what is the difference between having clause and  group by statement in sql server

I want to know what is the [difference](https://www.mindstick.com/articles/157114/good-news-or-bad-news-and-the-difference-is) between [having clause](https://www.mindstick.com/forum/33663/use-of-group-by-and-having-clause-in-sql-server) and [group](https://yourviews.mindstick.com/view/81323/adani-green-energy-group-bags-world-s-biggest-solar-bid) by in [sql server](https://www.mindstick.com/articles/12999/what-is-table-valued-function-in-sql-server) [please tell me](https://www.mindstick.com/forum/33900/please-tell-me-what-are-the-technique-to-content-optimization).

## Replies

### Reply by Anonymous User

**In [SQL](https://www.mindstick.com/articles/13115/types-of-keys-in-sql-or-oracle-database), the having [clause](https://www.mindstick.com/forum/33937/difference-between-throw-exception-and-throw-clause) and the group by statement work together when using aggregate functions like SUM, AVG, MAX, etc.**

**If we want to calculate the total salary that each employee has received, then we would write a SQL statement like this:** \

```
select Emp_Name, sum(Emp_Sal) as TotalSalary from Employee group by Emp_Name;
```

![what is the difference between having clause and  group by statement in sql server](https://www.mindstick.com/mindstickforums/09e47c86-c821-46e0-8491-45796a625e3f/images/822a8d97-412c-445a-93f1-b6607d01bc3f.png)

In the SQL statement above, you can see that we use the "group by" clause with the employee column. The group by clause allows us to find the sum of the salary for each employee.

Now, suppose we wanted to find the employees who received more than 1,0000 salary – this is assuming of course that the Employee table contains salary. This is when we need to use the HAVING clause to add the additional check to see if the sum of salary is greater than 1,0000 and this is what the SQL look like:\

```
select Emp_Name, sum(Emp_Sal)as TotalSalary from Employee group by Emp_Name having sum(Emp_Sal) > 10000;
```

![what is the difference between having clause and  group by statement in sql server](https://www.mindstick.com/mindstickforums/09e47c86-c821-46e0-8491-45796a625e3f/images/a496a981-c38b-4e03-a6e7-51e31a60354a.png)

So, from the example above, we can see that the group by clause is used to group column(s) so that aggregates (like SUM, MAX, etc) can be used to find the necessary information. The having clause is used with the group by clause when comparisons need to be made with those aggregate functions – like to see if the SUM is greater than 1,000, as in our example above.\


---

Original Source: https://www.mindstick.com/forum/33706/what-is-the-difference-between-having-clause-and-group-by-statement-in-sql-server

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
