<synopsis>
FROM <replaceable>table_reference</replaceable> <replaceable>alias</replaceable>
</synopsis>
- The <literal>AS</literal> key word is noise.
+ The <literal>AS</literal> key word is optional noise.
<replaceable>alias</replaceable> can be any identifier.
</para>
</para>
<para>
- If no output column name is specified using <literal>AS</>, the system assigns a
- default name. For simple column references, this is the name of the
- referenced column. For function
+ If no output column name is specified using <literal>AS</>,
+ the system assigns a default column name. For simple column references,
+ this is the name of the referenced column. For function
calls, this is the name of the function. For complex expressions,
the system will generate a generic name.
</para>
+ <para>
+ The <literal>AS</> keyword is optional, but only if the new column
+ name does not match any
+ <productname>PostgreSQL</productname> keyword (see <xref
+ linkend="sql-keywords-appendix">). To avoid an accidental match to
+ a keyword, you can double-quote the column name. For example,
+ <literal>VALUE</> is a keyword, so this does not work:
+<programlisting>
+SELECT a value, b + c AS sum FROM ...
+</programlisting>
+ but this does:
+<programlisting>
+SELECT a "value", b + c AS sum FROM ...
+</programlisting>
+ For protection against possible
+ future keyword additions, it is recommended that you always either
+ write <literal>AS</literal> or double-quote the output column name.
+ </para>
+
<note>
<para>
The naming of output columns here is different from that done in
<refsynopsisdiv>
<synopsis>
SELECT [ ALL | DISTINCT [ ON ( <replaceable class="parameter">expression</replaceable> [, ...] ) ] ]
- * | <replaceable class="parameter">expression</replaceable> [ AS <replaceable class="parameter">output_name</replaceable> ] [, ...]
+ * | <replaceable class="parameter">expression</replaceable> [ [ AS ] <replaceable class="parameter">output_name</replaceable> ] [, ...]
[ FROM <replaceable class="parameter">from_item</replaceable> [, ...] ]
[ WHERE <replaceable class="parameter">condition</replaceable> ]
[ GROUP BY <replaceable class="parameter">expression</replaceable> [, ...] ]
<literal>SELECT</> and <literal>FROM</>) specifies expressions
that form the output rows of the <command>SELECT</command>
statement. The expressions can (and usually do) refer to columns
- computed in the <literal>FROM</> clause. Using the clause
- <literal>AS <replaceable
- class="parameter">output_name</replaceable></literal>, another
- name can be specified for an output column. This name is
- primarily used to label the column for display. It can also be
- used to refer to the column's value in <literal>ORDER BY</> and
- <literal>GROUP BY</> clauses, but not in the <literal>WHERE</> or
- <literal>HAVING</> clauses; there you must write out the
- expression instead.
+ computed in the <literal>FROM</> clause.
+ </para>
+
+ <para>
+ Just as in a table, every output column of a <command>SELECT</command>
+ has a name. In a simple <command>SELECT</command> this name is just
+ used to label the column for display, but when the <command>SELECT</>
+ is a sub-query of a larger query, the name is seen by the larger query
+ as the column name of the virtual table produced by the sub-query.
+ To specify the name to use for an output column, write
+ <literal>AS</> <replaceable class="parameter">output_name</replaceable>
+ after the column's expression. (You can omit <literal>AS</literal>,
+ but only if the desired output name does not match any
+ <productname>PostgreSQL</productname> keyword (see <xref
+ linkend="sql-keywords-appendix">). For protection against possible
+ future keyword additions, it is recommended that you always either
+ write <literal>AS</literal> or double-quote the output name.)
+ If you do not specify a column name, a name is chosen automatically
+ by <productname>PostgreSQL</productname>. If the column's expression
+ is a simple column reference then the chosen name is the same as that
+ column's name; in more complex cases a generated name looking like
+ <literal>?column<replaceable>N</>?</literal> is usually chosen.
+ </para>
+
+ <para>
+ An output column's name can be used to refer to the column's value in
+ <literal>ORDER BY</> and <literal>GROUP BY</> clauses, but not in the
+ <literal>WHERE</> or <literal>HAVING</> clauses; there you must write
+ out the expression instead.
</para>
<para>
Instead of an expression, <literal>*</literal> can be written in
the output list as a shorthand for all the columns of the selected
- rows. Also, one can write <literal><replaceable
+ rows. Also, you can write <literal><replaceable
class="parameter">table_name</replaceable>.*</literal> as a
- shorthand for the columns coming from just that table.
+ shorthand for the columns coming from just that table. In these
+ cases it is not possible to specify new names with <literal>AS</>;
+ the output column names will be the same as the table columns' names.
</para>
</refsect2>
<para>
The ordinal number refers to the ordinal (left-to-right) position
- of the result column. This feature makes it possible to define an
+ of the output column. This feature makes it possible to define an
ordering on the basis of a column that does not have a unique
name. This is never absolutely necessary because it is always
- possible to assign a name to a result column using the
+ possible to assign a name to an output column using the
<literal>AS</> clause.
</para>
<para>
It is also possible to use arbitrary expressions in the
<literal>ORDER BY</literal> clause, including columns that do not
- appear in the <command>SELECT</command> result list. Thus the
+ appear in the <command>SELECT</command> output list. Thus the
following statement is valid:
<programlisting>
SELECT name FROM distributors ORDER BY code;
<para>
If an <literal>ORDER BY</> expression is a simple name that
- matches both a result column name and an input column name,
- <literal>ORDER BY</> will interpret it as the result column name.
+ matches both an output column name and an input column name,
+ <literal>ORDER BY</> will interpret it as the output column name.
This is the opposite of the choice that <literal>GROUP BY</> will
make in the same situation. This inconsistency is made to be
compatible with the SQL standard.
</refsect2>
<refsect2>
- <title>The <literal>AS</literal> Key Word</title>
+ <title>Omitting the <literal>AS</literal> Key Word</title>
+
+ <para>
+ In the SQL standard, the optional key word <literal>AS</> can be
+ omitted before an output column name whenever the new column name
+ is a valid column name (that is, not the same as any reserved
+ keyword). <productname>PostgreSQL</productname> is slightly more
+ restrictive: <literal>AS</> is required if the new column name
+ matches any keyword at all, reserved or not. Recommended practice is
+ to use <literal>AS</> or double-quote output column names, to prevent
+ any possible conflict against future keyword additions.
+ </para>
<para>
- In the SQL standard, the optional key word <literal>AS</> is just
- noise and can be omitted without affecting the meaning. The
- <productname>PostgreSQL</productname> parser requires this key
- word when renaming output columns because the type extensibility
- features lead to parsing ambiguities without it.
- <literal>AS</literal> is optional in <literal>FROM</literal>
- items, however.
+ In <literal>FROM</literal> items, both the standard and
+ <productname>PostgreSQL</productname> allow <literal>AS</> to
+ be omitted before an alias that is an unreserved keyword. But
+ this is impractical for output column names, because of syntactic
+ ambiguities.
</para>
</refsect2>
<para>
In the SQL-92 standard, an <literal>ORDER BY</literal> clause can
- only use result column names or numbers, while a <literal>GROUP
+ only use output column names or numbers, while a <literal>GROUP
BY</literal> clause can only use expressions based on input column
names. <productname>PostgreSQL</productname> extends each of
these clauses to allow the other choice as well (but it uses the
<productname>PostgreSQL</productname> also allows both clauses to
specify arbitrary expressions. Note that names appearing in an
expression will always be taken as input-column names, not as
- result-column names.
+ output-column names.
</para>
<para>