Common SQL Errors

MySQL Error 3087: ER_WRONG_FIELD_WITH_GROUP_V2 - How to Fix the ONLY_FULL_GROUP_BY Aggregation Issue

Galaxy Team
August 8, 2025

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.

Sign up for the latest in common SQL errors from the Galaxy Team!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

What is MySQL error 3087 (ER_WRONG_FIELD_WITH_GROUP_V2)?

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.

Error Highlights

Typical Error Message

ER_WRONG_FIELD_WITH_GROUP_V2

Error Type

Aggregation Error

Language

MySQL

Symbol

contains nonaggregated column '%s' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by ER_WRONG_FIELD_WITH_GROUP_V2 was added in 5.7.6.

Error Code

3087

SQL State

HY000

Explanation

Table of Contents

What does ER_WRONG_FIELD_WITH_GROUP_V2 mean?

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.

When does this error occur?

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.

Why is it important to resolve quickly?

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.

Common Causes

Missing column in GROUP BY

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.

Nonaggregated column in HAVING

The HAVING clause references a field that is neither aggregated nor grouped.

Implicit functional dependency assumption

Developers expect a primary key or unique index to guarantee functional dependency, but the column combination is incomplete.

ONLY_FULL_GROUP_BY enabled by default

MySQL 5.7+ ships with ONLY_FULL_GROUP_BY in sql_mode, surprising users migrating from older versions.

Related Errors

ER_WRONG_FIELD_WITH_GROUP

Legacy variant before version 5.7 that reports similar aggregation issues.

ER_NON_UNIQ_ERROR 1052

Column ambiguously referenced in GROUP BY or ORDER BY clauses.

ER_FIELD_IN_GROUPING_NOT_AGGREGATE 1463

Nonaggregated column appears in HAVING without GROUP BY.

FAQs

Can I just turn off ONLY_FULL_GROUP_BY?

Yes, but it may mask data issues. Prefer rewriting queries unless legacy code cannot be updated.

Why did my query work in MySQL 5.6?

ONLY_FULL_GROUP_BY was disabled by default before 5.7, so relaxed grouping rules applied.

Is ANY_VALUE safe to use?

ANY_VALUE returns an arbitrary value from the group. Use it only when the column is functionally dependent on the GROUP BY keys.

How does Galaxy help?

Galaxy’s SQL editor highlights aggregation errors in real time and its AI copilot refactors queries to meet ONLY_FULL_GROUP_BY rules automatically.

Start Querying with the Modern SQL Editor Today!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Check out some other errors

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo