SQLSTATE 42P10Severity lowLab verified

Incident brief

ORDER BY position out of range

ORDER BY and GROUP BY can reference output columns by position, but the position must be within the select list; a position beyond the number of selected columns raises this error.

Reproduced on PostgreSQL 18.4Verified 2026-07-24 (isolated lab, PostgreSQL 18.4)Reviewed by Verified against PostgreSQL 18.4 in an isolated lab environment

In 10 seconds

What
ORDER BY position out of range
What triggers it
Write ORDER BY <n> where <n> is larger than the number of columns in the select list.
The fix
Use a position within the select list, or order by the column name or expression directly.
Proof
Reproduced on PostgreSQL 18.4 → A single query reproduces SQLSTATE 42P10; the out-of-range ORDER BY position is rejected and the LINE points at it.

The fix

What to do right now

The immediate, application-level response to this error.

  • Use a position within the select list, or order by the column name or expression directly.
  • Recount the select list after edits so positional references stay valid.
Fix — SQL
-- Reference a position that exists in the select list.
SELECT label FROM ref_demo ORDER BY 1;

Related & next steps

Follow the thread

Everything this error touches — jump straight to the sibling error, term, runbook, or parameter.

Verification

PG 18.4
Last verified
2026-07-24 (isolated lab, PostgreSQL 18.4)
Reviewed by
Verified against PostgreSQL 18.4 in an isolated lab environment
Audit status
reviewed