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.
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.
SELECT column_name(s), aggregate_function(column_name)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
SUM()
, COUNT()
, AVG()
, etc.WHERE
condition to filter rows before grouping.Consider a table named Sales
:
SaleID | ProductID | Quantity | Price | SalesPerson |
---|---|---|---|---|
1 | 101 | 5 | 10 | John |
2 | 102 | 2 | 20 | Sarah |
3 | 101 | 4 | 10 | John |
4 | 103 | 3 | 15 | John |
5 | 102 | 6 | 20 | Sarah |
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;
SalesPerson | TotalQuantity |
---|---|
John | 12 |
Sarah | 8 |
SalesPerson
column.SUM(Quantity)
function calculates the total quantity of products sold for each salesperson.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;
SalesPerson | ProductID | TotalSales |
---|---|---|
John | 101 | 90 |
John | 103 | 45 |
Sarah | 102 | 160 |
SalesPerson
and ProductID
.SUM(Quantity * Price)
function calculates the total sales amount for each product sold by each salesperson.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;
SalesPerson | TotalQuantity |
---|---|
John | 12 |
HAVING
clause filters the results to include only those salespeople who sold more than 10 units.HAVING
clause is necessary because aggregate functions cannot be used in the WHERE
clause.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;
SalesPerson | NumberOfSales |
---|---|
John | 3 |
Sarah | 2 |
COUNT(*)
function counts the number of rows for each salesperson.NumberOfSales
gives a meaningful name to the count result.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;
SalesPerson | TotalQuantity |
---|---|
John | 12 |
Sarah | 8 |
ORDER BY
clause sorts the results by the TotalQuantity
column in descending order.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;
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 !❤️