SELECT list is not in GROUP BY clause and contains nonaggregated column .... incompatible with sql_modeonly_full_group_by
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
In the realm of SQL, particularly when dealing with MySQL databases, the sql_mode setting plays a crucial role in determining how SQL statements are executed and how strict the rules are enforced. One common configuration is ONLY_FULL_GROUP_BY, which has implications for how SELECT statements are structured when using GROUP BY operations. This article delves into what it means when you encounter an error such as "SELECT list is not in GROUP BY clause and contains nonaggregated column ... incompatible with sql_mode=only_full_group_by," providing technical explanations and examples to guide understanding and resolution.
Understanding sql_mode=ONLY_FULL_GROUP_BY
ONLY_FULL_GROUP_BY is a setting within MySQL's sql_mode that affects the execution of GROUP BY queries. When enabled, it requires that every column in the SELECT clause that is not part of an aggregate function must also appear in the GROUP BY clause. This strict mode prevents the database from guessing which value to display when non-aggregated columns are included in SELECT but not in GROUP BY.
Example Problem
Consider the following table sales:
| id | product | amount |
| 1 | Widget A | 30 |
| 2 | Widget B | 20 |
| 3 | Widget A | 25 |
| 4 | Widget B | 15 |
A typical incorrect query that might trigger this error is:
When ONLY_FULL_GROUP_BY is enabled, this query will cause an error:
The error occurs because amount is neither aggregated (e.g., by SUM, AVG, etc.) nor included in the GROUP BY clause.
Key Points and Resolutions
| Issue | Explanation/Resolution |
| Error Triggered | Occurs when non-aggregated columns are not included in GROUP BY while in ONLY_FULL_GROUP_BY mode |
| Enforcing Consistency | Ensures queries can be consistently interpreted with defined aggregate and grouped columns |
| Possible Solutions | 1. Add non-aggregated columns to the GROUP BY clause if semantically correct.
2. Use aggregate functions for non-aggregated columns, such as MAX, MIN, SUM, or AVG.
3. Disable ONLY_FULL_GROUP_BY mode if strict compliance isn't necessary (not recommended for consistent codebases). |
| Alternative Strategies | Consider redesigning database schema or restructuring queries for improved adherence to SQL standards |
Applying Resolutions
Adding to GROUP BY
However, this might not work as intended if the goal is to aggregate amounts.
Using an Aggregate Function
This solution adheres to ONLY_FULL_GROUP_BY by applying the SUM aggregate function to amount, making the SQL statement error-free and semantically meaningful.
Disabling ONLY_FULL_GROUP_BY
You might consider this approach in a development or testing environment, though it's not advisable for production due to potential data inconsistency issues:
Note that changing sql_mode requires appropriate privileges and should be used judiciously.
Additional Considerations
Impact on Performance
Explicitly addressing the ONLY_FULL_GROUP_BY requirements can have implications for performance, particularly with large datasets. Efficient use of indices and considering the use of materialized views or summary tables can mitigate potential performance drawbacks.
Best Practices
- Consistency: Maintain consistent query structures across your application to prevent unforeseen errors as database configurations change or the application evolves.
- Testing: Regularly test queries with
ONLY_FULL_GROUP_BYboth enabled and disabled, ensuring robustness in different environments. - Documentation: Clearly document all deviations from default
sql_modevalues, outlining reasons and querying conventions specific to your database schema.
In conclusion, adhering to sql_mode=ONLY_FULL_GROUP_BY is both a safeguard for data integrity and a practice for ensuring that SQL queries can be interpreted unambiguously. Through careful query construction, utilizing aggregate functions, and maintaining comprehensive documentation, developers can effectively manage and harness the power of SQL while avoiding common pitfalls associated with group operations.
Related reading
- Select MongoDB documents where a field either does not exist, is null, or is false?
- Select multiple columns using Entity Framework
- Select Pandas rows based on list index
- Select records from NOW -1 Day
- Select row with most recent date per user
- Select rows from a table that are not in another
- SELECT WHERE NOT EXISTS
- Selecting a row of pandas series/dataframe by integer index

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.