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

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

PostgreSQL error "GROUP 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

A GROUP BY used a positional number that does not correspond to a column in the select list. PostgreSQL raises SQLSTATE 42P10 (invalid_column_reference).

  • GROUP BY n where n exceeds the number of output columns.
  • Positional grouping references the SELECT list by index.
  • Common after editing the select list.

What the server log shows

ERROR:  GROUP BY position 5 is not in select list

Why PostgreSQL raises this — what the manual says

As Section 7.2.3 The GROUP BY and HAVING Clauses explains:

A GROUP BY item written as a positional number refers to the ordinal position of an output column in the select list; a number that does not correspond to any select-list column (for example GROUP BY 5 when only three columns are selected) is rejected.

Positional GROUP BY refers to the n-th item in the select list. When n is larger than the number of output columns (or otherwise invalid), there is no column to group by and PostgreSQL reports 42P10.

Common causes

  • A position number greater than the count of select-list items.
  • The select list changed but the positions did not.
  • Off-by-one positional references.

How to fix it

  1. Use a valid position within the select list range.
  2. Group 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.2 “Table Expressions”.

Was this helpful?