MySQL & MariaDB error 1055: Expression is not in GROUP BY clause (only_full_group_by)
Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 't_0.sales.rep' which is not functionally dependent on columns in GROUP (5.7.44) With ONLY_FULL_GROUP_BY, every selected column must be grouped, aggregated, or functionally dependent on the grouped columns. The query selects one that isn't, so the server won't pick an arbitrary value for you.
| MySQL | MariaDB | |
|---|---|---|
| Symbol | ER_WRONG_FIELD_WITH_GROUP | ER_WRONG_FIELD_WITH_GROUP |
| SQLSTATE | 42000 | 42000 |
| Type | Server error | Server error |
| Versions | every release since 5.0 active | every release since 5.5 active |
What it means
Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'shop.sales.rep' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by.
In SELECT region, rep, SUM(amount) … GROUP BY region there are several rep values per region, and the query doesn’t say which one you want. Older MySQL simply returned one of them, unpredictably.
Why it happens
ONLY_FULL_GROUP_BYhas been in MySQL’s defaultsql_modesince 5.7.5, so queries written for 5.6 start failing after an upgrade. MySQL 5.6 and MariaDB don’t enable it by default (our tests show the default behaviour per version).- Selecting extra descriptive columns “for convenience” alongside an aggregate.
How to fix it
- Group by every selected column that isn’t aggregated:
GROUP BY region, rep. - Aggregate it:
MAX(rep),MIN(rep),GROUP_CONCAT(rep). - Say you don’t care which value:
ANY_VALUE(rep)(MySQL 5.7+). - Group by the primary key: columns that depend on it are then allowed (functional dependency).
- Removing
ONLY_FULL_GROUP_BYfromsql_modemakes the error go away but brings back unpredictable results.
Reproduce it yourself
These steps are the exact test our harness runs. Use a scratch server, never production.
Connect with
mysql -u root -pand create a scratch database and the test data:CREATE DATABASE demo; USE demo; CREATE TABLE sales (region VARCHAR(10), rep VARCHAR(10), amount INT); INSERT INTO sales VALUES ('east','ann',10),('east','bob',20),('west','cy',5);Run:
SET SESSION sql_mode = CONCAT(@@sql_mode, ',ONLY_FULL_GROUP_BY'); SELECT region, rep, SUM(amount) FROM sales GROUP BY region;
You get: 't_0.sales.rep' isn't in GROUP BY(11.8.9)
Verified fixes
Say which value you want: ANY_VALUE(), MIN()/MAX(), or add it to GROUP BY
SELECT region, MAX(rep), SUM(amount) FROM sales GROUP BY region ORDER BY region;
Tested on
| Step | MySQL | MariaDB | |||||||
|---|---|---|---|---|---|---|---|---|---|
| 5.6 | 5.7 | 8.0 | 8.4 | 9 | 10.6 | 10.11 | 11.4 | 11.8 | |
| With ONLY_FULL_GROUP_BY on, select a column that is neither grouped nor aggregated repro | ✓ 1055 | ✓ 1055 | ✓ 1055 | ✓ 1055 | ✓ 1055 | ✓ 1055 | ✓ 1055 | ✓ 1055 | ✓ 1055 |
| Same query with the server's default sql_mode (ONLY_FULL_GROUP_BY is default on MySQL 5.7+ only) version check | ≠ | ✓ 1055 | ✓ 1055 | ✓ 1055 | ✓ 1055 | ≠ | ≠ | ≠ | ≠ |
| Say which value you want: ANY_VALUE(), MIN()/MAX(), or add it to GROUP BY fix | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
What the warnings and failures say (1)
- Same query with the server's default sql_mode (ONLY_FULL_GROUP_BY is default on MySQL 5.7+ only) (MySQL 5.6, MariaDB 10.6, MariaDB 10.11, MariaDB 11.4, MariaDB 11.8): expected error 1055 did not occur