How to Fix MySQL Error 3028 (ER_AGGREGATE_ORDER_FOR_UNION)
You trigger mysql error 3028 when your query sorts combined result sets using an aggregate function. You fix it by calculating the aggregate value inside each subquery before you sort the rows. This failure uses the official symbol ER_AGGREGATE_ORDER_FOR_UNION and returns SQLSTATE HY000. MySQL stops the query and says the sort expression contains an aggregate function for a set operation. This rule applies to UNION, EXCEPT, and INTERSECT statements across the database engine. Understanding how
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 3028 when your query sorts combined result sets using an aggregate function. You fix it by calculating the aggregate value inside each subquery before you sort the rows.
This failure uses the official symbol ER_AGGREGATE_ORDER_FOR_UNION and returns SQLSTATE HY000. MySQL stops the query and says the sort expression contains an aggregate function for a set operation. This rule applies to UNION, EXCEPT, and INTERSECT statements across the database engine. Understanding how the engine reads combined rows helps your team write safe queries.
Why mysql error 3028 happens in the database engine
A set operation merges rows from two or more separate select queries into one final list. The database engine finishes each select statement first and then combines the resulting rows together. At this final stage, the raw source tables are no longer open for grouping.
When you place a sum, count, or average in the final sort clause, MySQL gets confused. An aggregate function needs grouped rows to compute a single value from many records. The final sort step only sees flat, merged rows and cannot run new group math. Because the engine cannot compute that aggregate value on merged outputs, it returns mysql error 3028.
Teams often hit this error after they upgrade their database or tighten SQL modes. Older setups sometimes ignored broken sort logic or picked random values during a merge. Modern database rules block ambiguous math so your app never gets wrong sort orders. The engine forces you to state your math clearly before the final sort runs.
How to diagnose and fix the query in production
Start by checking your app logs to find the exact query that threw code 3028. The error message gives you the exact position number of the bad sort expression. Count the items in your final sort clause to spot the aggregate function right away.
You have two clean ways to fix the query without turning off database safety rules. First, you can move the aggregate function into the select list of each individual subquery. Give that calculated column a clear alias name in the first select statement. Then, tell the final sort clause to order the merged rows by that plain alias.
MySQL Access Governance + End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Your second option is to wrap the entire combined query inside an outer derived table. The inner query merges the rows from your union, except, or intersect blocks first. Then, the outer query groups those merged rows and sorts them with the aggregate function. Both fixes follow strict SQL rules and keep your production data safe and accurate.
How to prevent query failures across your team
Fixing a broken query in code takes time when your team lacks safe database workflows. Engineers often ask for broad admin roles just to test a failing report in production. Granting wide roles to fix one query creates huge security risks for your company.
A better path is to check read queries through a central proxy layer with least privilege. You can set up role rules so developers only run safe select checks on live data. You can also turn on data masking to rewrite emails, phone numbers, and card numbers in results. This lets your team test complex union logic and fix errors while keeping private fields safe.
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 SQLSTATE HY000 with code 3028 mean in MySQL?
Code 3028 with SQLSTATE HY000 means a final sort clause uses an aggregate function on merged rows. MySQL blocks this pattern across UNION, EXCEPT, and INTERSECT queries because grouping is already done.
Can I fix mysql error 3028 by changing server settings?
No, you should not change server modes to hide this query structure error. The engine blocks this pattern because it cannot compute new group math during the final sort step. You must rewrite the query to calculate the aggregate column before sorting.
Does this error only happen with UNION statements?
No, the ER_AGGREGATE_ORDER_FOR_UNION symbol covers three different set operations in the database. You will see the exact same error if you use aggregate sorting on EXCEPT or INTERSECT queries.
Related guides
In this post