---
title: "How to apply Group on multiple columns in SQL?"  
description: "How to apply Group on multiple columns in SQL?"  
author: "Revati S Misra"  
published: 2023-04-12  
updated: 2023-04-24  
canonical: https://www.mindstick.com/forum/157767/how-to-apply-group-on-multiple-columns-in-sql  
category: "mssql server"  
tags: ["sql server", "sql"]  
reading_time: 3 minutes  

---

# How to apply Group on multiple columns in SQL?

How to [apply](https://www.mindstick.com/articles/13148/discover-to-apply-make-up-like-a-professional-supermodel) [Group](https://yourviews.mindstick.com/view/81323/adani-green-energy-group-bags-world-s-biggest-solar-bid) on [multiple columns](https://www.mindstick.com/forum/34393/selecting-multiple-columns-in-linq) in [SQL](https://www.mindstick.com/articles/13115/types-of-keys-in-sql-or-oracle-database)?

## Replies

### Reply by Aryan Kumar

To apply a **GROUP BY** clause on multiple columns in SQL, you simply list the column names separated by commas after the **GROUP BY** keyword.

Here's an example:

```php
SELECT column1, column2, SUM(column3)
FROM table_name
GROUP BY column1, column2;
```

In this example, we have a table called **table_name** with columns **column1**, **column2**, and **column3**. We want to group the data by values in **column1** and **column2**, and get the sum of values in **column3** for each group.

The **GROUP BY** clause includes both **column1** and **column2**, separated by a comma. The **SUM** function is used to add up the values in **column3** for each group.

When you run this query, you'll get a result set that shows the values of **column1** and **column2**, and the sum of values in **column3** for each group of unique combinations of values in **column1** and **column2**

### Reply by Krishnapriya Rajeev

To apply GROUP BY on [multiple](https://www.mindstick.com/blog/12797/iowa-is-expected-to-see-heavy-growth-in-multiple-sectors) [columns in SQL](https://www.mindstick.com/forum/161180/how-to-compare-rows-and-columns-in-sql-server-database), we have to include the names of those columns we need, to be separated by commas in the GROUP BY clause. Here's an example SQL query that demonstrates how to use GROUP BY on multiple columns:

```plaintext
SELECT column1, column2, COUNT(*)
FROM table_name
GROUP BY column1, column2;
```

In this example, *column1* and *column2* are the names of the columns on which we want to group the data, and *table_name* is the name of the table from which we want to retrieve the data. The COUNT(*) function is used to count the number of rows that match each combination of values in column1 and column2.

By using GROUP BY with multiple columns, we can get more *granular information* about the data than we could with a single column.

For example:

We have a table named *orders* as shown below:

| **Customer** | **Month** | **Amount** |
| --- | --- | --- |
| John | May | 20 |
| Alfiya | June | 30 |
| John | May | 25 |
| John | August | 30 |
| Alfiya | June | 10 |

We might want to know the total amount spent by each customer each month. In this case, we would use GROUP BY on both the customer and month columns:

```plaintext
SELECT customer, month, SUM(amount)
FROM orders
GROUP BY customer, month;
```

This query would return a table showing the total amount spent by placed by each customer in each month as shown below.

| **customer** | **month** | **SUM(amount)** |
| --- | --- | --- |
| John | May | 45 |
| Alfiya | June | 40 |
| John | August | 30 |


---

Original Source: https://www.mindstick.com/forum/157767/how-to-apply-group-on-multiple-columns-in-sql

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
