Introduction to SQL GROUP BY Multiple Columns
SQL GROUP BY multiple columns is the technique using which we can retrieve the summarized result set from the database using the SQL query that involves grouping of column values done by considering more than one column as grouping criteria. Group by is done for clubbing together the records that have the same values for the criteria that are defined for grouping. When a single column is considered for grouping then the records containing the same value for that column on which criteria are defined are grouped into a single record for the resultset. Similarly, when the grouping criteria are defined on more than one column then all the values of those columns should be the same as that of other columns to consider them for grouping into a single record. In this article, we will learn about the syntax, usage, and implementation of the GROUP BY clause that involves the specification of multiple columns as its grouping criteria with the help of some of the examples.
column1, column2,..., columnm, aggregate_function(columni)
GROUP BY criteriacolumn1 , criteriacolumn2,...,criteriacolumnj;
The syntax of the GROUP BY clause is as shown above. It is the optional clause used in the select clause whenever we need to summarize and reduce the resultset. It should always be placed after the FROM and WHERE clause in the SELECT clause. Some of the terms used in the above syntax are explained below –
- column1, column2,…, column – These are the names of the columns of the target_table table that need to retrieved and fetched in the resultset.
- aggregate_function(column) – These are the aggregate functions defined on the columns of target_table that needs to be retrieved from the SELECT query.
- target_table – Name of the table from where the result is to be fetched.
- conditions_or_constraints – If you want to apply certain conditions on certain columns they can be mentioned in the optional WHERE clause.
criteriacolumn1 , criteriacolumn2,…,criteriacolumnj – These are the columns that will be considered as the criteria to create the groups in the MYSQL query. There can be single or multiple column names on which the criteria need to be applied. We can even mention expressions as the grouping criteria. SQL does not allow using the alias as the grouping criteria in the GROUP BY clause. Note that multiple criteria of grouping should be mentioned in a comma-separated format.
Usage of GROUP BY Multiple Columns
When the grouping criteria are defined on more than one column or expression then all the records that match and have the same values for their respective columns mentioned in the grouping criteria are grouped into a single record. The group by clause is most often used along with the aggregate functions like MAX(), MIN(), COUNT(), SUM(), etc to get the summarized data from the table or multiple tables joined together. Grouping on multiple columns is most often used for generating queries for reports, dashboarding, etc.
Consider a table named educba_learning having the contents and structure as shown in the output of the following select query statement –
SELECT * FROM educba_learning;
The output of the execution of the above query statement is as follows showing the structure and contents of educba_learning table –
Now, we will group the resultset of the educba_learnning table contents based on sessions and expert_name columns so that the retrieved records will only a single record for the rows having the same values for sessions and expert_name collectively. Our query statement will be as follows –
GROUP BY sessions,
The output of the above query statement in SQL is as shown below containing the unique records for each of the session, expert name column values –
Note that while using the grouping criteria it is important to retrieve the records on which the grouping clause is defined. Using the above statement for retrieving all the records will give the following error if the SQL mode is set to only full group by –
GROUP BY sessions,
The output of the above query statement in SQL is as shown below-
Let us use the aggregate functions in the group by clause with multiple columns. We will consider the same above example in which we will apply the SUM() aggregate function on the sessions column to retrieve the total sessions of that expert name for which the same number of session count is present in the table. This means given for the expert named Payal, two different records will be retrieved as there are two different values for session count in the table educba_learning that are 750 and 950. Out of them, there are two records with expert name Payal and session count 750, hence they both will combine because of grouping statement and will result in a single record with total session count value as 1500.
Let us execute the following query statement and study the output and confirm whether it results in output as discussed above –
GROUP BY sessions,
The output of the execution of the above query statement is as follows –
We can observe that for the expert named Payal two records are fetched with session count as 1500 and 950 respectively. Similar work applies to other experts and records too. Note that the aggregate functions are used mostly for numeric valued columns when group by clause is used.
We can group the resultset in SQL on multiple column values. When we define the grouping criteria on more than one column, all the records having the same value for the columns defined in the group by clause are collectively represented using a single record in the query output. All the column values defined as grouping criteria should match with other records column values to group them to a single record. Most of the time, group by clause is used along with aggregate functions to retrieve the sum, average, count, minimum or maximum value from the table contents of multiple tables joined query’s output.
This is a guide to the SQL GROUP BY Multiple Columns. Here we discuss the Introduction, syntax, and examples with code implementation respectively. You may also have a look at the following articles to learn more –