<p>MySQL raises ER_SP_NO_AGGREGATE when an aggregate function is placed inside a stored function.</p>
<p>MySQL Error 1460 ER_SP_NO_AGGREGATE occurs when you use aggregate functions like SUM or COUNT in a stored function. Move the aggregation into a stored procedure, view, or calling query to resolve the error.</p>
AGGREGATE is not supported for stored functions
MySQL raises error 1460 with condition ER_SP_NO_AGGREGATE when the CREATE FUNCTION or ALTER FUNCTION statement contains an aggregate such as SUM, COUNT, AVG, MIN, MAX or GROUP_CONCAT. The engine blocks the statement and returns the message AGGREGATE is not supported for stored functions.
The restriction exists because stored functions must return a single deterministic value without scanning multiple rows, which aggregates naturally require. Allowing aggregates could break row by row evaluation and binary logging consistency.
The error surfaces immediately during compilation of the function definition. It can also appear at runtime after an upgrade if a legacy function with an aggregate is invoked.
The function creation fails, blocking deployments and causing dependent application code to throw unknown function errors. Resolving ER_SP_NO_AGGREGATE keeps CI pipelines and production releases running.
Developers can switch to a stored procedure, view or inline SELECT to compute aggregates. Procedures allow aggregates and can return result sets or OUT parameters. Views encapsulate group logic while staying reusable in Galaxy collections.
Galaxy's editor highlights disallowed aggregates in real time. The AI copilot proposes refactoring the logic into a procedure or view and lets teams endorse the corrected query so others reuse the safe pattern.
Including SUM(column) in a RETURN statement or variable assignment violates the no aggregate rule.
Calling COUNT(*) inside a function to validate existence triggers the error.
A SELECT ... GROUP BY inside the function is treated as an aggregate and is blocked.
Even nested SELECT statements with aggregates count toward the restriction and raise ER_SP_NO_AGGREGATE.
Raised when certain statements like INSERT or DELETE appear in a stored function.
Indicates that subqueries were once not supported inside stored routines in early versions.
Thrown when a referenced routine name cannot be found or has been dropped.
No. The limitation is hard coded in the parser and cannot be disabled by SQL_MODE.
No. Procedures can freely use aggregate functions and return the results.
As of MySQL 8.1 the roadmap has not indicated a change. Rely on procedures or views instead.
Automated lints like Galaxy's static checker or mysql --sql-mode=traditional during deploy will abort on ER_SP_NO_AGGREGATE.