SQLSTATE 42P10 ERROR Class 42: Syntax Error or Access Rule Violation

invalid_column_reference ORDER BY position … is not in select list — 42P10

PostgreSQL error "ORDER BY position … is not in select list" (SQLSTATE 42P10): what it means, common causes, and how to fix it.

PG 12, 13, 14, 15, 16, 17, 18 Official docs
Last reviewed May 2025 Grounded in source

Diagnostic Queries

Symptoms

An ORDER BY used a positional number outside the range of the select list. PostgreSQL raises SQLSTATE 42P10 (invalid_column_reference).

  • ORDER BY n where n exceeds the number of output columns.
  • Positional ordering references the SELECT list by index.
  • Common after changing the select list.

What the server log shows

ERROR:  ORDER BY position 4 is not in select list

Why PostgreSQL raises this — what the manual says

Section 7.5 Sorting Rows (ORDER BY):

“A sort_expression can also be the column label or number of an output column”

Positional ORDER BY refers to the n-th select-list item. When n exceeds the number of output columns, there is no column at that position and PostgreSQL reports 42P10.

Common causes

  • A position number larger than the select-list length.
  • The select list shrank but ORDER BY positions did not.
  • Off-by-one ordinal references.

How to fix it

  1. Use a position within the valid range.
  2. Order by the column name/expression instead of a number.
  3. Recount the select-list items after edits.

Related & next steps

Reference: PostgreSQL 18 Section 7.5 “Sorting Rows”.

Was this helpful?