Common SQL Errors

MySQL Error 1386: ER_ORDER_WITH_PROC - Can't Use ORDER Clause with PROCEDURE

Galaxy Team
August 7, 2025

<p>The SELECT ... PROCEDURE syntax in MySQL cannot be combined with an ORDER BY clause, triggering error 1386.</p>

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 1386 (ER_ORDER_WITH_PROC)?

<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>

Error Highlights

Typical Error Message

Can't use ORDER clause with this procedure

Error Type

Syntax Error

Language

MySQL

Symbol

ER_ORDER_WITH_PROC

Error Code

1386

SQL State

HY000

Explanation

Table of Contents

What is MySQL Error 1386 (ER_ORDER_WITH_PROC)?

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.

What Causes This Error?

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.

How to Fix MySQL Error 1386

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.

Common Scenarios and Solutions

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.

Best Practices to Avoid This Error

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.

Related Errors and Solutions

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.

Common Causes

ORDER BY Added to PROCEDURE ANALYSE

Diagnostic queries that call ANALYSE often include ORDER BY for readability, triggering the error.

ORM Auto-Appending ORDER BY

Some ORMs add ORDER BY on every SELECT, even those constructed for PROCEDURE calls.

Legacy Migration Scripts

Old code from MySQL 4.x may embed ORDER BY after PROCEDURE without validation in newer engines.

Related Errors

MySQL Error 1387: ER_PROC_USED

Raised when other forbidden clauses are combined with PROCEDURE.

MySQL Error 1224: ER_CANT_USE_OPTION_HERE

Occurs when an option like GROUP BY appears where it is not allowed, requiring query refactor.

MySQL Error 1111: ER_INVALID_GROUP_FUNC_USE

Appears when aggregate functions are used incorrectly in GROUP BY contexts.

FAQs

Can I ever mix ORDER BY and PROCEDURE in MySQL?

No. MySQL prohibits the combination by design. You must separate the clauses using subqueries or temp tables.

Does this error affect MariaDB?

Yes. MariaDB shares the same parser logic, so the error and fixes are identical.

Will upgrading MySQL fix the problem?

Upgrading alone will not help. The restriction is intentional and persists in newer releases.

How does Galaxy help avoid this error?

Galaxy's static SQL analysis flags the ORDER BY plus PROCEDURE pattern in real time and suggests wrapping the query, preventing runtime failures.

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