Australian cloud for business software — built and run in Sydney since 2002

NetLeverage
NetLeverage

MySQL ERROR 1055 (42000) "Expression #2 of SELECT list is not in GROUP BY clause": make the query deterministic before you switch only_full_group_by off

Technical article
Troubleshooting

About this article: This article is reviewed against the cited sources. This is source research, not lab validation. Check the stated versions and your own requirements before making changes.

MySQL stops a query with ERROR 1055 (42000): Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'mydb.t.address' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by when the select list names a column that is not grouped, not aggregated and not determined by the grouped columns. The number after # is the position in the select list, and the quoted name is the column. Fix the query first. Turning the mode off lets MySQL return any value from each group.

What the error means

The manual describes the ONLY_FULL_GROUP_BY SQL mode like this: "Reject queries for which the select list, HAVING condition, or ORDER BY list refer to nonaggregated columns that are neither named in the GROUP BY clause nor are functionally dependent on (uniquely determined by) GROUP BY columns." The MySQL 8.4 manual says the mode is enabled "(which it is by default)", and its list of default modes for MySQL 8.4 and for MySQL 8.0 starts with ONLY_FULL_GROUP_BY.

The mode became a default in MySQL 5.7.5, a milestone release dated 25 September 2014. Its release notes say "ONLY_FULL_GROUP_BY is now enabled by default". Our own reading: the error often appears when an older application meets a newer server.

The manual shows the message as ERROR 1055 (42000). The 8.4 Error Reference lists number 1055 with the short text "'%s' isn't in GROUP BY". It lists the long text under number 3087 (symbol ER_WRONG_FIELD_WITH_GROUP_V2, SQLSTATE HY000) as "Expression #%u of %s is not in GROUP BY clause and 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". Search for the long text, whichever number your client prints.

A second form covers a query with an aggregate function and no GROUP BY. The manual prints it as ERROR 1140 (42000): In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column 'mydb.t.name'; this is incompatible with sql_mode=only_full_group_by. The Error Reference lists the same text under 3088 as "In aggregated query without GROUP BY, expression #%u of %s contains nonaggregated column '%s'; this is incompatible with sql_mode=only_full_group_by".

When the mode is off, the manual says "the values chosen are nondeterministic, which is probably not what you want", and an ORDER BY does not change which value is chosen.

Likely causes

Cause What you see How to confirm
A selected column is not in GROUP BY and is not determined by it Error 1055 naming the column The column is missing from GROUP BY; the grouped column is not a primary key or a unique NOT NULL column of that table
An aggregate function with no GROUP BY beside a plain column The 1140 or 3088 text The select list mixes MAX(), SUM() or COUNT() with a plain column
A newer server or a new session setting The query ran before a move to a newer server SELECT @@GLOBAL.sql_mode; on both servers
DISTINCT with ORDER BY on a column that is not selected The manual says such a query "is rejected as invalid if any ORDER BY expression does not satisfy at least one of these conditions" The ORDER BY column is not in the select list
A formula over a GROUP BY expression Error although the formula looks derived The GROUP BY holds a function result such as FLOOR(value/100) and the select list adds to it
The values are the same but MySQL cannot prove it Error on a query you know is valid Check the data with a read-only query

Prove the cause before changing anything

Everything here only reads.

  1. Read the message. Expression #2 is the second item in the select list. The quoted name, such as 'shop.sales.rep', is the column.
  2. Read the modes. The manual gives these two statements:
SELECT @@GLOBAL.sql_mode;
SELECT @@SESSION.sql_mode;

A value that contains ONLY_FULL_GROUP_BY is the cause of the rejection. If the session value differs from the global one, the application or connection library sets its own value; the manual says "each application can set its session SQL mode to its own requirements".

  1. Look for a key. The manual says a query is valid when the selected column is functionally dependent on a grouped column, for example when the grouped column is a primary key. It adds: "A UNIQUE index over a NOT NULL column could be used instead of a primary key and the same functional dependence would apply". A UNIQUE index that allows NULL does not qualify. Show the definition:
SHOW CREATE TABLE shop.sales\G
  1. If you believe each group holds one value, prove it before relying on that. Count distinct values per group; any group with a count above 1 makes the value arbitrary:
SELECT region, COUNT(DISTINCT rep) FROM shop.sales GROUP BY region;

Fix

Choose in this order: make the query valid, tell MySQL the values are the same, and only then change the mode for one session.

Edit the query

First save the current state:

cp -p report.sql report.sql.2026-12-03

Risk: the next step edits the query in report.sql. If you add a column to GROUP BY, the result has one row per combination, so it can return more rows than before and a different total layout for whatever reads it. If you use ANY_VALUE() on a column whose values differ within a group, the result shows an arbitrary one of them and can change between runs.

Edit the query in one of these ways. Run only the one that applies.

Add the column to GROUP BY, or drop it from the select list:

SELECT region, rep, SUM(amount) FROM shop.sales GROUP BY region, rep;

Or, when the proof in step 4 showed one value per group, wrap the column in ANY_VALUE(). The manual says the function "simply acts to suppress the test for nondeterminism"; it is not an aggregate function:

SELECT region, ANY_VALUE(rep), SUM(amount) FROM shop.sales GROUP BY region;

For a formula over a GROUP BY expression, the manual's workaround is "to use a derived table".

To undo it: copy the saved file back, then run the original query to see the same error again.

Risk: if you need to back out, the next command overwrites report.sql with the saved copy and discards any later edits to it.

cp -p report.sql.2026-12-03 report.sql

Change the mode for one session

First save the current state: run this statement and copy its result into a file named sql_mode-before.txt.

SELECT @@SESSION.sql_mode;

Risk: the next statement changes the SQL mode of the current connection only. "Setting the SESSION variable affects only the current client." Queries on this connection then accept nondeterministic selects, and results can differ between runs.

Write the saved list from sql_mode-before.txt without the one entry ONLY_FULL_GROUP_BY. For MySQL 8.4 the default list is ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION, so without that entry it is STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION.

SET SESSION sql_mode = '<saved list without ONLY_FULL_GROUP_BY>';

This changes only the connection that runs it. To change what an application sees, the application must set its own session sql_mode when it connects.

To undo it: set the session value back to the complete list saved in sql_mode-before.txt, then read it with SELECT @@SESSION.sql_mode;.

Risk: if you need to back out, the next statement turns ONLY_FULL_GROUP_BY on again for the current connection; queries that rely on the nondeterministic selects then fail with error 1055 again.

SET SESSION sql_mode = '<saved list>';

The global value also exists. The manual says setting it "requires the SYSTEM_VARIABLES_ADMIN privilege (or the deprecated SUPER privilege) and affects the operation of all clients that connect from that time on". No command for it is given here, because the change reaches every client.

Example (illustrative): On db01, shop.sales holds four rows: (1, north, Ana, 100.00), (2, north, Ana, 50.00), (3, south, Ben, 70.00), (4, south, Cal, 30.00). The query SELECT region, rep, SUM(amount) FROM shop.sales GROUP BY region; fails with Expression #2 of SELECT list ... 'shop.sales.rep'. The check in step 4 returns 1 for north and 2 for south, so ANY_VALUE(rep) would pick Ben or Cal for the south row. Grouping by region, rep returns three rows: north and Ana 150.00 (100.00 + 50.00), south and Ben 70.00, south and Cal 30.00. The totals for south are 70.00 + 30.00 = 100.00 either way.

Confirm the fix

  1. The edited query runs without error 1055, 1140 or 3088 on a session where SELECT @@SESSION.sql_mode; still lists ONLY_FULL_GROUP_BY.
  2. Compare the result with the old one: the row count and the totals match what the report needs.
  3. Run it twice. With ANY_VALUE() the selected column may differ between runs; with GROUP BY it does not.

What to watch afterwards

  • Test application queries on a server with the default mode before a move to a newer MySQL version.
  • Do not leave ANY_VALUE() on a column whose values differ within a group.
  • Queries that mix a grouped expression with a formula of it need a derived table, as the manual shows.
  • A reporting tool that sets its own sql_mode per connection can reintroduce the difference; compare the session values from step 2.

Risks and limits

  • These commands have not been run or lab-tested.
  • Switching the mode off hides the fault. The manual says that disabling the mode "is useful primarily when you know that, due to some property of the data, all values in each nonaggregated column not named in the GROUP BY are the same for each group".
  • The manual warns on changing the server SQL mode: "It is strongly recommended that you never change the SQL mode once you have created tables employing user-defined partitioning." It also advises that a source and its replicas use the same server SQL mode, because differing modes "can also lead to problems" with replicated partitioned tables.
  • The error numbers above are those printed in the 8.4 manual and Error Reference; check the number your own server prints.

Sources