Group by Multiple Columns MySQL

Learn via video courses
Topics Covered

Overview

Data grouping is one of the most frequent actions that developers carry out on databases. MySQL's GROUP BY clause enables programmers to organize data based on one or more columns. By using this command, we may group records with the same values based on certain criteria that have been set up specifically for grouping. In this article, we will discuss the basic syntax, use, and different ways to use this command along with various examples.

Introduction

The technique of aggregating and summarising data based on one or more columns is known as grouping data in MySQL. When working with enormous datasets and attempting to gain insight into the data, this might be helpful. MySQL's GROUP BY clause enables programmers to organize data based on one or more columns. Developers can execute aggregate operations on the data by grouping them, such as computing the count, average, or sum of a given column.

Basic GROUP BY Clause

Let's first grasp the fundamental syntax of the GROUP BY clause in MySQL before moving on to grouping data using multiple columns. The GROUP BY clause's basic syntax is as follows:

The GROUP BY clause takes the column name as the parameter on which basis the rows will be grouped, as shown above. Also, generally, a specific attribute is aggregated to provide a single value that represents that group.

Example 1: Keeping track of the number of students by subject

students Table:

idnamesubject
1SahilComputer
2HitendraMaths
3IshanMaths
4VertikEnglish
5AmanComputer
6ManishHindi

Query: We can use the following query to count the number of students in each subject:

Output:

subjectnum_students
Computer2
English1
Hindi1
Maths2

Example 2: Counting the number of orders received from each customer

orders Table:

idcustomer_noorder_value
111100
22520
31735
432400
53810

Query: We can use the following query to count the number of orders for each customer:

Output:

customer_nototal_orders
12
21
32

GROUP BY Multiple Columns

Now that we are familiar with the GROUP BY clause's fundamental structure, let's see how to group data using several columns. Data grouped using multiple columns can be grouped similarly to data grouped using a single column. The fact that we specify numerous columns to group the data by is the sole difference.

To group many records with the same (or similar) values for the designated columns, use the GROUP BY multiple columns function. Anytime we do a GROUP BY multiple columns, all the rows which have all the column values mentioned with GROUP BY as the same value will be grouped.

By using more than one column as a criterion for grouping, the GROUP BY multiple-columns technique obtains grouped column values from one or more database tables.

The following syntax is used to group data using multiple columns:

With the technique shown above, we may group the data by several columns that are separated by commas after the GROUP BY clause.

Example 1: Calculating the number of students by class and subject

students Table:

idnameclasssubject
1Sahil2Computer
2Hitendra3Maths
3Ishan3Maths
4Vertik2English
5Aman3Computer
6Manish3Hindi

Query:

Output:

classsubjectnum_students
2Computer1
3Maths2
2English1
3Computer1
3Hindi1

Example 2: Finding the total profit by item and year

sales Table:

iditemyearprofit
1A2020100
2B20213000
3A20204500
4C20212100
5B20213050

Query: We can use the following query to determine the overall profits for each product by year:

Output:

itemyeartotal_profits
A20204600
B20216050
C20212100

Using Aggregate Functions with GROUP BY Multiple Columns

We can execute aggregate functions on data grouped by many columns just like when using a single column to group the data. The aggregate function is specified after the column we wish to use for the grouping.

It's also common to compute values for each group when combining data based on numerous columns using aggregate functions like SUM, COUNT, AVG, MAX, and MIN. Data can be summarised using these features, which can also reveal patterns and trends in the data.

Simply include the function in your SELECT statement together with the columns you want to group by, if you want to use an aggregate function, with GROUP BY multiple columns. Each set of rows with the same combination of values in the grouping columns will have the function applied to them.

Example:

orders Table:

idcustomer_nameorder_datetotal_amount
1Shivam2022-01-15500.00
2Sachin2022-01-20500.00
3Shivam2022-02-01300.00
4Shivam2022-02-15500.00
5Sachin2022-03-101000.00
6Yash2022-02-151500.00
7Sachin2022-03-01500.00
8Shivam2022-01-31700.00
9Yash2022-02-28500.00

Consider, for instance, the above-given table named orders containing details on customer orders, such as the customer's name, the order date, and the total amount. You need to determine the cumulative sales for each client by year and month. Here's how you could accomplish this using the SUM function and the GROUP BY many columns:

Query:

In this example, we'll group by all three columns (customer name, year, and month) after extracting the year and month from the order date column using the YEAR() and MONTH() functions. The SUM function is also being used to determine the total order amount for each group.

Output:

customer_nameYEAR(order_date)MONTH(order_date)SUM(total_amount)
Shivam202211200.00
Shivam20222800.00
Sachin20221500.00
Sachin202231500.00
Yash202222000.00

Based on the information in the orders table, this output displays the total sales for each customer by year and month. The total amount column has been subjected to the SUM function, and the customer name, year, and month columns have been used to aggregate the outcomes.

By summarising your data in different ways, aggregate functions using GROUP BY several columns can give you insightful information about your data.

GROUP BY with JOINs

The data can be aggregated from other tables and the results can be grouped based on one or more columns using GROUP BY with JOIN function in MySQL. This is helpful when aggregating and extracting data from related tables.

When the JOIN and a GROUP BY clause are used together, MySQL will combine the rows from the specified tables based on a common column and then divide the result set into groups based on one or more columns. Using the aggregated data in this fashion, you may execute aggregate operations and get more insightful conclusions from the data.

The syntax for using GROUP BY with JOINs is as follows:

Using GROUP BY with JOIN is illustrated by the following example:

customers Table:

customer_idcustomer_name
1Ram
2Shyam
3Ghanshyam

orders Table:

order_idcustomer_idorder_monthtotal_profit
11Jan500.00
22Jan250.00
33Feb1000.00
41Feb500.00
53Feb1500.00
63Mar500.00
71Jan200.00
82Jan1000.00
93Feb500.00

By using a join between the orders and the customers tables on the customer_id column and then grouping the result set by the customer_name column, we can obtain the total profit of all orders placed by each customer as follows:

Query:

Output:

customer_nametotal_profit_sum
Ram1200.00
Shyam1250.00
Ghanshyam3500.00

We can see from the result that we grouped the data from the customers table by customer_name and computed the total profit amount for every customer from the orders table.

Conclusion

Key points discussed in the article "GROUP BY Multiple Columns MySQL" include:

  1. In MySQL, grouping data is a helpful method for condensing and analyzing huge datasets.
  2. The aggregate functions SUM, AVG, MAX, and MIN can be combined with GROUP BY to produce summary statistics for each category.
  3. Grouping is also possible based on multiple column combinations, where rows with all the specified column values as the same will be grouped.
  4. Large datasets can be usefully analyzed by employing aggregate functions to group data based on one or more columns.
  5. To combine data from different tables, JOIN and GROUP BY can be utilized.

See Also

  1. MySQL Joins
  2. GROUP BY with subqueries in MySQL
  3. DISTINCT in MySQL