How to use SQL GROUP BY for arranging identical values in groups

If you want to combine rows with identical values into a group, the SQL GROUP BY statement is the right choice. It is typically used in combination with aggregate functions.

What is SQL GROUP BY?

In Structured Query Language, the SQL GROUP BY statement is used to combine rows with identical values in a group. It is used with the SQL command SELECT and follows the WHERE statement. SQL GROUP BY is often used in combination with functions such as SQL AVG(), SQL COUNT(), MAX(), MIN() or SUM(). This allows you to perform calculations and display the results within your table. The statement is particularly helpful for determining sequences or relating values to each other.

VPS Hosting
Fully virtualized servers with root access
  • Unlimited traffic
  • Fast SSD NVMe storage
  • Free Plesk Web Host Edition

Syntax and function

The basic syntax of SQL GROUP BY is:

SELECT column1, column2, column3, ... 
FROM name_of_table 
GROUP BY column1, column2, column3, ...;
sql

However, the version with a WHERE clause, which allows you to specify certain conditions, is much more common. This version looks like this:

SELECT column1, column2, column3, ... 
FROM name_of_table 
WHERE condition 
GROUP BY column1, column2, column3, ... 
ORDER BY column1, column2, column3, ...;
sql

Example of use with COUNT()

To illustrate how you can use SQL GROUP BY, let’s create a simple table called “Customer List”. This contains columns for a customer number, the name, the location and the items purchased:

Customer Number Name Location Items
1427 Smith New York 13
1377 Johnson Los Angeles 9
1212 Brown Los Angeles 15
1431 Davis Chicago 22
1118 Wilson New York 10

Now we can use SQL GROUP BY in combination with the COUNT() function, for example, to list how many customers come from which cities. Here is the corresponding code:

SELECT Location, COUNT(*) AS Number 
FROM Customer_list 
GROUP BY Location;
sql

The result is:

Location Count
Chicago 1
New York 2
Los Angeles 2

Here we use the SQL aliases to display the results as a “number”.

Used with SUM()

In the next example, we use SQL GROUP BY in combination with SUM() to determine and display how many items were ordered from Los Angeles. We use this code for this:

SELECT Location, SUM(Item) AS Total 
FROM Customer_list 
WHERE Location = 'Los Angeles' 
GROUP BY Location;
sql

The result we obtain is:

Location Total
Los Angeles 24

Used with ORDER BY

A combination with ORDER BY is also possible. For our table, we sort by the highest number of items ordered per customer and per city. We start with the location where a customer has purchased the most items. The corresponding code for combining SQL GROUP BY with the MAX() function and ORDER BY function is:

SELECT Location, MAX(item) AS Most 
FROM Customer_list 
GROUP BY Location ORDER BY Most DESC;
sql

And the corresponding issue:

Location Most
Chicago 22
Los Angeles 15
New York 13

Used with HAVING

You can also combine SQL GROUP BY with SQL HAVING. In the following example, we remove customers from the list whose customer number is less than 1300. We then sort the remaining customers according to the number of items they have ordered in ascending order. The code looks like this:

SELECT location, customer number, MIN(article) AS fewest 
FROM Customer list 
GROUP BY Location, Customer number HAVING Customer number > 1300;
sql

The resulting table is:

Location Customer Number Fewest
Los Angeles 1377 9
New York 1427 13
Chicago 1431 22

Alternatives to SQL GROUP BY

A popular alternative to SQL GROUP BY is the PARTITION BY statement. The difference is that all original values are retained and displayed. Additionally, many of the aggregate functions mentioned above also work without SQL GROUP BY.

Tip

A database tailored to your needs: With SQL Server Hosting from IONOS, you can choose between MSSQL, MySQL, or MariaDB. In every case, you benefit from outstanding performance, strong security features, and personalized advice.

Was this article helpful?
We use cookies on our website to provide you with the best possible user experience. By continuing to use our website or services, you agree to their use. More Information.
Page top