so far as the
current query is concerned — it is not allowed to refer to the
table by the original name elsewhere in the query. Thus, this is not
valid:
<programlisting>
SELECT * FROM my_table AS m WHERE my_table.a > 5; -- wrong
</programlisting>
</para>
<para>
Table aliases are mainly for notational convenience, but it is
necessary to use them when joining a table to itself, e.g.:
<programlisting>
SELECT * FROM people AS mother JOIN people AS child ON mother.id = child.mother_id;
</programlisting>
</para>
<para>
Parentheses are used to resolve ambiguities. In the following example,
the first statement assigns the alias <literal>b</literal> to the second
instance of <literal>my_table</literal>, but the second statement assigns the
alias to the result of the join:
<programlisting>
SELECT * FROM my_table AS a CROSS JOIN my_table AS b ...
SELECT * FROM (my_table AS a CROSS JOIN my_table) AS b ...
</programlisting>
</para>
<para>
Another form of table aliasing gives temporary names to the columns of
the table, as well as the table itself:
<synopsis>
FROM <replaceable>table_reference</replaceable> <optional>AS</optional> <replaceable>alias</replaceable> ( <replaceable>column1</replaceable> <optional>, <replaceable>column2</replaceable> <optional>, ...</optional></optional> )
</synopsis>
If fewer column aliases are specified than the actual table has
columns, the remaining columns are not renamed. This syntax is
especially useful for self-joins or subqueries.
</para>
<para>
When an alias is applied to the output of a <literal>JOIN</literal>
clause, the alias hides the original
name(s) within the <literal>JOIN</literal>. For example:
<programlisting>
SELECT a.* FROM my_table AS a JOIN your_table AS b ON ...
</programlisting>
is valid SQL, but:
<programlisting>
SELECT a.* FROM (my_table AS a JOIN your_table AS b ON ...) AS c
</programlisting>
is not valid; the table alias <literal>a</literal> is not visible
outside the alias <literal>c</literal>.
</para>
</sect3>
<sect3 id="queries-subqueries">
<title>Subqueries</title>
<indexterm zone="queries-subqueries">
<primary>subquery</primary>
</indexterm>
<para>
Subqueries specifying a derived table must be enclosed in
parentheses. They may be assigned a table alias name, and optionally
column alias names (as in <xref linkend="queries-table-aliases"/>).
For example:
<programlisting>
FROM (SELECT * FROM table1) AS alias_name
</programlisting>
</para>
<para>
This example is equivalent to <literal>FROM table1 AS
alias_name</literal>. More interesting cases, which cannot be
reduced to a plain join, arise when the subquery involves
grouping or aggregation.
</para>
<para>
A subquery can also be a <command>VALUES</command> list:
<programlisting>
FROM (VALUES ('anne', 'smith'), ('bob', 'jones'), ('joe', 'blow'))
AS names(first, last)
</programlisting>
Again, a table alias is optional. Assigning alias names to the columns
of the <command>VALUES</command> list is optional, but is good practice.
For more information see <xref linkend="queries-values"/>.
</para>
<para>
According to the SQL standard, a table alias name must be supplied
for a subquery. <productname>PostgreSQL</productname>
allows <literal>AS</literal> and the alias to be omitted, but
writing one is good practice in SQL code that might be ported to
another system.
</para>
</sect3>
<sect3 id="queries-tablefunctions">
<title>Table Functions</title>
<indexterm zone="queries-tablefunctions"><primary>table function</primary></indexterm>
<indexterm zone="queries-tablefunctions">
<primary>function</primary>
<secondary>in the FROM clause</secondary>
</indexterm>
<para>
Table functions are functions