Common SQL Errors

MySQL Error 3065 ER_FIELD_IN_ORDER_NOT_SELECT: Fix “Expression # of ORDER BY is not in SELECT”

Galaxy Team
August 8, 2025

MySQL throws error 3065 when ONLY_FULL_GROUP_BY or DISTINCT requires every ORDER BY column to appear in the SELECT list.

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 3065 ER_FIELD_IN_ORDER_NOT_SELECT?

MySQL error 3065 ER_FIELD_IN_ORDER_NOT_SELECT occurs when ONLY_FULL_GROUP_BY or DISTINCT is active and an ORDER BY column is missing from SELECT. Add the column to SELECT, wrap it in an aggregate, or disable ONLY_FULL_GROUP_BY to resolve the issue.

Error Highlights

Typical Error Message

ER_FIELD_IN_ORDER_NOT_SELECT

Error Type

Query Syntax Error

Language

MySQL

Symbol

references column '%s' which is not in SELECT list; this is incompatible with %s ER_FIELD_IN_ORDER_NOT_SELECT was added in 5.7.5.

Error Code

3065

SQL State

HY000

Explanation

Table of Contents

What is MySQL error 3065 ER_FIELD_IN_ORDER_NOT_SELECT?

MySQL raises error 3065 with message “Expression # of ORDER BY clause is not in SELECT list” when ONLY_FULL_GROUP_BY or DISTINCT is on and a query orders by a column that is not returned in the SELECT list or wrapped in an aggregate function.

The rule, introduced in MySQL 5.7.5, enforces deterministic results by ensuring every nonaggregated column used for sorting or filtering is functionally dependent on the grouped columns.

What Causes This Error?

ONLY_FULL_GROUP_BY SQL mode is enabled by default in MySQL 5.7 and later. The mode requires all nonaggregated columns in ORDER BY or HAVING to appear in the SELECT clause.

Queries using DISTINCT also trigger the check: ORDER BY expressions must exactly match the projection list to prevent undefined ordering after duplicate removal.

An alias removed during query rewrite or a view that hides the column can inadvertently cause the error even when the original SQL looked correct.

How to Fix MySQL error 3065

Add the missing column to the SELECT list so that ORDER BY references a selected expression.

Wrap the column in an aggregate such as MIN() or MAX() when you need a representative value per group.

Disable ONLY_FULL_GROUP_BY at session level with SET sql_mode = (SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY','')) when backward-compatibility outweighs strict correctness.

Common Scenarios and Solutions

Aggregated reports: include the grouped column in SELECT, then ORDER BY it to rank groups.

DISTINCT user lists sorted by created_at: SELECT DISTINCT user_id, created_at FROM events ORDER BY user_id, created_at.

Subquery pattern: SELECT * FROM (SELECT id, MAX(score) AS top FROM tests GROUP BY id) t ORDER BY top DESC;

Best Practices to Avoid This Error

Always write explicit column lists instead of SELECT * so missing columns are obvious.

Validate queries in Galaxy’s editor. The linter highlights ORDER BY columns absent from SELECT, preventing runtime errors.

Add automated tests that run in a staging database with ONLY_FULL_GROUP_BY enabled to catch violations early.

Related Errors and Solutions

Error 1055 (ONLY_FULL_GROUP_BY) occurs when nonaggregated columns in SELECT are not in GROUP BY. Fix by adding columns to GROUP BY or aggregating them.

Error 1221 (Incorrect usage of UNION and ORDER BY) happens when ORDER BY appears before the last SELECT in a UNION chain. Move ORDER BY to the end of the unioned query.

Error 3064 (DISTINCT and ORDER BY) appears when ORDER BY refers to a non-selected blob/text column in a DISTINCT query. Include the column or drop DISTINCT.

Common Causes

ONLY_FULL_GROUP_BY mode enabled

This mode forces every nonaggregated column referenced in ORDER BY or HAVING to be listed in SELECT, triggering error 3065 when the rule is violated.

DISTINCT with mismatched ORDER BY

When DISTINCT removes duplicates, MySQL must sort by the exact same columns; any additional column in ORDER BY throws the error.

Column hidden by view or subquery

If a view or derived table omits a column later used for ordering, the outer query fails with ER_FIELD_IN_ORDER_NOT_SELECT.

Related Errors

Error 1055 ONLY_FULL_GROUP_BY

Occurs when nonaggregated columns appear in SELECT but not in GROUP BY; resolved by grouping or aggregating.

Error 1292 Truncated incorrect value

Triggered by invalid data type conversions; fix by adjusting data formats or column types.

Error 1264 Out of range value

Raised when inserted data exceeds column limits; solve by widening column definitions or validating input.

FAQs

Can I ignore error 3065 safely?

Ignoring it by disabling ONLY_FULL_GROUP_BY may cause nondeterministic result sets. Use that approach only for quick debugging, not in production.

Does error 3065 affect performance?

The error prevents execution, so there is no performance cost. Correcting the query usually has minimal impact on execution time.

Is SELECT * always unsafe with ONLY_FULL_GROUP_BY?

SELECT * is safe if every referenced column is part of GROUP BY or aggregated. However, explicit lists reduce the risk of errors.

How does Galaxy help avoid this error?

Galaxy’s real-time linter flags ORDER BY columns missing from SELECT, and the AI copilot suggests compliant rewrites before the query reaches MySQL.

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