Common SQL Errors

MySQL Error 3029 ER_AGGREGATE_ORDER_NON_AGG_QUERY: How to Fix and Prevent

Galaxy Team
August 8, 2025

The error appears when ORDER BY contains an aggregate function in a SELECT that lacks GROUP BY or other aggregation context.

Sign up for the latest in common SQL errors from the Galaxy Team!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

What is MySQL error ER_AGGREGATE_ORDER_NON_AGG_QUERY?

ER_AGGREGATE_ORDER_NON_AGG_QUERY arises when an ORDER BY clause holds an aggregate like SUM() or COUNT() in a query without GROUP BY. Remove the aggregate or add an appropriate GROUP BY to resolve the issue.

Error Highlights

Typical Error Message

ER_AGGREGATE_ORDER_NON_AGG_QUERY

Error Type

Syntax Error

Language

MySQL

Symbol

and applies to the result of a non-aggregated query ER_AGGREGATE_ORDER_NON_AGG_QUERY was added in 5.7.5.

Error Code

3029

SQL State

HY000

Explanation

Table of Contents

What is MySQL error ER_AGGREGATE_ORDER_NON_AGG_QUERY?

Error 3029 (SQLSTATE HY000) is raised when a SELECT statement uses ORDER BY with an aggregate function such as SUM(), COUNT(), MAX(), MIN(), or AVG() but the query itself is not aggregated with GROUP BY, DISTINCT, or a window function.

MySQL added this safeguard in version 5.7.5 to prevent ambiguous result sets where rows cannot be reliably ordered by values derived from multiple rows.

When does the error show up?

The message appears immediately when the SQL parser detects an aggregate in ORDER BY without the surrounding aggregation context. It blocks execution before any data retrieval starts.

Developers typically see it while refactoring reports, copying snippets from BI tools, or adding quick ORDER BY clauses for readability.

Why is it important to fix?

Leaving the query uncorrected leads to failed deployments, broken reports, and blocked ETL jobs. Fixing it ensures predictable ordering and keeps production pipelines healthy.

What Causes This Error?

Placing SUM(), COUNT(), or a similar aggregate directly inside ORDER BY for a simple SELECT is the primary trigger. MySQL cannot evaluate row-level ordering using multi-row calculations without grouping.

Another cause is using ORDER BY 3 when the third SELECT column is an aggregate, yet the query lacks GROUP BY. The positional reference still counts as an aggregate expression.

How to Fix ER_AGGREGATE_ORDER_NON_AGG_QUERY

The quickest remedy is ordering by a non-aggregate column or by a column alias already computed in a GROUP BY query.

If the aggregate sort is required, rewrite the statement with GROUP BY or a subquery that returns the aggregate per group, then order the outer query.

Common Scenarios and Solutions

Reporting queries that total revenue but forget GROUP BY date often hit this error. Add GROUP BY date and select the aggregate correctly.

Analytics dashboards sometimes wrap ORDER BY SUM(value) around a raw fact table. Move the aggregation into a CTE or subquery, then order.

Best Practices to Avoid This Error

Always design ORDER BY clauses after deciding on aggregation strategy. Validate queries in a modern SQL IDE like Galaxy that underlines aggregate mis-use in real time.

Write unit tests for critical ETL steps to detect syntax errors early. Enable MySQL SQL_MODE options that enforce strict standards.

Related Errors and Solutions

ER_MIX_OF_GROUP_FUNC_AND_FIELDS occurs when non-aggregated columns appear with aggregates in SELECT without GROUP BY. The fix pattern is similar: add grouping or remove the offending column.

ER_WRONG_FIELD_WITH_GROUP handles invalid HAVING references and is resolved by selecting aggregated or grouped columns only.

Common Causes

ORDER BY uses SUM() without GROUP BY

A developer appends ORDER BY SUM(sales) to a plain SELECT, triggering the error because MySQL cannot order individual rows by a multi-row calculation.

Positional ORDER BY referencing an aggregate column

Using ORDER BY 3 when the third column is COUNT(*) causes the same issue since position 3 expands to the aggregate expression.

Dynamic SQL building errors

Applications that stitch SQL fragments sometimes forget to add GROUP BY when adding aggregate functions, leading to this runtime failure.

Related Errors

ER_MIX_OF_GROUP_FUNC_AND_FIELDS

Raised when non-aggregated columns mix with aggregates in SELECT without GROUP BY.

ER_WRONG_FIELD_WITH_GROUP

Occurs when HAVING or ORDER BY references columns not in GROUP BY.

ER_NON_GROUPING_FIELD_USED

Appears when the SELECT list contains columns that are neither aggregated nor grouped.

FAQs

Can I disable this error with sql_mode?

No. The check is hard-coded from 5.7.5 onward. Queries must be rewritten.

Does using DISTINCT avoid the error?

Yes. DISTINCT counts as an aggregation context, so ORDER BY COUNT(*) works if SELECT DISTINCT is used.

Why did my old query start failing after upgrade?

MySQL 5.7.5 introduced the stricter validation. Older versions allowed ambiguous ordering, so upgrading exposes latent issues.

How does Galaxy help?

Galaxy’s editor warns about aggregate misuse in real time and its AI copilot suggests correct GROUP BY clauses, reducing debugging time.

Start Querying with the Modern SQL Editor Today!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Check out some other errors

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo