1
1
<!--
2
- $Header: /cvsroot/pgsql/doc/src/sgml/ref/select.sgml,v 1.25 2000/02/21 01:13:52 tgl Exp $
2
+ $Header: /cvsroot/pgsql/doc/src/sgml/ref/select.sgml,v 1.26 2000/03/15 23:31:19 tgl Exp $
3
3
Postgres documentation
4
4
-->
5
5
@@ -37,7 +37,7 @@ SELECT [ ALL | DISTINCT [ ON ( <replaceable class="PARAMETER">expression</replac
37
37
38
38
<refsect2 id="R2-SQL-SELECT-1">
39
39
<refsect2info>
40
- <date>1998-09-24 </date>
40
+ <date>2000-03-15 </date>
41
41
</refsect2info>
42
42
<title>
43
43
Inputs
@@ -59,10 +59,12 @@ SELECT [ ALL | DISTINCT [ ON ( <replaceable class="PARAMETER">expression</replac
59
59
<listitem>
60
60
<para>
61
61
Specifies another name for a column or an expression using
62
- the AS clause. This name is primarily used to label the output
63
- column. The <replaceable class="PARAMETER">name</replaceable>
64
- cannot be used in the WHERE, GROUP BY, or HAVING clauses.
65
- It can, however, be referenced in ORDER BY clauses.
62
+ the AS clause. This name is primarily used to label the column
63
+ for display. It can also be used to refer to the column's value in
64
+ ORDER BY and GROUP BY clauses. But the
65
+ <replaceable class="PARAMETER">name</replaceable>
66
+ cannot be used in the WHERE or HAVING clauses; write out the
67
+ expression instead.
66
68
</para>
67
69
</listitem>
68
70
</varlistentry>
@@ -72,7 +74,8 @@ SELECT [ ALL | DISTINCT [ ON ( <replaceable class="PARAMETER">expression</replac
72
74
<term>TEMP</term>
73
75
<listitem>
74
76
<para>
75
- The table is created unique to this session, and is
77
+ If TEMPORARY or TEMP is specified,
78
+ the table is created unique to this session, and is
76
79
automatically dropped on session exit.
77
80
</para>
78
81
</listitem>
@@ -83,10 +86,10 @@ SELECT [ ALL | DISTINCT [ ON ( <replaceable class="PARAMETER">expression</replac
83
86
<listitem>
84
87
<para>
85
88
If the INTO TABLE clause is specified, the result of the
86
- query will be stored in another table with the indicated
89
+ query will be stored in a new table with the indicated
87
90
name.
88
91
The target table (<replaceable class="PARAMETER">new_table</replaceable>) will
89
- be created automatically and should not exist before this command.
92
+ be created automatically and must not exist before this command.
90
93
Refer to <command>SELECT INTO</command> for more information.
91
94
92
95
<note>
@@ -143,7 +146,8 @@ SELECT [ ALL | DISTINCT [ ON ( <replaceable class="PARAMETER">expression</replac
143
146
<term><replaceable class="PARAMETER">select</replaceable></term>
144
147
<listitem>
145
148
<para>
146
- A select statement with all features except the ORDER BY clause.
149
+ A select statement with all features except the ORDER BY and
150
+ LIMIT clauses.
147
151
</para>
148
152
</listitem>
149
153
</varlistentry>
@@ -188,7 +192,7 @@ SELECT [ ALL | DISTINCT [ ON ( <replaceable class="PARAMETER">expression</replac
188
192
189
193
<refsect1 id="R1-SQL-SELECT-1">
190
194
<refsect1info>
191
- <date>1998-09-24 </date>
195
+ <date>2000-03-15 </date>
192
196
</refsect1info>
193
197
<title>
194
198
Description
@@ -210,7 +214,9 @@ SELECT [ ALL | DISTINCT [ ON ( <replaceable class="PARAMETER">expression</replac
210
214
<para>
211
215
<command>DISTINCT ON</command> eliminates rows that match on all the
212
216
specified expressions, keeping only the first row of each set of
213
- duplicates. Note that "the first row" of each set is unpredictable
217
+ duplicates. The DISTINCT ON expressions are interpreted using the
218
+ same rules as for ORDER BY items; see below.
219
+ Note that "the first row" of each set is unpredictable
214
220
unless <command>ORDER BY</command> is used to ensure that the desired
215
221
row appears first. For example,
216
222
<programlisting>
@@ -226,21 +232,20 @@ SELECT [ ALL | DISTINCT [ ON ( <replaceable class="PARAMETER">expression</replac
226
232
227
233
<para>
228
234
The GROUP BY clause allows a user to divide a table
229
- conceptually into groups.
235
+ into groups of rows that match on one or more values .
230
236
(See <xref linkend="sql-groupby" endterm="sql-groupby-title">.)
231
237
</para>
232
238
233
239
<para>
234
- The HAVING clause specifies a grouped table derived by the
235
- elimination of groups from the result of the previously
236
- specified clause.
240
+ The HAVING clause allows selection of only those groups of rows
241
+ meeting the specified condition.
237
242
(See <xref linkend="sql-having" endterm="sql-having-title">.)
238
243
</para>
239
244
240
245
<para>
241
- The ORDER BY clause allows a user to specify that he/she
242
- wishes the rows sorted according to the ASCending or
243
- DESCending mode operator .
246
+ The ORDER BY clause causes the returned rows to be sorted in a specified
247
+ order. If ORDER BY is not given, the rows are returned in whatever order
248
+ the system finds cheapest to produce .
244
249
(See <xref linkend="sql-orderby-title" endterm="sql-orderby-title">.)
245
250
</para>
246
251
@@ -279,7 +284,7 @@ SELECT [ ALL | DISTINCT [ ON ( <replaceable class="PARAMETER">expression</replac
279
284
280
285
<refsect2 id="SQL-WHERE">
281
286
<refsect2info>
282
- <date>1998-09-24 </date>
287
+ <date>2000-03-15 </date>
283
288
</refsect2info>
284
289
<title id="sql-where-title">
285
290
WHERE Clause
@@ -312,15 +317,14 @@ WHERE <replaceable class="PARAMETER">boolean_expr</replaceable>
312
317
locally-defined operator,
313
318
and <replaceable class="PARAMETER">log_op</replaceable> can be one
314
319
of: AND, OR, NOT.
315
- The comparison returns either TRUE or FALSE and all
316
- instances will be discarded
317
- if the expression evaluates to FALSE.
320
+ SELECT will ignore all rows for which the WHERE condition does not return
321
+ TRUE.
318
322
</para>
319
323
</refsect2>
320
324
321
325
<refsect2 id="SQL-GROUPBY">
322
326
<refsect2info>
323
- <date>1998-09-24 </date>
327
+ <date>2000-03-15 </date>
324
328
</refsect2info>
325
329
<title id="sql-groupby-title">
326
330
GROUP BY Clause
@@ -334,20 +338,28 @@ GROUP BY <replaceable class="PARAMETER">column</replaceable> [, ...]
334
338
</para>
335
339
336
340
<para>
337
- GROUP BY will condense into a single row all rows that share the
341
+ GROUP BY will condense into a single row all selected rows that share the
338
342
same values for the grouped columns. Aggregate functions, if any,
339
343
are computed across all rows making up each group, producing a
340
344
separate value for each group (whereas without GROUP BY, an
341
345
aggregate produces a single value computed across all the selected
342
- rows). When GROUP BY is present, it is not valid to refer to
346
+ rows). When GROUP BY is present, it is not valid for the SELECT
347
+ output expression(s) to refer to
343
348
ungrouped columns except within aggregate functions, since there
344
349
would be more than one possible value to return for an ungrouped column.
345
350
</para>
351
+
352
+ <para>
353
+ An item in GROUP BY can also be the name or ordinal number of an output
354
+ column (SELECT expression), or it can be an arbitrary expression formed
355
+ from input-column values. In case of ambiguity, a GROUP BY name will
356
+ be interpreted as an input-column name rather than an output column name.
357
+ </para>
346
358
</refsect2>
347
359
348
360
<refsect2 id="SQL-HAVING">
349
361
<refsect2info>
350
- <date>1998-09-24 </date>
362
+ <date>2000-03-15 </date>
351
363
</refsect2info>
352
364
<title id="sql-having-title">
353
365
HAVING Clause
@@ -365,8 +377,12 @@ HAVING <replaceable class="PARAMETER">cond_expr</replaceable>
365
377
366
378
<para>
367
379
HAVING specifies a grouped table derived by the elimination
368
- of groups from the result of the previously specified clause
369
- that do not meet the <replaceable class="PARAMETER">cond_expr</replaceable>.</para>
380
+ of group rows that do not satisfy the
381
+ <replaceable class="PARAMETER">cond_expr</replaceable>.
382
+ HAVING is different from WHERE:
383
+ WHERE filters individual rows before application of GROUP BY,
384
+ while HAVING filters group rows created by GROUP BY.
385
+ </para>
370
386
371
387
<para>
372
388
Each column referenced in
@@ -378,7 +394,7 @@ HAVING <replaceable class="PARAMETER">cond_expr</replaceable>
378
394
379
395
<refsect2 id="SQL-ORDERBY">
380
396
<refsect2info>
381
- <date>1998-09-24 </date>
397
+ <date>2000-03-15 </date>
382
398
</refsect2info>
383
399
<title id="sql-orderby-title">
384
400
ORDER BY Clause
@@ -389,15 +405,15 @@ ORDER BY <replaceable class="PARAMETER">column</replaceable> [ ASC | DESC ] [, .
389
405
</synopsis></para>
390
406
391
407
<para>
392
- <replaceable class="PARAMETER">column</replaceable> can be either a column
393
- name or an ordinal number.
408
+ <replaceable class="PARAMETER">column</replaceable> can be either a
409
+ result column name or an ordinal number.
394
410
</para>
395
411
<para>
396
412
The ordinal numbers refers to the ordinal (left-to-right) position
397
- of the column. This feature makes it possible to define an ordering
413
+ of the result column. This feature makes it possible to define an ordering
398
414
on the basis of a column that does not have a proper name.
399
415
This is never absolutely necessary because it is always possible
400
- to assign a name to a calculated column using the AS clause, e.g.:
416
+ to assign a name to a result column using the AS clause, e.g.:
401
417
<programlisting>
402
418
SELECT title, date_prod + 1 AS newlen FROM films ORDER BY newlen;
403
419
</programlisting></para>
@@ -410,6 +426,11 @@ SELECT title, date_prod + 1 AS newlen FROM films ORDER BY newlen;
410
426
<programlisting>
411
427
SELECT name FROM distributors ORDER BY code;
412
428
</programlisting>
429
+ Note that if an ORDER BY item is a simple name that matches both
430
+ a result column name and an input column name, ORDER BY will interpret
431
+ it as the result column name. This is the opposite of the choice that
432
+ GROUP BY will make in the same situation. This inconsistency is
433
+ mandated by the SQL92 standard.
413
434
</para>
414
435
415
436
<para>
@@ -436,7 +457,7 @@ SELECT name FROM distributors ORDER BY code;
436
457
437
458
where
438
459
<replaceable class="PARAMETER">table_query</replaceable>
439
- specifies any select expression without an ORDER BY clause.
460
+ specifies any select expression without an ORDER BY or LIMIT clause.
440
461
</para>
441
462
442
463
<para>
@@ -476,7 +497,7 @@ SELECT name FROM distributors ORDER BY code;
476
497
477
498
where
478
499
<replaceable class="PARAMETER">table_query</replaceable>
479
- specifies any select expression without an ORDER BY clause.
500
+ specifies any select expression without an ORDER BY or LIMIT clause.
480
501
</para>
481
502
482
503
<para>
@@ -507,7 +528,7 @@ SELECT name FROM distributors ORDER BY code;
507
528
508
529
where
509
530
<replaceable class="PARAMETER">table_query</replaceable>
510
- specifies any select expression without an ORDER BY clause.
531
+ specifies any select expression without an ORDER BY or LIMIT clause.
511
532
</para>
512
533
513
534
<para>
@@ -560,7 +581,7 @@ SELECT name FROM distributors ORDER BY code;
560
581
</para>
561
582
562
583
<para>
563
- As of Postgres 7.0, the
584
+ As of PostgreSQL 7.0, the
564
585
query optimizer takes LIMIT into account when generating a query plan,
565
586
so you are very likely to get different plans (yielding different row
566
587
orders) depending on what you give for LIMIT and OFFSET. Thus, using
@@ -765,6 +786,18 @@ SELECT distributors.* WHERE name = 'Westwood';
765
786
The DISTINCT ON phrase is not part of <acronym>SQL92</acronym>.
766
787
Nor are LIMIT and OFFSET.
767
788
</para>
789
+
790
+ <para>
791
+ In <acronym>SQL92</acronym>, an ORDER BY clause may only use result
792
+ column names or numbers, while a GROUP BY clause may only use input
793
+ column names.
794
+ <productname>Postgres</productname> extends each of these clauses to
795
+ allow the other choice as well (but it uses the standard's interpretation
796
+ if there is ambiguity).
797
+ <productname>Postgres</productname> also allows both clauses to specify
798
+ arbitrary expressions. Note that names appearing in an expression will
799
+ always be taken as input-column names, not as result-column names.
800
+ </para>
768
801
</refsect3>
769
802
770
803
<refsect3 id="R3-SQL-UNION-1">
0 commit comments