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
- Add an alias:
SELECT * FROM (SELECT 1) AS t;(theASis optional:(SELECT 1) t). - 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”.
Thanks — noted. This helps keep the database accurate.