Common SQL Errors

MySQL Error 3028: ER_AGGREGATE_ORDER_FOR_UNION - How to Fix and Prevent

Galaxy Team
August 8, 2025

MySQL raises error 3028 when an ORDER BY inside a UNION references an aggregate function such as SUM or COUNT.

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 3028 ER_AGGREGATE_ORDER_FOR_UNION?

ER_AGGREGATE_ORDER_FOR_UNION (MySQL error 3028) occurs when the ORDER BY clause of a UNION query contains an aggregate like SUM(). Remove the aggregate, group first, or wrap the UNION in a subquery and sort outside to resolve the issue.

Error Highlights

Typical Error Message

ER_AGGREGATE_ORDER_FOR_UNION

Error Type

Query Syntax Error

Language

MySQL

Symbol

and applies to a UNION ER_AGGREGATE_ORDER_FOR_UNION was added in 5.7.5.

Error Code

3028

SQL State

HY000

Explanation

Table of Contents

What is MySQL error ER_AGGREGATE_ORDER_FOR_UNION (3028)?

MySQL error 3028 appears when a UNION or UNION ALL query tries to sort the combined result set using an aggregate function in its ORDER BY clause.

The server rejects the statement because aggregates are calculated after ORDER BY in UNION processing, making such sorting logically ambiguous.

What Causes This Error?

The error is triggered whenever ORDER BY includes functions like SUM(), COUNT(), AVG(), MIN(), or MAX() directly after a UNION block.

MySQL versions 5.7.5 and later enforce this rule to keep execution plans deterministic and standards-compliant.

How to Fix ER_AGGREGATE_ORDER_FOR_UNION

Move the ORDER BY outside the UNION by wrapping the set operation in a subquery.

Alternatively, compute aggregates in a derived table, join back, and then sort. Removing the aggregate from ORDER BY also works if the exact ordering is not essential.

Common Scenarios and Solutions

Report queries that UNION daily totals and then try to ORDER BY SUM(total) fail. Wrapping the UNION in a subquery and ordering outside restores functionality.

Ad-hoc analytics that UNION multiple SELECTs with COUNT(*) in ORDER BY must either GROUP BY first or sort by non-aggregate columns.

Best Practices to Avoid This Error

Always treat UNION output as a virtual table. Apply ORDER BY and LIMIT only after the full set is materialised.

Use Galaxy’s query linter to detect aggregates in ORDER BY within UNION blocks before execution.

Related Errors and Solutions

ER_ORDER_BY_AGGREGATE (1583) appears in non-UNION queries when ORDER BY contains aggregates without GROUP BY.

ER_FIELD_IN_ORDER_NOT_SELECT (3065) occurs when ORDER BY references a column not present in SELECT. Both issues resolve by adjusting ORDER BY or using derived tables.

Common Causes

Aggregate in ORDER BY

Including SUM(), COUNT(), or other aggregate functions directly in the ORDER BY list following a UNION statement.

Version Upgrade

Migrating to MySQL 5.7.5+ where stricter parsing detects invalid ORDER BY usage that older versions allowed.

Copy-Pasted Analytics SQL

Ad-hoc queries copied between tools often add ORDER BY SUM(x) without realising a UNION appears earlier.

Related Errors

ER_ORDER_BY_AGGREGATE (1583)

Raised when ORDER BY contains an aggregate without GROUP BY in non-UNION queries.

ER_FIELD_IN_ORDER_NOT_SELECT (3065)

Occurs if ORDER BY references a column not in the SELECT list when ONLY_FULL_GROUP_BY is active.

ER_WRONG_GROUP_FIELD (1055)

Triggered when a non-aggregated column in SELECT is not included in GROUP BY under strict mode.

FAQs

Can I disable this check in MySQL?

No configuration parameter bypasses ER_AGGREGATE_ORDER_FOR_UNION. You must rewrite the query.

Does the error affect UNION ALL differently from UNION?

Both UNION and UNION ALL raise error 3028 because ordering rules are identical.

Will wrapping the query in a view help?

Yes. Creating a view or derived table and then selecting from it lets you apply ORDER BY safely.

How does Galaxy help avoid this error?

Galaxy’s static analysis flags aggregates in ORDER BY within UNION queries and suggests subquery rewrites before you run the SQL.

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