1
1
<!--
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 $
3
3
PostgreSQL documentation
4
4
-->
5
5
@@ -21,7 +21,7 @@ PostgreSQL documentation
21
21
<refsynopsisdiv>
22
22
<synopsis>
23
23
SELECT [ 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> ] [, ...]
25
25
[ FROM <replaceable class="parameter">from_item</replaceable> [, ...] ]
26
26
[ WHERE <replaceable class="parameter">condition</replaceable> ]
27
27
[ GROUP BY <replaceable class="parameter">expression</replaceable> [, ...] ]
@@ -477,23 +477,45 @@ HAVING <replaceable class="parameter">condition</replaceable>
477
477
<literal>SELECT</> and <literal>FROM</>) specifies expressions
478
478
that form the output rows of the <command>SELECT</command>
479
479
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.
489
509
</para>
490
510
491
511
<para>
492
512
Instead of an expression, <literal>*</literal> can be written in
493
513
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
495
515
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.
497
519
</para>
498
520
</refsect2>
499
521
@@ -661,17 +683,17 @@ ORDER BY <replaceable class="parameter">expression</replaceable> [ ASC | DESC |
661
683
662
684
<para>
663
685
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
665
687
ordering on the basis of a column that does not have a unique
666
688
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
668
690
<literal>AS</> clause.
669
691
</para>
670
692
671
693
<para>
672
694
It is also possible to use arbitrary expressions in the
673
695
<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
675
697
following statement is valid:
676
698
<programlisting>
677
699
SELECT name FROM distributors ORDER BY code;
@@ -684,8 +706,8 @@ SELECT name FROM distributors ORDER BY code;
684
706
685
707
<para>
686
708
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.
689
711
This is the opposite of the choice that <literal>GROUP BY</> will
690
712
make in the same situation. This inconsistency is made to be
691
713
compatible with the SQL standard.
@@ -1135,16 +1157,25 @@ SELECT distributors.* WHERE distributors.name = 'Westward';
1135
1157
</refsect2>
1136
1158
1137
1159
<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>
1139
1172
1140
1173
<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.
1148
1179
</para>
1149
1180
</refsect2>
1150
1181
@@ -1153,15 +1184,15 @@ SELECT distributors.* WHERE distributors.name = 'Westward';
1153
1184
1154
1185
<para>
1155
1186
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
1157
1188
BY</literal> clause can only use expressions based on input column
1158
1189
names. <productname>PostgreSQL</productname> extends each of
1159
1190
these clauses to allow the other choice as well (but it uses the
1160
1191
standard's interpretation if there is ambiguity).
1161
1192
<productname>PostgreSQL</productname> also allows both clauses to
1162
1193
specify arbitrary expressions. Note that names appearing in an
1163
1194
expression will always be taken as input-column names, not as
1164
- result -column names.
1195
+ output -column names.
1165
1196
</para>
1166
1197
1167
1198
<para>
0 commit comments