How to Fix MySQL Error 3029 (ER_AGGREGATE_ORDER_NON_AGG_QUERY)

Published
Length
3 min read

Luis Felipe Dias

All posts

You get mysql error 3029 when an order clause uses an aggregate function on raw rows. You fix it by adding a group clause or removing the aggregate function from the sort. Database engines need clear rules to sort data. When you mix summary functions with raw row output, the engine stops the query. This guide explains why this happens and how to fix it in production. Why mysql error 3029 happens in your database The database tracks this issue under symbol ER_AGGREGATE_ORDER_NON_AGG_QUERY and

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 get mysql error 3029 when an order clause uses an aggregate function on raw rows. You fix it by adding a group clause or removing the aggregate function from the sort.

Database engines need clear rules to sort data. When you mix summary functions with raw row output, the engine stops the query. This guide explains why this happens and how to fix it in production.

Why mysql error 3029 happens in your database

The database tracks this issue under symbol ER_AGGREGATE_ORDER_NON_AGG_QUERY and state HY000. The official message says an order expression contains an aggregate function on a non-aggregated query. In plain words, you asked the database to sort individual rows by a single summary number.

Suppose you read a table of user orders. You ask for every single order row, not a summary. Then you tell the engine to sort those individual rows by the total sum of orders. That request makes no logical sense to the query planner.

A sum or count collapses many rows into one single value. If the main query returns raw rows, the engine has no groups to summarize. Older database setups sometimes let bad queries slip through by guessing your intent. Modern strict modes reject this mismatch right away to protect data accuracy.

This failure often pops up during database upgrades or app updates. Query builders in app code can also trigger it by mistake. For example, a developer might append a sort rule to a raw search query.

How to diagnose the query failure

First, find the exact query that caused the error in your app logs. Look at the select list and the sort clause at the end. Check if the select list asks for plain columns without any summary functions.

Next, inspect the sort clause at the bottom of the query. Look for summary functions like count, sum, average, min, or max. If you see one there, look for a group clause before it.

Non-Human Identity Management + MySQL Access Governance: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

When a query lacks a group clause and selects plain columns, it is non-aggregated. Putting a summary function in the sort step breaks the query rules. You now know the exact root cause of the failure.

How to fix the error safely in production

Do not turn off strict SQL modes to bypass this error. Turning off safety checks hides bad logic and returns wrong data to users. Also, do not grant broad admin permissions to developers to test fixes in production.

Instead, fix the query logic in your app code using two clear paths. First, if you want summary data, add a group clause for the selected columns. Now the engine groups the rows first and sorts the groups by the summary value.

Second, if you want every individual row, remove the summary function from the sort step. Sort the rows by a plain column name instead. If you must sort raw rows by a group total, compute the total in a subquery.

When you check production data to verify your fix, keep sensitive fields safe. Query tools with data masking can rewrite matched fields like emails and card numbers in results. This lets your team test query logic without exposing private customer details.

Check MySQL queries and results on the wire

MySQL grants control who logs in. They do not check single queries or mask sensitive columns once a session starts. With hoop.dev, you run an open-source proxy, a sidecar for your MySQL, inline in the MySQL wire protocol. On every query an AI agent or human runs against production MySQL, RDS, or Aurora, hoop.dev decides to pass, mask, block, or hold it for review.

Multi-statement batches and prepared queries are split and checked one by one by hoop.dev guardrails, which refuse banned queries with a native MySQL error or hold risky ones for approval. On the response path, hoop.dev data masking rewrites string values in returned rows in memory, and every decision is logged. See the MIT-licensed project on GitHub.

Frequently asked questions

What does ER_AGGREGATE_ORDER_NON_AGG_QUERY mean in plain English?

It means your query sorts plain rows using a summary math function like count or sum. You must group your rows first before you sort them by a group total.

Should I change SQL modes to stop mysql error 3029?

No, weakening SQL modes lets the database return random or wrong results. Always fix the broken query by adding a group clause or changing the sort column.

Why did this error start after a database upgrade?

Newer database versions enforce strict grouping rules by default. Queries that relied on loose rules in older engines now fail until you fix the logic.

The query runs. The emails stay hidden.