11<!--
2- $PostgreSQL: pgsql/doc/src/sgml/ref/select.sgml,v 1.102 2007/11/28 15:42:31 petere Exp $
2+ $PostgreSQL: pgsql/doc/src/sgml/ref/select.sgml,v 1.103 2008/02/15 22:17:06 tgl Exp $
33PostgreSQL documentation
44-->
55
@@ -21,7 +21,7 @@ PostgreSQL documentation
2121 <refsynopsisdiv>
2222<synopsis>
2323SELECT [ ALL | DISTINCT [ ON ( <replaceable class="parameter">expression</replaceable> [, ...] ) ] ]
24- * | <replaceable class="parameter">expression</replaceable> [ AS <replaceable class="parameter">output_name</replaceable> ] [, ...]
24+ * | <replaceable class="parameter">expression</replaceable> [ [ AS ] <replaceable class="parameter">output_name</replaceable> ] [, ...]
2525 [ FROM <replaceable class="parameter">from_item</replaceable> [, ...] ]
2626 [ WHERE <replaceable class="parameter">condition</replaceable> ]
2727 [ GROUP BY <replaceable class="parameter">expression</replaceable> [, ...] ]
@@ -477,23 +477,45 @@ HAVING <replaceable class="parameter">condition</replaceable>
477477 <literal>SELECT</> and <literal>FROM</>) specifies expressions
478478 that form the output rows of the <command>SELECT</command>
479479 statement. The expressions can (and usually do) refer to columns
480- computed in the <literal>FROM</> clause. Using the clause
481- <literal>AS <replaceable
482- class="parameter">output_name</replaceable></literal>, another
483- name can be specified for an output column. This name is
484- primarily used to label the column for display. It can also be
485- used to refer to the column's value in <literal>ORDER BY</> and
486- <literal>GROUP BY</> clauses, but not in the <literal>WHERE</> or
487- <literal>HAVING</> clauses; there you must write out the
488- expression instead.
480+ computed in the <literal>FROM</> clause.
481+ </para>
482+
483+ <para>
484+ Just as in a table, every output column of a <command>SELECT</command>
485+ has a name. In a simple <command>SELECT</command> this name is just
486+ used to label the column for display, but when the <command>SELECT</>
487+ is a sub-query of a larger query, the name is seen by the larger query
488+ as the column name of the virtual table produced by the sub-query.
489+ To specify the name to use for an output column, write
490+ <literal>AS</> <replaceable class="parameter">output_name</replaceable>
491+ after the column's expression. (You can omit <literal>AS</literal>,
492+ but only if the desired output name does not match any
493+ <productname>PostgreSQL</productname> keyword (see <xref
494+ linkend="sql-keywords-appendix">). For protection against possible
495+ future keyword additions, it is recommended that you always either
496+ write <literal>AS</literal> or double-quote the output name.)
497+ If you do not specify a column name, a name is chosen automatically
498+ by <productname>PostgreSQL</productname>. If the column's expression
499+ is a simple column reference then the chosen name is the same as that
500+ column's name; in more complex cases a generated name looking like
501+ <literal>?column<replaceable>N</>?</literal> is usually chosen.
502+ </para>
503+
504+ <para>
505+ An output column's name can be used to refer to the column's value in
506+ <literal>ORDER BY</> and <literal>GROUP BY</> clauses, but not in the
507+ <literal>WHERE</> or <literal>HAVING</> clauses; there you must write
508+ out the expression instead.
489509 </para>
490510
491511 <para>
492512 Instead of an expression, <literal>*</literal> can be written in
493513 the output list as a shorthand for all the columns of the selected
494- rows. Also, one can write <literal><replaceable
514+ rows. Also, you can write <literal><replaceable
495515 class="parameter">table_name</replaceable>.*</literal> as a
496- shorthand for the columns coming from just that table.
516+ shorthand for the columns coming from just that table. In these
517+ cases it is not possible to specify new names with <literal>AS</>;
518+ the output column names will be the same as the table columns' names.
497519 </para>
498520 </refsect2>
499521
@@ -661,17 +683,17 @@ ORDER BY <replaceable class="parameter">expression</replaceable> [ ASC | DESC |
661683
662684 <para>
663685 The ordinal number refers to the ordinal (left-to-right) position
664- of the result column. This feature makes it possible to define an
686+ of the output column. This feature makes it possible to define an
665687 ordering on the basis of a column that does not have a unique
666688 name. This is never absolutely necessary because it is always
667- possible to assign a name to a result column using the
689+ possible to assign a name to an output column using the
668690 <literal>AS</> clause.
669691 </para>
670692
671693 <para>
672694 It is also possible to use arbitrary expressions in the
673695 <literal>ORDER BY</literal> clause, including columns that do not
674- appear in the <command>SELECT</command> result list. Thus the
696+ appear in the <command>SELECT</command> output list. Thus the
675697 following statement is valid:
676698<programlisting>
677699SELECT name FROM distributors ORDER BY code;
@@ -684,8 +706,8 @@ SELECT name FROM distributors ORDER BY code;
684706
685707 <para>
686708 If an <literal>ORDER BY</> expression is a simple name that
687- matches both a result column name and an input column name,
688- <literal>ORDER BY</> will interpret it as the result column name.
709+ matches both an output column name and an input column name,
710+ <literal>ORDER BY</> will interpret it as the output column name.
689711 This is the opposite of the choice that <literal>GROUP BY</> will
690712 make in the same situation. This inconsistency is made to be
691713 compatible with the SQL standard.
@@ -1135,16 +1157,25 @@ SELECT distributors.* WHERE distributors.name = 'Westward';
11351157 </refsect2>
11361158
11371159 <refsect2>
1138- <title>The <literal>AS</literal> Key Word</title>
1160+ <title>Omitting the <literal>AS</literal> Key Word</title>
1161+
1162+ <para>
1163+ In the SQL standard, the optional key word <literal>AS</> can be
1164+ omitted before an output column name whenever the new column name
1165+ is a valid column name (that is, not the same as any reserved
1166+ keyword). <productname>PostgreSQL</productname> is slightly more
1167+ restrictive: <literal>AS</> is required if the new column name
1168+ matches any keyword at all, reserved or not. Recommended practice is
1169+ to use <literal>AS</> or double-quote output column names, to prevent
1170+ any possible conflict against future keyword additions.
1171+ </para>
11391172
11401173 <para>
1141- In the SQL standard, the optional key word <literal>AS</> is just
1142- noise and can be omitted without affecting the meaning. The
1143- <productname>PostgreSQL</productname> parser requires this key
1144- word when renaming output columns because the type extensibility
1145- features lead to parsing ambiguities without it.
1146- <literal>AS</literal> is optional in <literal>FROM</literal>
1147- items, however.
1174+ In <literal>FROM</literal> items, both the standard and
1175+ <productname>PostgreSQL</productname> allow <literal>AS</> to
1176+ be omitted before an alias that is an unreserved keyword. But
1177+ this is impractical for output column names, because of syntactic
1178+ ambiguities.
11481179 </para>
11491180 </refsect2>
11501181
@@ -1153,15 +1184,15 @@ SELECT distributors.* WHERE distributors.name = 'Westward';
11531184
11541185 <para>
11551186 In the SQL-92 standard, an <literal>ORDER BY</literal> clause can
1156- only use result column names or numbers, while a <literal>GROUP
1187+ only use output column names or numbers, while a <literal>GROUP
11571188 BY</literal> clause can only use expressions based on input column
11581189 names. <productname>PostgreSQL</productname> extends each of
11591190 these clauses to allow the other choice as well (but it uses the
11601191 standard's interpretation if there is ambiguity).
11611192 <productname>PostgreSQL</productname> also allows both clauses to
11621193 specify arbitrary expressions. Note that names appearing in an
11631194 expression will always be taken as input-column names, not as
1164- result -column names.
1195+ output -column names.
11651196 </para>
11661197
11671198 <para>
0 commit comments