Grouping Data with GROUP BY

In SQL, the GROUP BY clause is used to group rows that have the same values in specified columns into summary rows. It is typically used in conjunction with aggregate functions like COUNT(), SUM(), AVG(), MIN(), and MAX() to perform calculations on each group of data. This chapter will take you through a detailed explanation of the GROUP BY clause, from the basics to advanced uses, and include code examples with clear explanations.

What is SQL GROUP BY?

The GROUP BY clause allows you to group rows based on one or more columns. Once the rows are grouped, you can apply aggregate functions to return summary statistics for each group.

Syntax

				
					SELECT column_name(s), aggregate_function(column_name)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);

				
			
  • column_name(s): The column(s) you want to group by.
  • aggregate_function: Functions like SUM(), COUNT(), AVG(), etc.
  • table_name: The table from which you are retrieving data.
  • condition: (Optional) A WHERE condition to filter rows before grouping.
  • ORDER BY: (Optional) Used to sort the results.

Example: Basic GROUP BY

Consider a table named Sales:

SaleIDProductIDQuantityPriceSalesPerson
1101510John
2102220Sarah
3101410John
4103315John
5102620Sarah

We want to calculate the total quantity sold by each salesperson. We can achieve this using GROUP BY:

				
					SELECT SalesPerson, SUM(Quantity) AS TotalQuantity
FROM Sales
GROUP BY SalesPerson;

				
			

Output:

SalesPersonTotalQuantity
John12
Sarah8

Explanation:

  • The query groups the rows based on the SalesPerson column.
  • The SUM(Quantity) function calculates the total quantity of products sold for each salesperson.

Using Multiple Columns with GROUP BY

You can group by multiple columns to further break down your data. For example, let’s calculate the total sales (quantity * price) made by each salesperson for each product.

				
					SELECT SalesPerson, ProductID, SUM(Quantity * Price) AS TotalSales
FROM Sales
GROUP BY SalesPerson, ProductID;

				
			

Output:

SalesPersonProductIDTotalSales
John10190
John10345
Sarah102160

Explanation:

  • The query groups by both SalesPerson and ProductID.
  • The SUM(Quantity * Price) function calculates the total sales amount for each product sold by each salesperson.

Filtering Data with GROUP BY and HAVING

The HAVING clause allows you to filter groups after the GROUP BY operation has been applied. This is different from the WHERE clause, which filters rows before they are grouped.

For example, to find salespeople who have sold more than 10 units:

				
					SELECT SalesPerson, SUM(Quantity) AS TotalQuantity
FROM Sales
GROUP BY SalesPerson
HAVING SUM(Quantity) > 10;

				
			

Output:

SalesPersonTotalQuantity
John12

Explanation:

  • The HAVING clause filters the results to include only those salespeople who sold more than 10 units.
  • The HAVING clause is necessary because aggregate functions cannot be used in the WHERE clause.

GROUP BY with Aliases

Sometimes, you may want to give the result of your aggregate functions a name, which can be done using aliases. For instance:

				
					SELECT SalesPerson, COUNT(*) AS NumberOfSales
FROM Sales
GROUP BY SalesPerson;

				
			

Output:

SalesPersonNumberOfSales
John3
Sarah2

Explanation:

  • The COUNT(*) function counts the number of rows for each salesperson.
  • The alias NumberOfSales gives a meaningful name to the count result.

GROUP BY with ORDER BY

You can combine the GROUP BY clause with ORDER BY to sort the results based on either the group column(s) or the aggregate results.

For example, to list the total quantity sold by each salesperson in descending order:

				
					SELECT SalesPerson, SUM(Quantity) AS TotalQuantity
FROM Sales
GROUP BY SalesPerson
ORDER BY TotalQuantity DESC;

				
			

Output:

SalesPersonTotalQuantity
John12
Sarah8

Explanation:

  • The ORDER BY clause sorts the results by the TotalQuantity column in descending order.

Advanced Example: Grouping by Dates

In a more advanced scenario, you might need to group data by date ranges. Suppose you have a Sales table with a SaleDate column and you want to calculate daily sales totals:

				
					SELECT SaleDate, SUM(Quantity * Price) AS DailyTotal
FROM Sales
GROUP BY SaleDate;

				
			

Explanation:

  • This query groups the sales by date and calculates the total sales amount for each day.

The GROUP BY clause is a powerful tool in SQL, allowing you to summarize data based on specific columns and perform calculations on groups. Whether you're working with simple data or more complex queries involving multiple columns, GROUP BY is essential for producing summary reports and insights. With the addition of HAVING for post-grouping filtering and ORDER BY for sorting, the GROUP BY clause becomes even more flexible. Happy coding !❤️

Table of Contents

Contact here

Copyright © 2025 Diginode

Made with ❤️ in India