How to Fix MySQL Error 3065 (ER_FIELD_IN_ORDER_NOT_SELECT)
You get mysql error 3065 when a distinct query sorts by an unselected column. You fix it by adding the sort column to your select list. Database engines enforce strict rules to make sure query results stay predictable. When your app sends a broken sort request, the database blocks the query. It returns state HY000 and stops the task right away. Understanding why the engine rejects this query helps your team write better logic. Why mysql error 3065 happens in your database The database tracks
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 get mysql error 3065 when a distinct query sorts by an unselected column. You fix it by adding the sort column to your select list.
Database engines enforce strict rules to make sure query results stay predictable. When your app sends a broken sort request, the database blocks the query. It returns state HY000 and stops the task right away. Understanding why the engine rejects this query helps your team write better logic.
Why mysql error 3065 happens in your database
The database tracks this failure under the official symbol ER_FIELD_IN_ORDER_NOT_SELECT. The error message states that an order expression is not in the select list. It points out the exact column name that caused the crash.
This problem occurs when you combine distinct results with a sort clause. First, the engine grabs the columns you asked for in the query. Next, it removes duplicate rows so every remaining row is unique. Finally, it tries to sort those unique rows using your order column.
If the sort column is missing from the select list, the engine gets stuck. Multiple raw rows might match one unique row in your final output. Each of those raw rows can hold a different value in the sort column. The database does not know which hidden value to use for sorting.
Strict SQL mode catches this math flaw before the query runs. Older database setups sometimes guessed a random value and returned mixed results. Modern setups block the query so your app never reads bad data.
How to find the broken query in production
Your first step is finding which service or user ran the failing query. Check your app error logs for state HY000 and code 3065. The database message gives you the column name and the expression number.
Look at your code base for queries that sort by that specific column. Object relational mappers often build these bad queries behind the scenes. A developer might ask for unique records while a default sort rule stays active. The tool then joins both rules into one broken statement.
Just-in-Time Access + MySQL Access Governance: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
When you check production data, keep strict access controls in place. Read-only roles let engineers inspect views and query plans without risking live data. If your tables hold sensitive data, data masking rewrites matched fields in results. Masking can rewrite emails, phone numbers, card numbers, or national IDs during checks.
How to fix the query without lowering safety checks
Many forum posts tell you to turn off strict mode in the database. Do not do this in a live system. Turning off safety rules hides bugs and leads to random sort orders. Instead, fix the query logic in your app code.
The easiest fix is adding the sort column to your select list. Once the column is in the list, the engine knows how to sort it. Your app can ignore the extra column if the screen does not need it. Keep in mind that adding a column can change which rows are unique.
If your output must stay unique without that extra column, use a group rule. Group the rows by your selected columns first. Then sort the grouped rows using a summary value like minimum or maximum. For example, sort by the highest date value linked to each unique user. This gives the engine a clear, single value for every row.
You can also use a subquery to split the work into two steps. Have the inner query sort and filter the raw table first. Then have the outer query pick the unique values you need. Test the new query on a staging database before you push it live.
Check MySQL queries and results on the wire
MySQL grants control who logs in. They do not check single queries or mask sensitive columns once a session starts. With hoop.dev, you run an open-source proxy, a sidecar for your MySQL, inline in the MySQL wire protocol. On every query an AI agent or human runs against production MySQL, RDS, or Aurora, hoop.dev decides to pass, mask, block, or hold it for review.
Multi-statement batches and prepared queries are split and checked one by one by hoop.dev guardrails, which refuse banned queries with a native MySQL error or hold risky ones for approval. On the response path, hoop.dev data masking rewrites string values in returned rows in memory, and every decision is logged. See the MIT-licensed project on GitHub.
Frequently asked questions
What does ER_FIELD_IN_ORDER_NOT_SELECT mean in plain terms?
It means your query asks for unique rows but sorts by a hidden column. The database cannot tell which hidden row value should set the final sort order.
Should I change my database SQL mode to stop this error?
No, you should keep strict SQL rules active in production. Turning off the rule causes the database to pick random sort values and return bad results.
Why did this error start after a database upgrade?
Newer database versions turn on strict sort rules by default. Your old database ran the same query by guessing the sort order without telling you.
Related guides
In this post