How to use GROUP BY in SQL
By Tan Lee Published on Mar 07, 2025 112
Let’s say you have the following table tracking sales transactions and you want to know how many products each store sold:
Sales Table:
StoreName | ProductName | Quantity |
---|---|---|
Store A | Product 1 | 10 |
Store B | Product 2 | 5 |
Store A | Product 3 | 7 |
Store B | Product 1 | 8 |
Store A | Product 2 | 12 |
To calculate the total number of products each store sold, you can use the GROUP BY
clause as follows:
1 2 3 |
SELECT StoreName, SUM (Quantity) as TotalSold FROM Sales GROUP BY StoreName |
This will return the total quantity of products sold by each store:
StoreName | TotalSold |
---|---|
Store A | 29 |
Store B | 13 |
GROUP BY Multiple Columns
You can also group by multiple columns.
For example, if you want to know how many of each product was sold by each store, you can group by both StoreName
and ProductName
:
1 2 3 |
SELECT StoreName, ProductName, SUM (Quantity) as TotalSold FROM Sales GROUP BY StoreName, ProductName |
This produces the following result:
StoreName | ProductName | TotalSold |
---|---|---|
Store A | Product 1 | 10 |
Store A | Product 2 | 12 |
Store A | Product 3 | 7 |
Store B | Product 1 | 8 |
Store B | Product 2 | 5 |
GROUP BY and NULLs
When a grouping column contains a NULL
value, it creates a separate group for those rows.
For example, if some rows have missing store names, they will be grouped together as NULL
.
To avoid this, you can filter out rows with NULL
values in the grouping column using the WHERE
clause:
1 2 3 4 |
SELECT StoreName, SUM (Quantity) as TotalSold FROM Sales WHERE StoreName IS NOT NULL GROUP BY StoreName |
Error: Column is Invalid in the Select List
When using GROUP BY
, there are restrictions on which columns can appear in the SELECT
list. For instance, you cannot use a column that isn’t part of the GROUP BY
clause or doesn’t have an aggregate function applied to it.
For example, if you try this query:
1 2 3 |
SELECT StoreName, ProductName, COUNT (*) FROM Sales GROUP BY StoreName |
You’ll encounter the following error:
1 2 |
Msg 8120, Level 16, State 1, Line 1 Column 'Sales.ProductName' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. |
To fix this error, you either need to add ProductName
to the GROUP BY
clause or use an aggregate function like COUNT()
:
1 2 3 |
SELECT StoreName, ProductName, COUNT (*) FROM Sales GROUP BY StoreName, ProductName |
Alternatively, you can use ProductName
with an aggregate function like MAX()
to find the product with the highest quantity:
1 2 3 |
SELECT StoreName, MAX (ProductName), SUM (Quantity) FROM Sales GROUP BY StoreName |
This query will return the highest-selling product name for each store, along with the total quantity sold.
- How to Query JSON in SQL Server
- How to modify JSON in SQL Server
- How to set time to 00:00:00 with GETDATE() in SQL
- How to find all the dependencies of a table in SQL Server
- How to Find Objects Referencing a Table in SQL Server
- Case sensitivity in SQL Server
- How to Convert varchar to uniqueidentifier in SQL Server
- Filtering GROUP BY with HAVING and WHERE in SQL