3368
MariaDBERRORNotableQuery ExecutionHIGH confidence

Non-aggregated column in SELECT list with GROUP BY (v2)

Production Risk

Medium — existing queries may break when ONLY_FULL_GROUP_BY is enforced.

How to reproduce
trigger — this will error
trigger — this will error
SET sql_mode = 'ONLY_FULL_GROUP_BY'; SELECT name, COUNT(*) FROM t GROUP BY id;

expected output

ERROR 3368 (HY000): Expression #1 of SELECT list is not in GROUP BY clause.

Fix

Use ANY_VALUE() for non-deterministic column

Use ANY_VALUE() for non-deterministic column
SELECT ANY_VALUE(name), COUNT(*) FROM t GROUP BY id;

Why this works

ANY_VALUE() explicitly acknowledges that any value from the group is acceptable.

What not to do

Sources
Official documentation ↗

MySQL 8.0 — 3368 ER_WRONG_FIELD_WITH_GROUP_V2

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All MariaDB errors