Error
Error Code: 1056

MariaDB Error 1056: Invalid GROUP BY Column

📦 MariaDB
📋

Description

This error occurs when a column specified in the `GROUP BY` clause is not valid or when a non-aggregated column in the `SELECT` list is not included in the `GROUP BY` clause. MariaDB requires that all non-aggregated columns in the `SELECT` statement also appear in the `GROUP BY` clause to ensure consistent result sets for aggregated data.
💬

Error Message

Can't group on '%s'
🔍

Known Causes

3 known causes
⚠️
Non-aggregated SELECT Column
A column is included in the `SELECT` list without an aggregate function, but is not present in the `GROUP BY` clause.
⚠️
Invalid GROUP BY Expression
The expression or column specified in the `GROUP BY` clause is too complex, non-deterministic, or otherwise invalid for grouping.
⚠️
Misspelled Column or Alias
The column name or alias used in the `GROUP BY` clause does not match an existing column or a valid alias from the `SELECT` list.
🛠️

Solutions

4 solutions available

1. Include Non-Aggregated Columns in GROUP BY easy

Ensure all non-aggregated columns in the SELECT list are present in the GROUP BY clause.

1
Review your SQL query. Identify any columns in the `SELECT` statement that are not part of an aggregate function (like `SUM`, `COUNT`, `AVG`, `MAX`, `MIN`).
2
Add these non-aggregated columns to your `GROUP BY` clause. For example, if your query is `SELECT column1, COUNT(*) FROM my_table GROUP BY column1;` and you add `column2` to the `SELECT` list as `SELECT column1, column2, COUNT(*) FROM my_table GROUP BY column1;`, you must also add `column2` to the `GROUP BY` clause: `SELECT column1, column2, COUNT(*) FROM my_table GROUP BY column1, column2;`.
SELECT column1, column2, aggregate_function(column3)
FROM your_table
WHERE ...
GROUP BY column1, column2;

2. Use Aggregate Functions for Non-Grouped Columns easy

If a column cannot be included in GROUP BY, apply an aggregate function to it.

1
Examine your `SELECT` statement. For columns that are not in the `GROUP BY` clause and are not intended for grouping, you must use an aggregate function.
2
Choose an appropriate aggregate function. For instance, if you want to display the `MAX` value of a column for each group, use `MAX()`. If you just need to pick any value from the group (though this can be arbitrary), `ANY_VALUE()` is a suitable option in MariaDB (versions 10.0+).
SELECT column1, ANY_VALUE(column2), SUM(column3)
FROM your_table
WHERE ...
GROUP BY column1;

3. Enable ONLY_FULL_GROUP_BY Mode (with caution) medium

Temporarily disable strict GROUP BY checking, but understand the implications.

1
Check the current `sql_mode` setting. This can be done by running `SHOW VARIABLES LIKE 'sql_mode';`.
SHOW VARIABLES LIKE 'sql_mode';
2
If `ONLY_FULL_GROUP_BY` is present in the output, it means this strict checking is enabled. To disable it for the current session, run `SET SESSION sql_mode = 'your_current_sql_mode_without_ONLY_FULL_GROUP_BY';`. Replace `your_current_sql_mode_without_ONLY_FULL_GROUP_BY` with the actual value from the previous step, ensuring `ONLY_FULL_GROUP_BY` is removed.
SET SESSION sql_mode = 'STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION'; -- Example, replace with your actual mode minus ONLY_FULL_GROUP_BY
3
To make this change permanent, you need to edit your MariaDB configuration file (e.g., `my.cnf` or `my.ini`) and remove `ONLY_FULL_GROUP_BY` from the `sql_mode` setting. After editing, restart the MariaDB server.
# Example entry in my.cnf
[mysqld]
sql_mode = "STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION"
4
Understand that disabling `ONLY_FULL_GROUP_BY` can lead to non-deterministic results for columns not in the `GROUP BY` clause, as MariaDB might return an arbitrary value from the group. It's generally recommended to fix the query rather than disable this mode.

4. Use Subqueries or CTEs for Complex Grouping advanced

Restructure queries with subqueries or Common Table Expressions (CTEs) for clearer grouping logic.

1
Identify the part of your query that is causing the `Invalid GROUP BY Column` error. This often happens in complex joins or when aggregating data from multiple sources.
2
Consider breaking down the query. A subquery can pre-aggregate or filter data, and then the outer query can perform further grouping on the results of the subquery. Similarly, CTEs can define intermediate result sets that are easier to manage.
WITH PreAggregated AS (
    SELECT column1, column2, SUM(column3) as total_value
    FROM your_table
    WHERE ...
    GROUP BY column1, column2
)
SELECT column1, MAX(total_value)
FROM PreAggregated
GROUP BY column1;
3
This approach allows you to first group by the necessary columns in the subquery/CTE and then perform a second level of aggregation in the main query, potentially resolving the error by ensuring all columns in the outer `GROUP BY` are correctly handled.
🔗

Related Errors

5 related errors