~ubuntu-branches/ubuntu/oneiric/postgresql-9.1/oneiric-security

« back to all changes in this revision

Viewing changes to doc/src/sgml/html/queries-select-lists.html

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2011-05-11 10:41:53 UTC
  • Revision ID: james.westby@ubuntu.com-20110511104153-psbh2o58553fv1m0
Tags: upstream-9.1~beta1
ImportĀ upstreamĀ versionĀ 9.1~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 
2
<HTML
 
3
><HEAD
 
4
><TITLE
 
5
>Select Lists</TITLE
 
6
><META
 
7
NAME="GENERATOR"
 
8
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
 
9
REV="MADE"
 
10
HREF="mailto:pgsql-docs@postgresql.org"><LINK
 
11
REL="HOME"
 
12
TITLE="PostgreSQL 9.1beta1 Documentation"
 
13
HREF="index.html"><LINK
 
14
REL="UP"
 
15
TITLE="Queries"
 
16
HREF="queries.html"><LINK
 
17
REL="PREVIOUS"
 
18
TITLE="Table Expressions"
 
19
HREF="queries-table-expressions.html"><LINK
 
20
REL="NEXT"
 
21
TITLE="Combining Queries"
 
22
HREF="queries-union.html"><LINK
 
23
REL="STYLESHEET"
 
24
TYPE="text/css"
 
25
HREF="stylesheet.css"><META
 
26
HTTP-EQUIV="Content-Type"
 
27
CONTENT="text/html; charset=ISO-8859-1"><META
 
28
NAME="creation"
 
29
CONTENT="2011-04-27T21:20:33"></HEAD
 
30
><BODY
 
31
CLASS="SECT1"
 
32
><DIV
 
33
CLASS="NAVHEADER"
 
34
><TABLE
 
35
SUMMARY="Header navigation table"
 
36
WIDTH="100%"
 
37
BORDER="0"
 
38
CELLPADDING="0"
 
39
CELLSPACING="0"
 
40
><TR
 
41
><TH
 
42
COLSPAN="5"
 
43
ALIGN="center"
 
44
VALIGN="bottom"
 
45
><A
 
46
HREF="index.html"
 
47
>PostgreSQL 9.1beta1 Documentation</A
 
48
></TH
 
49
></TR
 
50
><TR
 
51
><TD
 
52
WIDTH="10%"
 
53
ALIGN="left"
 
54
VALIGN="top"
 
55
><A
 
56
TITLE="Table Expressions"
 
57
HREF="queries-table-expressions.html"
 
58
ACCESSKEY="P"
 
59
>Prev</A
 
60
></TD
 
61
><TD
 
62
WIDTH="10%"
 
63
ALIGN="left"
 
64
VALIGN="top"
 
65
><A
 
66
TITLE="Queries"
 
67
HREF="queries.html"
 
68
>Fast Backward</A
 
69
></TD
 
70
><TD
 
71
WIDTH="60%"
 
72
ALIGN="center"
 
73
VALIGN="bottom"
 
74
>Chapter 7. Queries</TD
 
75
><TD
 
76
WIDTH="10%"
 
77
ALIGN="right"
 
78
VALIGN="top"
 
79
><A
 
80
TITLE="Queries"
 
81
HREF="queries.html"
 
82
>Fast Forward</A
 
83
></TD
 
84
><TD
 
85
WIDTH="10%"
 
86
ALIGN="right"
 
87
VALIGN="top"
 
88
><A
 
89
TITLE="Combining Queries"
 
90
HREF="queries-union.html"
 
91
ACCESSKEY="N"
 
92
>Next</A
 
93
></TD
 
94
></TR
 
95
></TABLE
 
96
><HR
 
97
ALIGN="LEFT"
 
98
WIDTH="100%"></DIV
 
99
><DIV
 
100
CLASS="SECT1"
 
101
><H1
 
102
CLASS="SECT1"
 
103
><A
 
104
NAME="QUERIES-SELECT-LISTS"
 
105
>7.3. Select Lists</A
 
106
></H1
 
107
><P
 
108
>   As shown in the previous section,
 
109
   the table expression in the <TT
 
110
CLASS="COMMAND"
 
111
>SELECT</TT
 
112
> command
 
113
   constructs an intermediate virtual table by possibly combining
 
114
   tables, views, eliminating rows, grouping, etc.  This table is
 
115
   finally passed on to processing by the <I
 
116
CLASS="FIRSTTERM"
 
117
>select list</I
 
118
>.  The select
 
119
   list determines which <SPAN
 
120
CLASS="emphasis"
 
121
><I
 
122
CLASS="EMPHASIS"
 
123
>columns</I
 
124
></SPAN
 
125
> of the
 
126
   intermediate table are actually output.
 
127
  </P
 
128
><DIV
 
129
CLASS="SECT2"
 
130
><H2
 
131
CLASS="SECT2"
 
132
><A
 
133
NAME="QUERIES-SELECT-LIST-ITEMS"
 
134
>7.3.1. Select-List Items</A
 
135
></H2
 
136
><P
 
137
>    The simplest kind of select list is <TT
 
138
CLASS="LITERAL"
 
139
>*</TT
 
140
> which
 
141
    emits all columns that the table expression produces.  Otherwise,
 
142
    a select list is a comma-separated list of value expressions (as
 
143
    defined in <A
 
144
HREF="sql-expressions.html"
 
145
>Section 4.2</A
 
146
>).  For instance, it
 
147
    could be a list of column names:
 
148
</P><PRE
 
149
CLASS="PROGRAMLISTING"
 
150
>SELECT a, b, c FROM ...</PRE
 
151
><P>
 
152
     The columns names <TT
 
153
CLASS="LITERAL"
 
154
>a</TT
 
155
>, <TT
 
156
CLASS="LITERAL"
 
157
>b</TT
 
158
>, and <TT
 
159
CLASS="LITERAL"
 
160
>c</TT
 
161
>
 
162
     are either the actual names of the columns of tables referenced
 
163
     in the <TT
 
164
CLASS="LITERAL"
 
165
>FROM</TT
 
166
> clause, or the aliases given to them as
 
167
     explained in <A
 
168
HREF="queries-table-expressions.html#QUERIES-TABLE-ALIASES"
 
169
>Section 7.2.1.2</A
 
170
>.  The name
 
171
     space available in the select list is the same as in the
 
172
     <TT
 
173
CLASS="LITERAL"
 
174
>WHERE</TT
 
175
> clause, unless grouping is used, in which case
 
176
     it is the same as in the <TT
 
177
CLASS="LITERAL"
 
178
>HAVING</TT
 
179
> clause.
 
180
   </P
 
181
><P
 
182
>    If more than one table has a column of the same name, the table
 
183
    name must also be given, as in:
 
184
</P><PRE
 
185
CLASS="PROGRAMLISTING"
 
186
>SELECT tbl1.a, tbl2.a, tbl1.b FROM ...</PRE
 
187
><P>
 
188
    When working with multiple tables, it can also be useful to ask for
 
189
    all the columns of a particular table:
 
190
</P><PRE
 
191
CLASS="PROGRAMLISTING"
 
192
>SELECT tbl1.*, tbl2.a FROM ...</PRE
 
193
><P>
 
194
    (See also <A
 
195
HREF="queries-table-expressions.html#QUERIES-WHERE"
 
196
>Section 7.2.2</A
 
197
>.)
 
198
   </P
 
199
><P
 
200
>    If an arbitrary value expression is used in the select list, it
 
201
    conceptually adds a new virtual column to the returned table.  The
 
202
    value expression is evaluated once for each result row, with
 
203
    the row's values substituted for any column references.  But the
 
204
    expressions in the select list do not have to reference any
 
205
    columns in the table expression of the <TT
 
206
CLASS="LITERAL"
 
207
>FROM</TT
 
208
> clause;
 
209
    they can be constant arithmetic expressions, for instance.
 
210
   </P
 
211
></DIV
 
212
><DIV
 
213
CLASS="SECT2"
 
214
><H2
 
215
CLASS="SECT2"
 
216
><A
 
217
NAME="QUERIES-COLUMN-LABELS"
 
218
>7.3.2. Column Labels</A
 
219
></H2
 
220
><P
 
221
>    The entries in the select list can be assigned names for subsequent
 
222
    processing, such as for use in an <TT
 
223
CLASS="LITERAL"
 
224
>ORDER BY</TT
 
225
> clause
 
226
    or for display by the client application.  For example:
 
227
</P><PRE
 
228
CLASS="PROGRAMLISTING"
 
229
>SELECT a AS value, b + c AS sum FROM ...</PRE
 
230
><P>
 
231
   </P
 
232
><P
 
233
>    If no output column name is specified using <TT
 
234
CLASS="LITERAL"
 
235
>AS</TT
 
236
>,
 
237
    the system assigns a default column name.  For simple column references,
 
238
    this is the name of the referenced column.  For function
 
239
    calls, this is the name of the function.  For complex expressions,
 
240
    the system will generate a generic name.
 
241
   </P
 
242
><P
 
243
>    The <TT
 
244
CLASS="LITERAL"
 
245
>AS</TT
 
246
> keyword is optional, but only if the new column
 
247
    name does not match any
 
248
    <SPAN
 
249
CLASS="PRODUCTNAME"
 
250
>PostgreSQL</SPAN
 
251
> keyword (see <A
 
252
HREF="sql-keywords-appendix.html"
 
253
>Appendix C</A
 
254
>).  To avoid an accidental match to
 
255
    a keyword, you can double-quote the column name.  For example,
 
256
    <TT
 
257
CLASS="LITERAL"
 
258
>VALUE</TT
 
259
> is a keyword, so this does not work:
 
260
</P><PRE
 
261
CLASS="PROGRAMLISTING"
 
262
>SELECT a value, b + c AS sum FROM ...</PRE
 
263
><P>
 
264
    but this does:
 
265
</P><PRE
 
266
CLASS="PROGRAMLISTING"
 
267
>SELECT a "value", b + c AS sum FROM ...</PRE
 
268
><P>
 
269
    For protection against possible
 
270
    future keyword additions, it is recommended that you always either
 
271
    write <TT
 
272
CLASS="LITERAL"
 
273
>AS</TT
 
274
> or double-quote the output column name.
 
275
   </P
 
276
><DIV
 
277
CLASS="NOTE"
 
278
><BLOCKQUOTE
 
279
CLASS="NOTE"
 
280
><P
 
281
><B
 
282
>Note: </B
 
283
>     The naming of output columns here is different from that done in
 
284
     the <TT
 
285
CLASS="LITERAL"
 
286
>FROM</TT
 
287
> clause (see <A
 
288
HREF="queries-table-expressions.html#QUERIES-TABLE-ALIASES"
 
289
>Section 7.2.1.2</A
 
290
>).  It is possible
 
291
     to rename the same column twice, but the name assigned in
 
292
     the select list is the one that will be passed on.
 
293
    </P
 
294
></BLOCKQUOTE
 
295
></DIV
 
296
></DIV
 
297
><DIV
 
298
CLASS="SECT2"
 
299
><H2
 
300
CLASS="SECT2"
 
301
><A
 
302
NAME="QUERIES-DISTINCT"
 
303
>7.3.3. <TT
 
304
CLASS="LITERAL"
 
305
>DISTINCT</TT
 
306
></A
 
307
></H2
 
308
><P
 
309
>    After the select list has been processed, the result table can
 
310
    optionally be subject to the elimination of duplicate rows.  The
 
311
    <TT
 
312
CLASS="LITERAL"
 
313
>DISTINCT</TT
 
314
> key word is written directly after
 
315
    <TT
 
316
CLASS="LITERAL"
 
317
>SELECT</TT
 
318
> to specify this:
 
319
</P><PRE
 
320
CLASS="SYNOPSIS"
 
321
>SELECT DISTINCT <TT
 
322
CLASS="REPLACEABLE"
 
323
><I
 
324
>select_list</I
 
325
></TT
 
326
> ...</PRE
 
327
><P>
 
328
    (Instead of <TT
 
329
CLASS="LITERAL"
 
330
>DISTINCT</TT
 
331
> the key word <TT
 
332
CLASS="LITERAL"
 
333
>ALL</TT
 
334
>
 
335
    can be used to specify the default behavior of retaining all rows.)
 
336
   </P
 
337
><P
 
338
>    Obviously, two rows are considered distinct if they differ in at
 
339
    least one column value.  Null values are considered equal in this
 
340
    comparison.
 
341
   </P
 
342
><P
 
343
>    Alternatively, an arbitrary expression can determine what rows are
 
344
    to be considered distinct:
 
345
</P><PRE
 
346
CLASS="SYNOPSIS"
 
347
>SELECT DISTINCT ON (<TT
 
348
CLASS="REPLACEABLE"
 
349
><I
 
350
>expression</I
 
351
></TT
 
352
> [<SPAN
 
353
CLASS="OPTIONAL"
 
354
>, <TT
 
355
CLASS="REPLACEABLE"
 
356
><I
 
357
>expression</I
 
358
></TT
 
359
> ...</SPAN
 
360
>]) <TT
 
361
CLASS="REPLACEABLE"
 
362
><I
 
363
>select_list</I
 
364
></TT
 
365
> ...</PRE
 
366
><P>
 
367
    Here <TT
 
368
CLASS="REPLACEABLE"
 
369
><I
 
370
>expression</I
 
371
></TT
 
372
> is an arbitrary value
 
373
    expression that is evaluated for all rows.  A set of rows for
 
374
    which all the expressions are equal are considered duplicates, and
 
375
    only the first row of the set is kept in the output.  Note that
 
376
    the <SPAN
 
377
CLASS="QUOTE"
 
378
>"first row"</SPAN
 
379
> of a set is unpredictable unless the
 
380
    query is sorted on enough columns to guarantee a unique ordering
 
381
    of the rows arriving at the <TT
 
382
CLASS="LITERAL"
 
383
>DISTINCT</TT
 
384
> filter.
 
385
    (<TT
 
386
CLASS="LITERAL"
 
387
>DISTINCT ON</TT
 
388
> processing occurs after <TT
 
389
CLASS="LITERAL"
 
390
>ORDER
 
391
    BY</TT
 
392
> sorting.)
 
393
   </P
 
394
><P
 
395
>    The <TT
 
396
CLASS="LITERAL"
 
397
>DISTINCT ON</TT
 
398
> clause is not part of the SQL standard
 
399
    and is sometimes considered bad style because of the potentially
 
400
    indeterminate nature of its results.  With judicious use of
 
401
    <TT
 
402
CLASS="LITERAL"
 
403
>GROUP BY</TT
 
404
> and subqueries in <TT
 
405
CLASS="LITERAL"
 
406
>FROM</TT
 
407
>, this
 
408
    construct can be avoided, but it is often the most convenient
 
409
    alternative.
 
410
   </P
 
411
></DIV
 
412
></DIV
 
413
><DIV
 
414
CLASS="NAVFOOTER"
 
415
><HR
 
416
ALIGN="LEFT"
 
417
WIDTH="100%"><TABLE
 
418
SUMMARY="Footer navigation table"
 
419
WIDTH="100%"
 
420
BORDER="0"
 
421
CELLPADDING="0"
 
422
CELLSPACING="0"
 
423
><TR
 
424
><TD
 
425
WIDTH="33%"
 
426
ALIGN="left"
 
427
VALIGN="top"
 
428
><A
 
429
HREF="queries-table-expressions.html"
 
430
ACCESSKEY="P"
 
431
>Prev</A
 
432
></TD
 
433
><TD
 
434
WIDTH="34%"
 
435
ALIGN="center"
 
436
VALIGN="top"
 
437
><A
 
438
HREF="index.html"
 
439
ACCESSKEY="H"
 
440
>Home</A
 
441
></TD
 
442
><TD
 
443
WIDTH="33%"
 
444
ALIGN="right"
 
445
VALIGN="top"
 
446
><A
 
447
HREF="queries-union.html"
 
448
ACCESSKEY="N"
 
449
>Next</A
 
450
></TD
 
451
></TR
 
452
><TR
 
453
><TD
 
454
WIDTH="33%"
 
455
ALIGN="left"
 
456
VALIGN="top"
 
457
>Table Expressions</TD
 
458
><TD
 
459
WIDTH="34%"
 
460
ALIGN="center"
 
461
VALIGN="top"
 
462
><A
 
463
HREF="queries.html"
 
464
ACCESSKEY="U"
 
465
>Up</A
 
466
></TD
 
467
><TD
 
468
WIDTH="33%"
 
469
ALIGN="right"
 
470
VALIGN="top"
 
471
>Combining Queries</TD
 
472
></TR
 
473
></TABLE
 
474
></DIV
 
475
></BODY
 
476
></HTML
 
477
>
 
 
b'\\ No newline at end of file'