Error related to only_full_group_by when executing a query in MySql
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Error related to only_full_group_by occurs quite frequently in MySQL when executing queries, especially for users who are transitioning from older versions of MySQL to newer ones. This article explains why this error happens, how to resolve it, and the contexts where it’s relevant.
Understanding the Error
The only_full_group_by error arises when the GROUP BY clause in a SQL query is used in a way that does not align with the SQL standard defined by the SQL:1999 specification. This option is enabled by default in MySQL versions 5.7 and later. When enabled, it requires that every column in the SELECT list that is not an aggregate function must be specified in the GROUP BY clause. The error occurs when a non-aggregated column that is not included in the GROUP BY clause is selected.
Example of the Error
Consider the following table and query:
This query would produce an error under the only_full_group_by mode because sale_date is neither an aggregated function nor included in the GROUP BY clause.
Key Concepts Involved
- Aggregate Function: A function where the values of multiple rows are grouped together to form a single summary value, like
SUM(),COUNT(),AVG(), etc. - Non-aggregated Columns: Columns in the
SELECTclause that aren't part of an aggregate function. GROUP BYClause: Used to arrange identical data into groups. All non-aggregated columns in aSELECTstatement must be in theGROUP BYclause whenonly_full_group_byis enabled.
Solutions to Resolve the Error
There are several approaches to deal with the only_full_group_by error:
- Include All Non-aggregated Columns in the
GROUP BYClause:Modify the query by adding all non-aggregated columns to theGROUP BYclause. For example:
- Disable
only_full_group_byMode:If the strict mode is not a requirement, you can disableonly_full_group_byby modifying the MySQL configuration or by running:
- Rewrite the Query Using Derived Tables:You can restructure your SQL query using subqueries or derived tables to ensure compliance:
Configuring MySQL SQL Mode
To make permanent changes, edit the MySQL configuration file (usually my.cnf or my.ini) and adjust the sql_mode to exclude ONLY_FULL_GROUP_BY. Afterward, restart the MySQL server.
Conclusion
Understanding and addressing the only_full_group_by error is key to writing efficient and standards-compliant MySQL queries. The mode aims to enforce stricter SQL rules, preventing ambiguous results but may require substantial query modifications or mode adjustments by developers transitioning from older MySQL versions.
Summary Table
| Aspect | Details |
| Error Occurrence | When SELECT contains non-aggregated columns not in GROUP BY. |
| Default Setting | Enabled by default in MySQL versions 5.7 and later. |
| Solution 1 | Add all non-aggregated columns to GROUP BY. |
| Solution 2 | Disable only_full_group_by mode. |
| Solution 3 | Use subqueries or derived tables. |
| Purpose | Enforce stricter SQL standard compliance. |
| Configuration | Modify sql_mode in MySQL configuration or runtime setting. |
Understanding the nuances of only_full_group_by not only helps with writing correct queries but also enhances one's ability to maintain standards-compliance in SQL. Developers dealing with legacy systems may find this understanding especially beneficial when upgrading databases.

