<p>The SELECT ... PROCEDURE syntax in MySQL cannot be combined with an ORDER BY clause, triggering error 1386.</p>
<p>MySQL Error 1386: ER_ORDER_WITH_PROC appears when a SELECT ... PROCEDURE query also contains ORDER BY. Remove ORDER BY or sort the results in a wrapping subquery to resolve the error.</p>
Can't use ORDER clause with this procedure
MySQL raises error 1386 with the message "Can't use ORDER clause with this procedure" when a SELECT statement that uses the PROCEDURE clause is paired with an ORDER BY clause. The SQL parser blocks this combination because row processing inside PROCEDURE cannot maintain the requested ordering.
The PROCEDURE keyword is rarely used today, appearing mainly with SELECT ... PROCEDURE ANALYSE or custom storage engine handlers. Any attempt to add ORDER BY to these statements triggers the error in MySQL 5.7, 8.0, and compatible MariaDB versions.
The primary trigger is the simultaneous use of the PROCEDURE clause and ORDER BY in one SELECT. MySQL treats PROCEDURE as an execution modifier that conflicts with server-side sorting. Legacy code migrated from MySQL 4.x often contains this pattern.
The error also appears when an ORM or code generator automatically appends ORDER BY to every query, including diagnostic PROCEDURE ANALYSE calls used during performance tuning.
Remove the ORDER BY clause from the SELECT ... PROCEDURE statement or move the ordering into an outer query. If you need sorted output, wrap the procedure call in a subquery and apply ORDER BY in the outer layer.
Alternatively, capture the procedure's output into a temporary table, then run a standard SELECT with ORDER BY against that table. This keeps logic clear and avoids the parser restriction.
Developers often run PROCEDURE ANALYSE to inspect column statistics and instinctively add ORDER BY to read results in a preferred order. Switching to a two-step query solves the issue without losing visibility.
Automated scripts may append ORDER BY id to every SELECT. Add conditional logic to omit the clause when your query string already contains PROCEDURE.
Keep diagnostic PROCEDURE calls separate from business logic queries. Use explicit wrapping subqueries for sorting. Validate generated SQL in CI pipelines to catch the pattern early.
Galaxy's linting engine flags unsupported clause combinations inside its editor, helping you reformat the query before it reaches production.
Error 1387 (ER_PROC_USED) surfaces when you attempt additional clauses that conflict with PROCEDURE. Error 1224 (ER_CANT_USE_OPTION_HERE) appears for other banned option combinations. The fixes follow the same pattern: separate the incompatible clauses or restructure the query.
Diagnostic queries that call ANALYSE often include ORDER BY for readability, triggering the error.
Some ORMs add ORDER BY on every SELECT, even those constructed for PROCEDURE calls.
Old code from MySQL 4.x may embed ORDER BY after PROCEDURE without validation in newer engines.
Raised when other forbidden clauses are combined with PROCEDURE.
Occurs when an option like GROUP BY appears where it is not allowed, requiring query refactor.
Appears when aggregate functions are used incorrectly in GROUP BY contexts.
No. MySQL prohibits the combination by design. You must separate the clauses using subqueries or temp tables.
Yes. MariaDB shares the same parser logic, so the error and fixes are identical.
Upgrading alone will not help. The restriction is intentional and persists in newer releases.
Galaxy's static SQL analysis flags the ORDER BY plus PROCEDURE pattern in real time and suggests wrapping the query, preventing runtime failures.