---
title: "How to use SQL GROUP BY Clause?"  
description: "How to use SQL GROUP BY Clause?"  
author: "Allen Scott"  
published: 2016-09-30  
updated: 2016-10-17  
canonical: https://www.mindstick.com/forum/34186/how-to-use-sql-group-by-clause  
category: "database"  
tags: ["sql server", "sql"]  
reading_time: 2 minutes  

---

# How to use SQL GROUP BY Clause?

Hi guys\
I want to know that how to use [SQL](https://www.mindstick.com/articles/13115/types-of-keys-in-sql-or-oracle-database) [GROUP BY Clause](https://www.mindstick.com/forum/262/group-by-clause)?\
Thanks\

## Replies

### Reply by Hubert Jason

The GROUP BY [clause](https://www.mindstick.com/forum/33937/difference-between-throw-exception-and-throw-clause) will gather all of the rows together that contain data in the specified column(s) and will allow aggregate functions to be performed on the one or more columns. \
**GROUP BY clause syntax:**

```
SELECT column1, SUM(column2) FROM "list-of-tables" GROUP BY "column-list";
```

Let's say you would like to retrieve a list of the highest paid salaries in each dept:

```
SELECT max(salary), dept FROM employee  GROUP BY dept;
```

This statement will select the maximum salary for the people in each unique department. Basically, the salary for the person who makes the most in each department will be displayed. Their, salary and their department will be returned

### Reply by Abhishek Srivasatava

Hi Allen,

[Group](https://yourviews.mindstick.com/view/81323/adani-green-energy-group-bags-world-s-biggest-solar-bid) by always used when there is any aggregate function like count.

Here is a small program to understand the logic for group by

```
SELECT empl_stat_cd , prsn_intn_id , count (prsn_intn_id)FROM EE_EMPL_CAT4 GROUP BY empl_stat_cd , prsn_intn_id
```

It will check that how many times prsn_intn_id is present in the column for the group of empl_stat_cd , prsn_intn_id.

After writing group we need to write the entire column name whatever we need to fetch from select statement.

If we don’t use group by then it is not possible to count error will come. In the above example if we put two column under select then count function accordingly.


---

Original Source: https://www.mindstick.com/forum/34186/how-to-use-sql-group-by-clause

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
