How does GROUP BY clause work internally in MySQL?

The GROUP BY clause in MySQL is used to arrange identical data into groups. This clause is often used with aggregate functions such as SUM(), AVG(), COUNT(), MAX(), and MIN() to perform calculations on each group of data. When a GROUP BY clause is included in a SQL query, MySQL processes the result set into groups based on the specified columns.

How GROUP BY Works Internally

Internally, when a query with a GROUP BY clause is executed, MySQL follows a specific sequence of operations:

  1. The database engine collects all the rows from the table that match the WHERE clause (if present).
  2. It then creates a temporary table containing only the rows that meet the specified criteria.
  3. MySQL groups the rows according to the columns specified in the GROUP BY clause.
  4. Aggregate functions are then applied to each group to compute aggregated values.
  5. Finally, the result set is returned to the user.

Example of GROUP BY

The following example demonstrates how to use the GROUP BY clause to count the number of employees in each department:

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

GROUP BY MySQL GROUP BY SQL aggregate functions MySQL queries