Open In App

MySQL Group By Clause

Last Updated : 01 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In MySQL, the GROUP BY clause is a powerful tool for grouping rows with the same values into summary rows, enabling efficient data analysis. It is often used with aggregate functions like SUM, COUNT, AVG, MIN, and MAX to perform calculations on grouped data. In this article, We will learn about the MySQL Group By Clause by the understanding with the various aggregate functions examples and so on.

MySQL Group By Clause

  • In MySQL, the GROUP BY clause is useful operator that is used to group rows that have the same values, and can be used to push these values into summary rows, like "find the number of customers in each city" or "calculate the total number of sales per product category."
  • It is often used along with aggregate functions like SUM, COUNT, AVG, MIN, and MAX to perform calculations on grouped data.

Syntax:

The GROUP BY clause is used to group rows that have the same values into summary rows. It operates on a set of columns that are specified in the SQL query. The basic syntax involving the GROUP BY operation is given below:

SELECT column1, aggregate_function(column2)
FROM table_name
WHERE condition
GROUP BY column1;

Where,

  • column1: Columns by which you want to group the result set.
  • aggregate_function: Functions like SUM, COUNT, AVG, etc., to perform calculations on the grouped data.
  • table_name: The table name from which you are fetching the data.
  • condition: Conditions to filter rows before grouping. This is optional if you are giong to perform just a raw and simple query

Examples of MySQL Group By Clause

Let's explore some examples using the employees table, which contains columns such as employee_id, name, department and salary

Groupby-table

Example 1: Grouping by a Single Column

Lets say that you have a table named employees with column such as employee_id, name, department and salary. We want to find the average salary of employees in each department( such as IT, HR, Marketing etc).

SELECT department, AVG(salary) AS average_salary
FROM employees
GROUP BY department;

Output:

Screenshot-2024-07-21-162409
FIG: Query Result showing the average salary of each dept. grouped by a single column

Explanation: This query groups the employees by the department column and calculates the average salary for each department. It will return each department along with the average salary of its employees.

Example 2: Grouping by Multiple Columns

Consider the same employees table , here we will find the average salary for each employee within their department. We can use the GROUP BY clause with multiple columns: department and name.

SELECT department, name, AVG(salary) AS average_salary
FROM employees
GROUP BY department , name;

Output:

s2
Fig: Query result showing the grouping by multiple column approch.

Explanation: This query groups the employees by both the department and name columns. It calculates the average salary for each employee, assuming they work in different departments.

Lets see the working of GROUP BY Clause along with the aggregate functions such as SUM, AVG,COUNT,MIN and MAX.

Example 3: Using the COUNT() Function

In this example, we will find the count of employees in each department or we can say how many employees are there in each department. We can use the COUNT() function to count the number of rows for each department.

SELECT department, COUNT(*) AS employee_count
FROM employees
GROUP BY department;

Output:

s3
Fig: Query result showing the employee count in each department

Explanation: This query above counts the number of employees in each department, which is performed by COUNT aggregation function. This result will show the department and the number of employees in each department.

Example 4: Using the SUM() Function

Let us calculate the total salary paid to employees in each department. We use the SUM() function in this query:

SELECT department, SUM(salary) AS total_salary
FROM employees
GROUP BY department;

Output:

s4
Fig: Query result showing the tatal salary of each department

Explanation: This query sums the salaries of employees in each department using the SUM Aggregate function. This result will show each department and the total salary paid to its employees.

Example 5: Using the MIN() Function

Here, in this example we will calculate the minimum salary in each department using the MIN() function.

SELECT department, MIN(salary) AS min_salary
FROM employees
GROUP BY department;

Output:

s6
Fig: Query result showing the minimum salary in each department

Explanation: This query finds the minimum salary in each department, by using the MIN aggregate function available in SQL.

Example 6: Using the MAX() Function

Here in this example, we will calculate the maximum salary in each department using the MAX() function.

SELECT department, MAX(salary) AS max_salary
FROM employees
GROUP BY department;

Output:

s7
FIg: Query result showing maximum salary of each department

Explanation: This query finds the maximum salary in each department, which is executed by the use of MAX as the aggregate function.

Example 7: MySQL GROUP BY Clause with AVG Function

Let us calculate the average salary in each department again to reinforce the use of the AVG() function.

SELECT department, AVG(salary) AS average_salary
FROM employees
GROUP BY department;

Output:

s8
Fig: Query result showing the average salary of each department

Explanation: This query calculates the average salary in each department, which is performed by the AVG Function as the aggregation.

Conclusion

Overall, GROUP BY clause in MySQL is an essential tool for data analysis and reporting. It allows you to group rows that have the same values into summary rows and perform calculations on these groups using aggregate functions like SUM, COUNT, AVG, MIN, and MAX. By mastering the GROUP BY clause, you can efficiently analyze and summarize large datasets, making it an invaluable skill for any database professional or data analyst.


Next Article
Article Tags :

Similar Reads