MySQL raises ER_WRONG_FIELD_WITH_GROUP_V2 when a SELECT list or HAVING clause contains a nonaggregated column that is not functionally dependent on columns in the GROUP BY clause while sql_mode includes ONLY_FULL_GROUP_BY.
ER_WRONG_FIELD_WITH_GROUP_V2 (error 3087) appears when a SELECT or HAVING clause references a nonaggregated column not present in the GROUP BY list while ONLY_FULL_GROUP_BY is enabled. Add the column to GROUP BY, wrap it in an aggregate, or disable ONLY_FULL_GROUP_BY to resolve the issue.
ER_WRONG_FIELD_WITH_GROUP_V2
MySQL throws error 3087 when ONLY_FULL_GROUP_BY mode is active and a query references a column that is neither aggregated nor listed in the GROUP BY clause. The server enforces SQL standard semantics to avoid ambiguous results.
The error text highlights the offending column and indicates that it is not functionally dependent on the grouped columns. Fixing the query or changing the SQL mode removes the failure.
The condition appears during SELECT, HAVING, or ORDER BY evaluation. It was introduced in MySQL 5.7.6 and remains active in all later versions, including MySQL 8.x.
It is triggered only if ONLY_FULL_GROUP_BY belongs to the current session or global sql_mode setting.
Leaving queries in an invalid state blocks reporting jobs, ETL pipelines, and application features that rely on grouped results. Prompt correction ensures accurate aggregates and prevents runtime outages.
A SELECT list includes a plain column that is absent from the GROUP BY list, for example SELECT id, MAX(score) FROM test GROUP BY id.
The HAVING clause references a field that is neither aggregated nor grouped.
Developers expect a primary key or unique index to guarantee functional dependency, but the column combination is incomplete.
MySQL 5.7+ ships with ONLY_FULL_GROUP_BY in sql_mode, surprising users migrating from older versions.
Legacy variant before version 5.7 that reports similar aggregation issues.
Column ambiguously referenced in GROUP BY or ORDER BY clauses.
Nonaggregated column appears in HAVING without GROUP BY.
Yes, but it may mask data issues. Prefer rewriting queries unless legacy code cannot be updated.
ONLY_FULL_GROUP_BY was disabled by default before 5.7, so relaxed grouping rules applied.
ANY_VALUE returns an arbitrary value from the group. Use it only when the column is functionally dependent on the GROUP BY keys.
Galaxy’s SQL editor highlights aggregation errors in real time and its AI copilot refactors queries to meet ONLY_FULL_GROUP_BY rules automatically.