How to Fix MySQL Error 1140 (ER_MIX_OF_GROUP_FUNC_AND_FIELDS)

Published
Length
3 min read

Luis Felipe Dias

All posts

You trigger mysql error 1140 when a query mixes aggregate functions with standard columns without grouping. You fix this issue by adding a grouping clause or removing the non-aggregated columns. Database engines enforce strict rules on how queries summarize data across rows. When a query asks for a single summary value alongside individual row values, the engine stops. It returns SQLSTATE 42000 with the symbol ER_MIX_OF_GROUP_FUNC_AND_FIELDS to protect data accuracy. Why mysql error 1140 happ

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

You trigger mysql error 1140 when a query mixes aggregate functions with standard columns without grouping. You fix this issue by adding a grouping clause or removing the non-aggregated columns.

Database engines enforce strict rules on how queries summarize data across rows. When a query asks for a single summary value alongside individual row values, the engine stops. It returns SQLSTATE 42000 with the symbol ER_MIX_OF_GROUP_FUNC_AND_FIELDS to protect data accuracy.

Why mysql error 1140 happens in your database

The database engine reads your query to build a clear execution plan. Aggregate functions like count, minimum, or maximum collapse many rows into one single summary row. Standard columns without an aggregate function ask the database to return every matching row instead.

When you mix both in one query without a grouping clause, the engine hits a conflict. It does not know which single row value to pair with the summary value. Older database setups sometimes guessed a random row and returned silent bad data. Modern setups enable strict grouping rules to stop this bad behavior right away.

The official message states that mixing group columns with non-group columns is illegal without grouping. This rule comes from the SQL standard for functional dependency. Every selected column must either live inside an aggregate function or appear in the grouping list.

How to diagnose the failing query in production

To find the broken query, check your app error logs for code 1140 or SQLSTATE 42000. The log entry points to the exact service or report that sent the bad statement. Once you find the query, look at all column listed in the select part.

Separate the columns into two lists on paper or in a text note. Put summary functions like count, sum, average, minimum, and maximum in the first list. Put plain table columns in the second list.

DPoP (Demonstration of Proof-of-Possession) + MySQL Access Governance: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Now check the bottom of your query for a grouping clause. If your query has items in both lists and no grouping clause, you found the bug. You may also see this error when a query builder adds an extra column by mistake. Object relational mappers often add primary keys to a summary query when settings are wrong.

How to fix the query without disabling safety checks

Some guides tell you to turn off strict grouping rules in the server settings. Do not do this in production. Turning off strict mode lets the database return random row values in your reports. It also requires high admin rights that app teams should not use for a query bug.

Instead, fix the query logic in your app code using one of three safe methods. First, if you want a summary broken down by category, add a grouping clause. List every plain column from your select list inside that grouping clause.

Second, if you only need the single summary value, delete the plain columns from the query. Third, if you need row details alongside a total count, use a window function or subquery. Window functions calculate totals across a set of rows without collapsing the main result set.

When you test these fixes against production data, protect sensitive user fields first. Dynamic data masking rewrites matched fields like emails, phone numbers, card numbers, and national IDs in results. This lets your team verify the fixed query logic safely during a live debug session.

Add wire guardrails and masking beside MySQL

Database grants and TLS setup decide who can reach MySQL. Even so, an open session can run broad updates or read raw personal data from allowed tables. In practice, hoop.dev sits inline in the wire protocol beside the database as a sidecar for your MySQL. On every production query an agent or engineer sends, hoop.dev decides to pass, mask, block, or escalate it for human review.

With hoop.dev MySQL support, guardrails refuse bad queries before they run or hold risky calls for approval, while data masking rewrites matched sensitive values in result rows in memory. Furthermore, hoop.dev holds SOC 2 Type II and generates evidence for GDPR, HIPAA, and PCI DSS programs. Read the docs and source on GitHub.

Frequently asked questions

What does ER_MIX_OF_GROUP_FUNC_AND_FIELDS mean in MySQL?

This symbol means your query combines summary functions like count or max with plain table columns. Because the query lacks a grouping clause, the database rejects the ambiguous request with code 1140.

Should I disable strict grouping mode to fix error 1140?

No, you should never turn off strict grouping rules to bypass this error. Disabling the check causes the database to pick random row values, which corrupts your financial or user reports.

Why did this error appear after a database upgrade?

Newer database versions turn on strict SQL grouping rules by default to match modern standards. Queries that ran with hidden bugs on older servers will fail until you add proper grouping.

The query runs. The emails stay hidden.