TATECHATLAS
◎ English
Data & databases / Tip

PostgreSQL table aliases in FROM: naming, shadowing, and derived tables

Table aliases rename a table reference for the rest of the query, which makes self-joins possible but forbids using the original table name afterward. Derived tables such as subqueries and VALUES lists follow the same naming rules.

On this page

In PostgreSQL, an alias defined in FROM becomes the only name by which that table reference can be addressed for the rest of the query. After FROM my_table AS m, writing my_table.a in WHERE, GROUP BY, HAVING, or the SELECT list is an error; you must use m.a. Aliases are optional convenience for base tables but required in practice for self-joins, where the same table appears twice under different names. Derived tables, meaning parenthesized subqueries and VALUES lists in FROM, are named the same way; the SQL standard requires an alias for a subquery, and PostgreSQL allows omitting it but writing one is good practice for portability.

Using Table Aliases in FROM

A table alias is a temporary name attached to a table reference in the FROM clause. You write either FROM table_reference AS alias or FROM table_reference alias; the AS keyword is optional noise, and the alias can be any identifier. Aliases serve two purposes. First, they are a notational convenience: short identifiers keep join clauses readable when table names are long, as in FROM some_very_long_table_name s JOIN another_fairly_long_name a ON s.id = a.num. Second, they are mandatory for joining a table to itself, because two references to the same table need distinct names to be distinguishable.

The alias applies to any table reference, not just a plain table name. A JOIN construct or a parenthesized combination can carry an alias too, and when a query nests several joins, parentheses resolve ambiguities about which FROM item an alias belongs to. In the documented example, the first statement assigns the alias b to the second instance of a table while the second statement assigns it to the whole join result, which changes what b refers to in the rest of the query.

SELECT * FROM people AS mother JOIN people AS child ON mother.id = child.mother_id;

Working with Derived Tables

A derived table is a subquery or a VALUES list placed in the FROM clause and treated as a table. The subquery is written in parentheses and should be given a table alias and, where needed, column aliases. Derived tables become genuinely useful when the inner query involves grouping or aggregation, since such results cannot be expressed as a plain join against a base table. A VALUES list works the same way: FROM (VALUES ('anne', 'smith'), ('bob', 'jones')) AS names(first, last) both names the derived table and labels its columns, which is optional but good practice.

SELECT names.first, names.last FROM (VALUES ('anne', 'smith'), ('bob', 'jones')) AS names(first, last);

The Alias Shadowing Effect

The alias becomes the new name of the table reference for the current query. Once it is defined, referring to the table by its original name elsewhere in the query is not allowed. The documentation gives a direct counterexample: SELECT * FROM my_table AS m WHERE my_table.a > 5 is invalid, because after FROM my_table AS m the only legal way to qualify columns of that reference is m.a. The same restriction holds in the SELECT list, WHERE, GROUP BY, and HAVING clauses of that query.

This is a naming-scope rule, not a performance or planning issue. The parser resolves column qualifications against the names established in the FROM clause, and the original name simply is not among them anymore. The error you get is an unresolved reference, and the fix is to qualify columns with the alias consistently. Note that this rule concerns the same query level: an outer query's column naming scope extends into its subqueries, so a subquery can reference the outer derived table by its alias, as in the documented fdt.c1 examples.

-- invalid: the original name is gone once the alias is defined SELECT * FROM my_table AS m WHERE my_table.a > 5; -- valid: qualify with the alias SELECT * FROM my_table AS m WHERE m.a > 5;

Resolving Ambiguities and Scope

In practice, pick one naming convention per query and stick to it. If you alias a table, qualify every reference to its columns with that alias, even where the column name would be unambiguous; the documentation notes that qualifying a column name adds clarity even when it is not strictly needed. For self-joins, choose semantically meaningful aliases such as mother and child rather than t1 and t2, because the join condition then documents itself. For derived tables, always supply an alias plus column aliases for VALUES lists as an optional readability and portability measure. The one case where column definitions are genuinely required is a function declared to return record: since its column set is unknown, the parser needs the column names to know what * should expand to and how to plan the query, as with dblink('dbname=mydb', 'SELECT ...') AS t1(proname name, prosrc text).

SELECT s.id, a.num FROM some_very_long_table_name s JOIN another_fairly_long_name a ON s.id = a.num;

Applicability

  • Does any clause in the query still qualify columns with the original table name after an alias was assigned?
  • Is the self-join using two distinct, meaningful aliases for the two references to the same table?
  • Does every subquery and VALUES list in FROM carry an explicit table alias, and column aliases where column names are needed?
  • Are parentheses placed so that each alias attaches to the intended FROM item in nested joins?

The behavior described here follows the PostgreSQL 18 documentation on table expressions; the permissiveness of omitting a subquery alias is a PostgreSQL extension and is not portable to other SQL systems.

Sources

  1. PostgreSQL: table expressions ↗
  2. PostgreSQL: constraints ↗
  3. PostgreSQL: aggregate functions ↗
Back to top ↑