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

syntax_error each UNION query must have the same number of columns — 42601

PostgreSQL error "each UNION query must have the same number of columns" (SQLSTATE 42601): 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

The branches of a UNION (or INTERSECT/EXCEPT) returned different numbers of columns. Set operations require matching column counts, so PostgreSQL raises SQLSTATE 42601 (syntax_error).

  • Two SELECTs in a UNION have unequal column counts.
  • Common after adding/removing a column from one branch.
  • Applies to UNION, INTERSECT, and EXCEPT.

What the server log shows

ERROR:  each UNION query must have the same number of columns

Why PostgreSQL raises this — what the manual says

Section 7.4 Combining Queries (UNION, INTERSECT, EXCEPT):

“which means that they return the same number of columns and the corresponding columns have compatible data types, as described in Section 10.5.”

Set operations align results column-by-column, so each branch must produce the same number of columns of compatible types. A mismatch in count makes alignment impossible and PostgreSQL reports 42601.

Common causes

  • One SELECT lists more/fewer columns than the other.
  • SELECT * on tables with different column counts.
  • An edit to one branch that wasn’t mirrored in the other.

How to fix it

  1. Make every branch select the same number of columns in the same order.
  2. Avoid SELECT * in set operations; list columns explicitly.
  3. Add placeholder/NULL columns to align counts when intentional.

Related & next steps

Reference: PostgreSQL 18 Section 7.4 “Combining Queries”.

Was this helpful?