1
- <!-- $PostgreSQL: pgsql/doc/src/sgml/queries.sgml,v 1.38 2006/10/23 18:10:31 petere Exp $ -->
1
+ <!-- $PostgreSQL: pgsql/doc/src/sgml/queries.sgml,v 1.39 2006/10/24 02:24:27 tgl Exp $ -->
2
2
3
3
<chapter id="queries">
4
4
<title>Queries</title>
@@ -514,8 +514,8 @@ SELECT * FROM my_table AS m WHERE my_table.a > 5;
514
514
is not valid according to the SQL standard. In
515
515
<productname>PostgreSQL</productname> this will draw an error if the
516
516
<xref linkend="guc-add-missing-from"> configuration variable is
517
- <literal>off</>. If it is <literal>on</>, an implicit table reference
518
- will be added to the
517
+ <literal>off</> (as it is by default) . If it is <literal>on</>,
518
+ an implicit table reference will be added to the
519
519
<literal>FROM</literal> clause, so the query is processed as if
520
520
it were written as
521
521
<programlisting>
@@ -1224,38 +1224,17 @@ SELECT DISTINCT ON (<replaceable>expression</replaceable> <optional>, <replaceab
1224
1224
<synopsis>
1225
1225
SELECT <replaceable>select_list</replaceable>
1226
1226
FROM <replaceable>table_expression</replaceable>
1227
- ORDER BY <replaceable>column1 </replaceable> <optional>ASC | DESC</optional> <optional>, <replaceable>column2 </replaceable> <optional>ASC | DESC</optional> ...</optional>
1227
+ ORDER BY <replaceable>sort_expression1 </replaceable> <optional>ASC | DESC</optional> <optional>, <replaceable>sort_expression2 </replaceable> <optional>ASC | DESC</optional> ...</optional>
1228
1228
</synopsis>
1229
- <replaceable>column1</replaceable>, etc., refer to select list
1230
- columns. These can be either the output name of a column (see
1231
- <xref linkend="queries-column-labels">) or the number of a column. Some
1232
- examples:
1229
+ The sort expression(s) can be any expression that would be valid in the
1230
+ query's select list. An example is
1233
1231
<programlisting>
1234
- SELECT a, b FROM table1 ORDER BY a;
1235
- SELECT a + b AS sum, c FROM table1 ORDER BY sum;
1236
- SELECT a, sum(b) FROM table1 GROUP BY a ORDER BY 1;
1237
- </programlisting>
1238
- </para>
1239
-
1240
- <para>
1241
- As an extension to the SQL standard, <productname>PostgreSQL</productname> also allows ordering
1242
- by arbitrary expressions:
1243
- <programlisting>
1244
- SELECT a, b FROM table1 ORDER BY a + b;
1245
- </programlisting>
1246
- References to column names of the <literal>FROM</> clause that are
1247
- not present in the select list are also allowed:
1248
- <programlisting>
1249
- SELECT a FROM table1 ORDER BY b;
1232
+ SELECT a, b FROM table1 ORDER BY a + b, c;
1250
1233
</programlisting>
1251
- But these extensions do not work in queries involving
1252
- <literal>UNION</>, <literal>INTERSECT</>, or <literal>EXCEPT</>,
1253
- and are not portable to other SQL databases.
1254
- </para>
1255
-
1256
- <para>
1257
- Each column specification may be followed by an optional
1258
- <literal>ASC</> or <literal>DESC</> to set the sort direction to
1234
+ When more than one expression is specified,
1235
+ the later values are used to sort rows that are equal according to the
1236
+ earlier values. Each expression may be followed by an optional
1237
+ <literal>ASC</> or <literal>DESC</> keyword to set the sort direction to
1259
1238
ascending or descending. <literal>ASC</> order is the default.
1260
1239
Ascending order puts smaller values first, where
1261
1240
<quote>smaller</quote> is defined in terms of the
@@ -1264,7 +1243,7 @@ SELECT a FROM table1 ORDER BY b;
1264
1243
<footnote>
1265
1244
<para>
1266
1245
Actually, <productname>PostgreSQL</> uses the <firstterm>default B-tree
1267
- operator class</> for thecolumn 's data type to determine the sort
1246
+ operator class</> for theexpression 's data type to determine the sort
1268
1247
ordering for <literal>ASC</> and <literal>DESC</>. Conventionally,
1269
1248
data types will be set up so that the <literal><</literal> and
1270
1249
<literal>></literal> operators correspond to this sort ordering,
@@ -1275,9 +1254,32 @@ SELECT a FROM table1 ORDER BY b;
1275
1254
</para>
1276
1255
1277
1256
<para>
1278
- If more than one sort column is specified, the later entries are
1279
- used to sort rows that are equal under the order imposed by the
1280
- earlier sort columns.
1257
+ For backwards compatibility with the SQL92 version of the standard,
1258
+ a <replaceable>sort_expression</> can instead be the name or number
1259
+ of an output column, as in
1260
+ <programlisting>
1261
+ SELECT a + b AS sum, c FROM table1 ORDER BY sum;
1262
+ SELECT a, max(b) FROM table1 GROUP BY a ORDER BY 1;
1263
+ </programlisting>
1264
+ both of which sort by the first output column. Note that an output
1265
+ column name has to stand alone, it's not allowed as part of an expression
1266
+ — for example, this is <emphasis>not</> correct:
1267
+ <programlisting>
1268
+ SELECT a + b AS sum, c FROM table1 ORDER BY sum + c; -- wrong
1269
+ </programlisting>
1270
+ This restriction is made to reduce ambiguity. There is still
1271
+ ambiguity if an <literal>ORDER BY</> item is a simple name that
1272
+ could match either an output column name or a column from the table
1273
+ expression. The output column is used in such cases. This would
1274
+ only cause confusion if you use <literal>AS</> to rename an output
1275
+ column to match some other table column's name.
1276
+ </para>
1277
+
1278
+ <para>
1279
+ <literal>ORDER BY</> can be applied to the result of a
1280
+ <literal>UNION</>, <literal>INTERSECT</>, or <literal>EXCEPT</>
1281
+ combination, but in this case it is only permitted to sort by
1282
+ output column names or numbers, not by expressions.
1281
1283
</para>
1282
1284
</sect1>
1283
1285
@@ -1299,6 +1301,7 @@ SELECT a FROM table1 ORDER BY b;
1299
1301
<synopsis>
1300
1302
SELECT <replaceable>select_list</replaceable>
1301
1303
FROM <replaceable>table_expression</replaceable>
1304
+ <optional> ORDER BY <replaceable>sort_expression1</replaceable> <optional>ASC | DESC</optional> <optional>, <replaceable>sort_expression2</replaceable> <optional>ASC | DESC</optional> ...</optional> </optional>
1302
1305
<optional> LIMIT { <replaceable>number</replaceable> | ALL } </optional> <optional> OFFSET <replaceable>number</replaceable> </optional>
1303
1306
</synopsis>
1304
1307
</para>