---
title: "Explain the purpose of the GROUP BY clause in SQL and provide an example."  
description: "Explain the purpose of the GROUP BY clause in SQL and provide an example."  
author: "Revati S Misra"  
published: 2023-06-30  
updated: 2023-07-11  
canonical: https://www.mindstick.com/forum/158904/explain-the-purpose-of-the-group-by-clause-in-sql-and-provide-an-example  
category: "mssql server"  
tags: ["mssql server", "sql server", "sql"]  
reading_time: 2 minutes  

---

# Explain the purpose of the GROUP BY clause in SQL and provide an example.

[Explain the purpose](https://www.mindstick.com/forum/159480/explain-the-purpose-of-the-virtual-keyword-and-virtual-functions-in-c-plus-plus) of the [GROUP BY clause](https://www.mindstick.com/forum/262/group-by-clause) in [SQL](https://www.mindstick.com/articles/13115/types-of-keys-in-sql-or-oracle-database) and provide an example.

## Replies

### Reply by Aryan Kumar

In SQL, the [GROUP](https://yourviews.mindstick.com/view/81323/adani-green-energy-group-bags-world-s-biggest-solar-bid) BY [clause](https://www.mindstick.com/forum/33937/difference-between-throw-exception-and-throw-clause) is used to group rows based on one or more columns and perform aggregate functions on each group. Its [purpose](https://yourviews.mindstick.com/view/247/no-fail-policy-failing-its-purpose) is to summarize or aggregate data based on specific criteria and provide meaningful insights.

The basic syntax for using the GROUP BY clause is as follows:

```plaintext
SELECT column1, column2, ..., aggregate_function(column)
FROM table
GROUP BY column1, column2, ...;
```

Here's how it works:

1. The SELECT statement retrieves the specified columns and can also include aggregate functions such as SUM, AVG, COUNT, MAX, MIN, etc.\
2. The GROUP BY clause specifies the columns that will be used for grouping the rows.\
3. The result set is divided into groups based on the unique combinations of values in the specified columns.\
4. The aggregate functions are then applied to each group, calculating the desired values for each group.\
5. The result set will include one row for each group, with the aggregated values.

Here's an example to illustrate the usage of the GROUP BY clause:

Consider a table named "orders" with the following columns: order_id, customer_id, product_id, order_date, and quantity.

To find the total quantity of products ordered by each customer, we can use the GROUP BY clause as follows:

```plaintext
SELECT customer_id, SUM(quantity) AS total_quantity
FROM orders
GROUP BY customer_id;
```

In this example, the result set will contain two columns: customer_id and total_quantity. The rows will be grouped based on the customer_id column, and the SUM function will calculate the total quantity of products ordered for each customer.

The output may look like this:

```plaintext
customer_id | total_quantity
---------------------------
  101      |      25
  102      |      18
  103      |      30
```

This result shows the customer IDs and the corresponding total quantities of products ordered by each customer.

The GROUP BY clause is powerful for performing data aggregation and summarization operations in SQL, allowing you to analyze data at a higher level of granularity. It is often used in conjunction with aggregate functions to generate meaningful reports and insights from large datasets.


---

Original Source: https://www.mindstick.com/forum/158904/explain-the-purpose-of-the-group-by-clause-in-sql-and-provide-an-example

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
