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

syntax_error subquery in FROM must have an alias — 42601

PostgreSQL error "subquery in FROM must have an alias" (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

A subquery used in the FROM clause was not given an alias. PostgreSQL requires every FROM-clause subquery (derived table) to be named, and raises SQLSTATE 42601 (syntax_error) otherwise.

  • The caret points just after the closing parenthesis of the subquery.
  • A hint usually suggests adding an alias: “For example, FROM (SELECT …) [AS] foo.”
  • Pure syntax error — no data is touched.

What the server log shows

ERROR:  subquery in FROM must have an alias
LINE 1: SELECT * FROM (SELECT 1);
                      ^
HINT:  For example, FROM (SELECT ...) [AS] foo.

Why PostgreSQL raises this — what the manual says

Section 7.2.1.3 Subqueries:

“According to the SQL standard, a table alias name must be supplied for a subquery.”

Each FROM item becomes a range-table entry that the rest of the query references by name. A derived table has no inherent name, so PostgreSQL requires an explicit alias; without it the parser cannot register the entry and fails with 42601.

Common causes

  • Forgetting the alias on a derived table.
  • SQL generated by a tool that omits the alias.

How to fix it

  1. Add an alias: SELECT * FROM (SELECT 1) AS t; (the AS is optional: (SELECT 1) t).
  2. Give column aliases too if you reference them: (SELECT 1 AS n) AS t.

Related & next steps

Reference: PostgreSQL 18 Section 7.2.1 “Table and Join Subqueries”.

Was this helpful?