Error
Error Code:
1463
MariaDB Error 1463: Non-Grouping Field in Query
Description
This error occurs when an SQL query attempts to use a column in a SELECT list, HAVING clause, or ORDER BY clause that is neither an aggregate function nor explicitly listed in the GROUP BY clause. MariaDB requires that all non-aggregated columns in these contexts are clearly defined for each group.
Error Message
Non-grouping field '%s' is used in %s clause
Known Causes
3 known causesSelecting Non-Grouped Column
A column is included in the SELECT list but is not an aggregate function (e.g., COUNT, SUM) and is not specified in the GROUP BY clause, violating SQL grouping rules.
Non-Grouped Field in HAVING Clause
The HAVING clause, which filters groups, references a column that is neither part of the GROUP BY clause nor an aggregate function, leading to ambiguity in filtering.
Strict SQL Mode (ONLY_FULL_GROUP_BY)
This error is frequently encountered when the `ONLY_FULL_GROUP_BY` SQL mode is active, enforcing strict SQL standards where all non-aggregated columns in the SELECT list must appear in the GROUP BY clause.
Solutions
4 solutions available1. Include Non-Grouping Field in GROUP BY Clause easy
Add the problematic non-grouping field to the GROUP BY clause.
1
Identify the non-grouping field and the clause where it's causing the error. The error message will explicitly state this. For example, if the error is 'Non-grouping field 'users.username' is used in SELECT clause', then 'users.username' is the field and 'SELECT' is the clause.
2
Modify your SQL query to include this identified field in the `GROUP BY` clause. This tells MariaDB that you intend to group by this field as well, making it a valid part of the aggregate query.
SELECT column1, column2, non_grouping_field FROM your_table WHERE condition GROUP BY column1, column2, non_grouping_field;
2. Remove Non-Grouping Field from SELECT or ORDER BY Clause easy
Remove the field from the SELECT or ORDER BY clause if it's not needed for aggregation.
1
Examine your query and determine if the non-grouping field is actually necessary in the `SELECT` list or `ORDER BY` clause. If you are using aggregate functions (like `COUNT()`, `SUM()`, `AVG()`, `MAX()`, `MIN()`), any non-aggregated column in the `SELECT` list or used in `ORDER BY` must be included in the `GROUP BY` clause. If the field is not crucial for the output or ordering, the simplest solution is to remove it.
SELECT column1, COUNT(*) FROM your_table WHERE condition GROUP BY column1;
2
If the field is in the `ORDER BY` clause and not in `GROUP BY`, and you don't need it for the final result, remove it from the `ORDER BY` clause.
SELECT column1, COUNT(*) FROM your_table WHERE condition GROUP BY column1 ORDER BY column1;
3. Use an Aggregate Function on the Non-Grouping Field medium
Apply an aggregate function to the field if you want to include it in the result set without grouping by it.
1
If you need to display a value from a non-grouping field in your results, but you don't want to group by it, you can apply an aggregate function. The choice of function depends on what you want to represent. For example, if you want to show one of the values from that column for each group, you could use `MAX()` or `MIN()`.
SELECT column1, MAX(non_grouping_field) AS representative_field FROM your_table WHERE condition GROUP BY column1;
2
Alternatively, if you're simply trying to count occurrences of a specific value within the non-grouping field for each group, you can use conditional aggregation.
SELECT column1, COUNT(CASE WHEN non_grouping_field = 'some_value' THEN 1 ELSE NULL END) AS count_of_some_value FROM your_table WHERE condition GROUP BY column1;
4. Disable ONLY_FULL_GROUP_BY Mode (Use with Caution) advanced
Temporarily disable the strictness of GROUP BY mode.
1
The `ONLY_FULL_GROUP_BY` SQL mode enforces that all non-aggregated columns in the `SELECT` list must be present in the `GROUP BY` clause. While this promotes data integrity, it can sometimes be restrictive. You can disable this mode. This is generally not recommended for production systems without a thorough understanding of the implications, as it can lead to ambiguous results.
2
To disable `ONLY_FULL_GROUP_BY` for the current session, execute the following SQL command:
SET SESSION sql_mode = (SELECT REPLACE(@@sql_mode, 'ONLY_FULL_GROUP_BY', ''));
3
To disable `ONLY_FULL_GROUP_BY` globally (requires appropriate privileges and affects all new connections), edit your MariaDB configuration file (e.g., `my.cnf` or `my.ini`) and remove `ONLY_FULL_GROUP_BY` from the `sql_mode` setting. Then, restart the MariaDB server.
[mysqld]
sql_mode = "ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION,..."
(Ensure ONLY_FULL_GROUP_BY is NOT listed)
4
After making changes to the configuration file, restart the MariaDB service. The exact command depends on your operating system and MariaDB installation.
# For systemd-based systems (e.g., Ubuntu 15.04+, CentOS 7+)
sudo systemctl restart mariadb
# For older init.d systems
sudo service mariadb restart