]> granicus.if.org Git - postgresql/blob - doc/src/sgml/ref/select.sgml
Inheritance overhaul by Chris Bitmead <chris@bitmead.com>
[postgresql] / doc / src / sgml / ref / select.sgml
1 <!--
2 $Header: /cvsroot/pgsql/doc/src/sgml/ref/select.sgml,v 1.29 2000/06/09 01:44:00 momjian Exp $
3 Postgres documentation
4 -->
5
6 <refentry id="SQL-SELECT">
7  <refmeta>
8   <refentrytitle id="sql-select-title">
9    SELECT
10   </refentrytitle>
11   <refmiscinfo>SQL - Language Statements</refmiscinfo>
12  </refmeta>
13  <refnamediv>
14   <refname>
15    SELECT
16   </refname>
17   <refpurpose>
18    Retrieve rows from a table or view.
19   </refpurpose></refnamediv>
20  <refsynopsisdiv>
21   <refsynopsisdivinfo>
22    <date>1999-07-20</date>
23   </refsynopsisdivinfo>
24   <synopsis>
25 SELECT [ ALL | DISTINCT [ ON ( <replaceable class="PARAMETER">expression</replaceable> [, ...] ) ] ]
26     <replaceable class="PARAMETER">expression</replaceable> [ AS <replaceable class="PARAMETER">name</replaceable> ] [, ...]
27     [ INTO [ TEMPORARY | TEMP ] [ TABLE ] <replaceable class="PARAMETER">new_table</replaceable> ]
28     [ FROM [ ONLY ]<replaceable class="PARAMETER">table</replaceable> [ <replaceable class="PARAMETER">alias</replaceable> ] [, ...] ]
29     [ WHERE <replaceable class="PARAMETER">condition</replaceable> ]
30     [ GROUP BY <replaceable class="PARAMETER">column</replaceable> [, ...] ]
31     [ HAVING <replaceable class="PARAMETER">condition</replaceable> [, ...] ]
32     [ { UNION [ ALL ] | INTERSECT | EXCEPT } <replaceable class="PARAMETER">select</replaceable> ]
33     [ ORDER BY <replaceable class="PARAMETER">column</replaceable> [ ASC | DESC | USING <replaceable class="PARAMETER">operator</replaceable> ] [, ...] ]
34     [ FOR UPDATE [ OF <replaceable class="PARAMETER">class_name</replaceable> [, ...] ] ]
35     LIMIT { <replaceable class="PARAMETER">count</replaceable> | ALL } [ { OFFSET | , } <replaceable class="PARAMETER">start</replaceable> ]
36   </synopsis>
37   
38   <refsect2 id="R2-SQL-SELECT-1">
39    <refsect2info>
40     <date>2000-03-15</date>
41    </refsect2info>
42    <title>
43     Inputs
44    </title>
45
46    <para>
47     <variablelist>
48      <varlistentry>
49       <term><replaceable class="PARAMETER">expression</replaceable></term>
50       <listitem>
51        <para>
52         The name of a table's column or an expression.
53        </para>
54       </listitem>
55      </varlistentry>
56      
57      <varlistentry>
58       <term><replaceable class="PARAMETER">name</replaceable></term>
59       <listitem>
60        <para>
61         Specifies another name for a column or an expression using
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.
68        </para>
69       </listitem>
70      </varlistentry>
71      
72     <varlistentry>
73      <term>TEMPORARY</term>
74      <term>TEMP</term>
75      <listitem>
76       <para>
77         If TEMPORARY or TEMP is specified,
78         the table is created unique to this session, and is
79         automatically dropped on session exit.
80       </para>
81      </listitem>
82     </varlistentry>
83
84      <varlistentry>
85       <term><replaceable class="PARAMETER">new_table</replaceable></term>
86       <listitem>
87        <para>
88         If the INTO TABLE clause is specified, the result of the
89         query will be stored in a new table with the indicated
90         name.
91         The target table (<replaceable class="PARAMETER">new_table</replaceable>) will
92         be created automatically and must not exist before this command.
93         Refer to <command>SELECT INTO</command> for more information.
94
95         <note>
96          <para>
97           The <command>CREATE TABLE AS</command> statement will also
98           create a new  table from a select query.
99          </para>
100         </note>
101        </para>
102       </listitem>
103      </varlistentry>
104      
105      <varlistentry>
106       <term><replaceable class="PARAMETER">table</replaceable></term>
107       <listitem>
108        <para>
109         The name of an existing table referenced by the FROM clause.
110        </para>
111       </listitem>
112      </varlistentry>
113      
114      <varlistentry>
115       <term><replaceable class="PARAMETER">alias</replaceable></term>
116       <listitem>
117        <para>
118         An alternate name for the preceding
119         <replaceable class="PARAMETER">table</replaceable>.
120         It is used for brevity or to eliminate ambiguity for joins
121         within a single table.
122        </para>
123       </listitem>
124      </varlistentry>
125      
126      <varlistentry>
127       <term><replaceable class="PARAMETER">condition</replaceable></term>
128       <listitem>
129        <para>
130         A boolean expression giving a result of true or false.
131         See the WHERE clause.
132        </para>
133       </listitem>
134      </varlistentry>
135      
136      <varlistentry>
137       <term><replaceable class="PARAMETER">column</replaceable></term>
138       <listitem>
139        <para>
140         The name of a table's column.
141        </para>
142       </listitem>
143      </varlistentry>
144      
145      <varlistentry>
146       <term><replaceable class="PARAMETER">select</replaceable></term>
147       <listitem>
148        <para>
149         A select statement with all features except the ORDER BY and
150         LIMIT clauses.
151        </para>
152       </listitem>
153      </varlistentry>
154      
155     </variablelist>
156    </para>
157   </refsect2>
158   
159   <refsect2 id="R2-SQL-SELECT-2">
160    <refsect2info>
161     <date>1998-09-24</date>
162    </refsect2info>
163    <title>
164     Outputs
165    </title>
166    <para>
167
168     <variablelist>
169      <varlistentry>
170       <term>Rows</term>
171       <listitem>
172        <para>
173         The complete set of rows resulting from the query specification.
174        </para>
175       </listitem>
176      </varlistentry>
177
178      <varlistentry>
179       <term>
180        <returnvalue><replaceable>count</replaceable></returnvalue>
181       </term>
182       <listitem>
183        <para>
184         The count of rows returned by the query.
185        </para>
186       </listitem>
187      </varlistentry>
188     </variablelist>
189    </para>
190   </refsect2>
191  </refsynopsisdiv>
192
193  <refsect1 id="R1-SQL-SELECT-1">
194   <refsect1info>
195    <date>2000-03-15</date>
196   </refsect1info>
197   <title>
198    Description
199   </title>
200   <para>
201    <command>SELECT</command> will return rows from one or more tables.
202    Candidates for selection are rows which satisfy the WHERE condition;
203    if WHERE is omitted, all rows are candidates.
204    (See <xref linkend="sql-where" endterm="sql-where-title">.)
205   </para>
206   <para>
207   <command>ONLY</command> will eliminate rows from subclasses of the table.
208   This was previously the default result, and getting subclasses was
209   obtained by appending <command>*</command> to the table name.
210   The old behaviour is available via the command 
211   <command>SET EXAMINE_SUBCLASS TO 'on';</command>
212   </para>
213
214   <para>
215    <command>DISTINCT</command> will eliminate duplicate rows from the
216    result.
217    <command>ALL</command> (the default) will return all candidate rows,
218    including duplicates.
219   </para>
220
221   <para>
222    <command>DISTINCT ON</command> eliminates rows that match on all the
223    specified expressions, keeping only the first row of each set of
224    duplicates.  The DISTINCT ON expressions are interpreted using the
225    same rules as for ORDER BY items; see below.
226    Note that "the first row" of each set is unpredictable
227    unless <command>ORDER BY</command> is used to ensure that the desired
228    row appears first.  For example,
229    <programlisting>
230         SELECT DISTINCT ON (location) location, time, report
231         FROM weatherReports
232         ORDER BY location, time DESC;
233    </programlisting>
234    retrieves the most recent weather report for each location.  But if
235    we had not used ORDER BY to force descending order of time values
236    for each location, we'd have gotten a report of unpredictable age
237    for each location.
238   </para>
239
240   <para>
241    The GROUP BY clause allows a user to divide a table
242    into groups of rows that match on one or more values.
243    (See <xref linkend="sql-groupby" endterm="sql-groupby-title">.)
244   </para>
245
246   <para>
247    The HAVING clause allows selection of only those groups of rows
248    meeting the specified condition.
249    (See <xref linkend="sql-having" endterm="sql-having-title">.)
250   </para>
251    
252   <para>
253    The ORDER BY clause causes the returned rows to be sorted in a specified
254    order.  If ORDER BY is not given, the rows are returned in whatever order
255    the system finds cheapest to produce.
256    (See <xref linkend="sql-orderby-title" endterm="sql-orderby-title">.)
257   </para>
258    
259   <para>
260    The UNION operator allows the result to be the collection of rows
261    returned by the queries involved.
262    (See <xref linkend="sql-union" endterm="sql-union-title">.)
263   </para>
264    
265   <para>
266    The INTERSECT operator gives you the rows that are common to both queries.
267    (See <xref linkend="sql-intersect" endterm="sql-intersect-title">.)
268   </para>
269    
270   <para>
271    The EXCEPT operator gives you the rows returned by the first query but
272    not the second query.
273    (See <xref linkend="sql-except" endterm="sql-except-title">.)
274   </para>
275    
276   <para>
277    The FOR UPDATE clause allows the SELECT statement to perform 
278    exclusive locking of selected rows.
279   </para>
280    
281   <para>
282    The LIMIT clause allows a subset of the rows produced by the query
283    to be returned to the user.
284    (See <xref linkend="sql-limit" endterm="sql-limit-title">.)
285   </para>
286
287   <para>
288    You must have SELECT privilege to a table to read its values
289    (See the <command>GRANT</command>/<command>REVOKE</command> statements).
290   </para>
291    
292   <refsect2 id="SQL-WHERE">
293    <refsect2info>
294     <date>2000-03-15</date>
295    </refsect2info>
296    <title id="sql-where-title">
297     WHERE Clause
298    </title>
299
300    <para>
301     The optional WHERE condition has the general form:
302     
303     <synopsis>
304 WHERE <replaceable class="PARAMETER">boolean_expr</replaceable>
305     </synopsis>
306     
307     <replaceable class="PARAMETER">boolean_expr</replaceable>
308     can consist of any expression which evaluates to a boolean value.
309     In many cases, this expression will be
310
311     <synopsis>
312      <replaceable class="PARAMETER">expr</replaceable> <replaceable class="PARAMETER">cond_op</replaceable> <replaceable class="PARAMETER">expr</replaceable>
313     </synopsis>
314
315     or
316
317     <synopsis>
318      <replaceable class="PARAMETER">log_op</replaceable> <replaceable class="PARAMETER">expr</replaceable>
319     </synopsis>
320
321     where <replaceable class="PARAMETER">cond_op</replaceable>
322     can be one of: =, &lt;, &lt;=, &gt;, &gt;= or &lt;&gt;,
323     a conditional operator like ALL, ANY, IN, LIKE, or a
324     locally-defined operator, 
325     and <replaceable class="PARAMETER">log_op</replaceable> can be one 
326     of: AND, OR, NOT.
327     SELECT will ignore all rows for which the WHERE condition does not return
328     TRUE.
329    </para>
330   </refsect2>
331   
332   <refsect2 id="SQL-GROUPBY">
333    <refsect2info>
334     <date>2000-03-15</date>
335    </refsect2info>
336    <title id="sql-groupby-title">
337     GROUP BY Clause
338    </title>
339    <para>
340     GROUP BY specifies a grouped table derived by the application
341     of this clause:
342     <synopsis>
343 GROUP BY <replaceable class="PARAMETER">column</replaceable> [, ...]
344     </synopsis>
345    </para>
346
347    <para>
348     GROUP BY will condense into a single row all selected rows that share the
349     same values for the grouped columns.  Aggregate functions, if any,
350     are computed across all rows making up each group, producing a
351     separate value for each group (whereas without GROUP BY, an
352     aggregate produces a single value computed across all the selected
353     rows).  When GROUP BY is present, it is not valid for the SELECT
354     output expression(s) to refer to
355     ungrouped columns except within aggregate functions, since there
356     would be more than one possible value to return for an ungrouped column.
357    </para>
358
359    <para>
360     An item in GROUP BY can also be the name or ordinal number of an output
361     column (SELECT expression), or it can be an arbitrary expression formed
362     from input-column values.  In case of ambiguity, a GROUP BY name will
363     be interpreted as an input-column name rather than an output column name.
364    </para>
365   </refsect2>
366
367   <refsect2 id="SQL-HAVING">
368    <refsect2info>
369     <date>2000-03-15</date>
370    </refsect2info>
371    <title id="sql-having-title">
372     HAVING Clause
373    </title>
374    <para>
375     The optional HAVING condition has the general form:
376     
377     <synopsis>
378 HAVING <replaceable class="PARAMETER">cond_expr</replaceable>
379     </synopsis>
380     
381     where <replaceable class="PARAMETER">cond_expr</replaceable> is the same
382     as specified for the WHERE clause.
383    </para>
384     
385    <para>
386     HAVING specifies a grouped table derived by the elimination
387     of group rows that do not satisfy the
388     <replaceable class="PARAMETER">cond_expr</replaceable>.
389     HAVING is different from WHERE:
390     WHERE filters individual rows before application of GROUP BY,
391     while HAVING filters group rows created by GROUP BY.
392    </para>
393
394    <para>
395     Each column referenced in 
396     <replaceable class="PARAMETER">cond_expr</replaceable> shall unambiguously
397     reference a grouping column, unless the reference appears within an
398     aggregate function.
399    </para>
400   </refsect2>
401   
402   <refsect2 id="SQL-ORDERBY">
403    <refsect2info>
404     <date>2000-03-15</date>
405    </refsect2info>
406    <title id="sql-orderby-title">
407     ORDER BY Clause
408    </title>
409    <para>
410     <synopsis>
411 ORDER BY <replaceable class="PARAMETER">column</replaceable> [ ASC | DESC ] [, ...]
412     </synopsis></para>
413     
414    <para>
415     <replaceable class="PARAMETER">column</replaceable> can be either a
416     result column name or an ordinal number.
417    </para>
418    <para>
419     The ordinal numbers refers to the ordinal (left-to-right) position
420     of the result column. This feature makes it possible to define an ordering
421     on the basis of a column that does not have a proper name.
422     This is never absolutely necessary because it is always possible
423     to assign a name to a result column using the AS clause, e.g.:
424     <programlisting>
425 SELECT title, date_prod + 1 AS newlen FROM films ORDER BY newlen;
426     </programlisting></para>
427     
428    <para>
429     It is also possible to ORDER BY
430     arbitrary expressions (an extension to SQL92),
431     including fields that do not appear in the
432     SELECT result list.
433     Thus the following statement is legal:
434     <programlisting>
435 SELECT name FROM distributors ORDER BY code;
436     </programlisting>
437
438     Note that if an ORDER BY item is a simple name that matches both
439     a result column name and an input column name, ORDER BY will interpret
440     it as the result column name.  This is the opposite of the choice that
441     GROUP BY will make in the same situation.  This inconsistency is
442     mandated by the SQL92 standard.
443    </para>
444     
445    <para>
446     Optionally one may add the keyword DESC (descending)
447     or ASC (ascending) after each column name in the ORDER BY clause.
448     If not specified, ASC is assumed by default.  Alternatively, a
449     specific ordering operator name may be specified.  ASC is equivalent
450     to USING '&lt;' and DESC is equivalent to USING '&gt;'.
451    </para>
452   </refsect2>
453   
454   <refsect2 id="SQL-UNION">
455    <refsect2info>
456     <date>1998-09-24</date>
457    </refsect2info>
458    <title id="sql-union-title">
459     UNION Clause
460    </title>
461    <para>
462     <synopsis>
463 <replaceable class="PARAMETER">table_query</replaceable> UNION [ ALL ] <replaceable class="PARAMETER">table_query</replaceable>
464     [ ORDER BY <replaceable class="PARAMETER">column</replaceable> [ ASC | DESC ] [, ...] ]
465     </synopsis>
466
467     where
468     <replaceable class="PARAMETER">table_query</replaceable>
469     specifies any select expression without an ORDER BY or LIMIT clause.
470    </para>
471     
472    <para>
473     The UNION operator allows the result to be the collection of rows
474     returned by the queries involved.
475     The two SELECTs that represent the direct operands of the UNION must
476     produce the same number of columns, and corresponding columns must be
477     of compatible data types.
478    </para>
479     
480    <para>
481     By default, the result of UNION does not contain any duplicate rows
482     unless the ALL clause is specified.
483    </para>
484     
485    <para>
486     Multiple UNION operators in the same SELECT statement are
487     evaluated left to right.
488     Note that the ALL keyword is not global in nature, being 
489     applied only for the current pair of table results.
490    </para>
491
492   </refsect2>
493
494   <refsect2 id="SQL-INTERSECT">
495    <refsect2info>
496     <date>1998-09-24</date>
497    </refsect2info>
498    <title id="sql-intersect-title">
499     INTERSECT Clause
500    </title>
501    <para>
502     <synopsis>
503 <replaceable class="PARAMETER">table_query</replaceable> INTERSECT <replaceable class="PARAMETER">table_query</replaceable>
504     [ ORDER BY <replaceable class="PARAMETER">column</replaceable> [ ASC | DESC ] [, ...] ]
505     </synopsis>
506     
507     where
508     <replaceable class="PARAMETER">table_query</replaceable>
509     specifies any select expression without an ORDER BY or LIMIT clause.
510    </para>
511
512    <para>
513     The INTERSECT operator gives you the rows that are common to both queries.
514     The two SELECTs that represent the direct operands of the INTERSECT must
515     produce the same number of columns, and corresponding columns must be
516     of compatible data types.
517    </para>
518     
519    <para>
520     Multiple INTERSECT operators in the same SELECT statement are
521     evaluated left to right, unless parentheses dictate otherwise.
522    </para>
523   </refsect2>
524
525   <refsect2 id="SQL-EXCEPT">
526    <refsect2info>
527     <date>1998-09-24</date>
528    </refsect2info>
529    <title id="sql-except-title">
530     EXCEPT Clause
531    </title>
532    <para>
533     <synopsis>
534 <replaceable class="PARAMETER">table_query</replaceable> EXCEPT <replaceable class="PARAMETER">table_query</replaceable>
535      [ ORDER BY <replaceable class="PARAMETER">column</replaceable> [ ASC | DESC ] [, ...] ]
536     </synopsis>
537     
538     where
539     <replaceable class="PARAMETER">table_query</replaceable>
540     specifies any select expression without an ORDER BY or LIMIT clause.
541    </para>
542
543    <para>
544     The EXCEPT operator gives you the rows returned by the first query but
545     not the second query.
546     The two SELECTs that represent the direct operands of the EXCEPT must
547     produce the same number of columns, and corresponding columns must be
548     of compatible data types.
549    </para>
550
551    <para>
552     Multiple EXCEPT operators in the same SELECT statement are
553     evaluated left to right, unless parentheses dictate otherwise.
554    </para>
555   </refsect2>
556
557   <refsect2 id="SQL-LIMIT">
558    <refsect2info>
559     <date>2000-02-20</date>
560    </refsect2info>
561    <title id="sql-limit-title">
562     LIMIT Clause
563    </title>
564    <para>
565     <synopsis>
566     LIMIT { <replaceable class="PARAMETER">count</replaceable> | ALL } [ { OFFSET | , } <replaceable class="PARAMETER">start</replaceable> ]
567     OFFSET <replaceable class="PARAMETER">start</replaceable>
568     </synopsis>
569     
570     where
571     <replaceable class="PARAMETER">count</replaceable> specifies the
572     maximum number of rows to return, and
573     <replaceable class="PARAMETER">start</replaceable> specifies the
574     number of rows to skip before starting to return rows.
575    </para>
576
577    <para>
578     LIMIT allows you to retrieve just a portion of the rows that are generated
579     by the rest of the query.  If a limit count is given, no more than that
580     many rows will be returned.  If an offset is given, that many rows will
581     be skipped before starting to return rows.
582    </para>
583
584    <para>
585     When using LIMIT, it is a good idea to use an ORDER BY clause that
586     constrains the result rows into a unique order.  Otherwise you will get
587     an unpredictable subset of the query's rows --- you may be asking for
588     the tenth through twentieth rows, but tenth through twentieth in what
589     ordering?  You don't know what ordering, unless you specified ORDER BY.
590    </para>
591
592    <para>
593     As of <productname>Postgres</productname> 7.0, the
594     query optimizer takes LIMIT into account when generating a query plan,
595     so you are very likely to get different plans (yielding different row
596     orders) depending on what you give for LIMIT and OFFSET.  Thus, using
597     different LIMIT/OFFSET values to select different subsets of a query
598     result <emphasis>will give inconsistent results</emphasis> unless
599     you enforce a predictable result ordering with ORDER BY.  This is not
600     a bug; it is an inherent consequence of the fact that SQL does not
601     promise to deliver the results of a query in any particular order
602     unless ORDER BY is used to constrain the order.
603    </para>
604   </refsect2>
605  </refsect1>
606
607  <refsect1 id="R1-SQL-SELECT-2">
608   <title>
609    Usage
610   </title>
611
612   <para>
613    To join the table <literal>films</literal> with the table
614    <literal>distributors</literal>:
615
616    <programlisting>
617 SELECT f.title, f.did, d.name, f.date_prod, f.kind
618     FROM distributors d, films f
619     WHERE f.did = d.did
620
621            title           | did |   name           | date_prod  | kind
622 ---------------------------+-----+------------------+------------+----------
623  The Third Man             | 101 | British Lion     | 1949-12-23 | Drama
624  The African Queen         | 101 | British Lion     | 1951-08-11 | Romantic
625  Une Femme est une Femme   | 102 | Jean Luc Godard  | 1961-03-12 | Romantic
626  Vertigo                   | 103 | Paramount        | 1958-11-14 | Action
627  Becket                    | 103 | Paramount        | 1964-02-03 | Drama
628  48 Hrs                    | 103 | Paramount        | 1982-10-22 | Action
629  War and Peace             | 104 | Mosfilm          | 1967-02-12 | Drama
630  West Side Story           | 105 | United Artists   | 1961-01-03 | Musical
631  Bananas                   | 105 | United Artists   | 1971-07-13 | Comedy
632  Yojimbo                   | 106 | Toho             | 1961-06-16 | Drama
633  There's a Girl in my Soup | 107 | Columbia         | 1970-06-11 | Comedy
634  Taxi Driver               | 107 | Columbia         | 1975-05-15 | Action
635  Absence of Malice         | 107 | Columbia         | 1981-11-15 | Action
636  Storia di una donna       | 108 | Westward         | 1970-08-15 | Romantic
637  The King and I            | 109 | 20th Century Fox | 1956-08-11 | Musical
638  Das Boot                  | 110 | Bavaria Atelier  | 1981-11-11 | Drama
639  Bed Knobs and Broomsticks | 111 | Walt Disney      |            | Musical
640 (17 rows)
641 </programlisting>
642   </para>
643
644   <para>
645    To sum the column <literal>len</literal> of all films and group
646    the results by <literal>kind</literal>:
647
648 <programlisting>
649 SELECT kind, SUM(len) AS total FROM films GROUP BY kind;
650
651    kind   | total
652 ----------+-------
653  Action   | 07:34
654  Comedy   | 02:58
655  Drama    | 14:28
656  Musical  | 06:42
657  Romantic | 04:38
658 (5 rows)
659 </programlisting>
660   </para>
661
662   <para>
663    To sum the column <literal>len</literal> of all films, group
664    the results by <literal>kind</literal> and show those group totals
665    that are less than 5 hours:
666
667 <programlisting>
668 SELECT kind, SUM(len) AS total
669     FROM films
670     GROUP BY kind
671     HAVING SUM(len) < INTERVAL '5 hour';
672
673  kind     | total
674 ----------+-------
675  Comedy   | 02:58
676  Romantic | 04:38
677 (2 rows)
678 </programlisting>
679   </para>
680
681   <para>
682    The following two examples are identical ways of sorting the individual
683    results according to the contents of the second column
684    (<literal>name</literal>):
685
686    <programlisting>
687 SELECT * FROM distributors ORDER BY name;
688 SELECT * FROM distributors ORDER BY 2;
689
690  did |       name
691 -----+------------------
692  109 | 20th Century Fox
693  110 | Bavaria Atelier
694  101 | British Lion
695  107 | Columbia
696  102 | Jean Luc Godard
697  113 | Luso films
698  104 | Mosfilm
699  103 | Paramount
700  106 | Toho
701  105 | United Artists
702  111 | Walt Disney
703  112 | Warner Bros.
704  108 | Westward
705 (13 rows)
706 </programlisting>
707   </para>
708
709   <para>
710    This example shows how to obtain the union of the tables
711    <literal>distributors</literal> and
712    <literal>actors</literal>, restricting the results to those that begin
713    with letter W in each table.  Only distinct rows are wanted, so the
714    ALL keyword is omitted:
715
716 <programlisting>
717 distributors:               actors:
718  did |     name              id |     name
719 -----+--------------        ----+----------------
720  108 | Westward               1 | Woody Allen
721  111 | Walt Disney            2 | Warren Beatty
722  112 | Warner Bros.           3 | Walter Matthau
723  ...                         ...
724
725 SELECT distributors.name
726     FROM   distributors
727     WHERE  distributors.name LIKE 'W%'
728 UNION
729 SELECT actors.name
730     FROM   actors
731     WHERE  actors.name LIKE 'W%'
732
733       name
734 ----------------
735  Walt Disney
736  Walter Matthau
737  Warner Bros.
738  Warren Beatty
739  Westward
740  Woody Allen
741 </programlisting>
742   </para>
743  </refsect1>
744  
745  <refsect1 id="R1-SQL-SELECT-3">
746   <title>
747    Compatibility
748   </title>
749   
750   <refsect2 id="R2-SQL-SELECT-4">
751    <refsect2info>
752     <date>1998-09-24</date>
753    </refsect2info>
754    <title>
755     <acronym>Extensions</acronym>
756    </title>
757
758    <para>
759 <productname>Postgres</productname> allows one to omit 
760 the <command>FROM</command> clause from a query. This feature
761 was retained from the original PostQuel query language:
762   <programlisting>
763 SELECT distributors.* WHERE name = 'Westwood';
764
765  did | name
766 -----+----------
767  108 | Westward
768   </programlisting>
769    </para>
770   </refsect2>
771
772   <refsect2 id="R2-SQL-SELECT-5">
773    <refsect2info>
774     <date>1998-09-24</date>
775    </refsect2info>
776    <title>
777     <acronym>SQL92</acronym>
778    </title>
779    <para>
780    </para>
781    
782    <refsect3 id="R3-SQL-SELECT-1">
783     <refsect3info>
784      <date>1998-04-15</date>
785     </refsect3info>
786     <title>
787      SELECT Clause
788     </title>
789     <para>
790      In the <acronym>SQL92</acronym> standard, the optional keyword "AS"
791      is just noise and can be 
792      omitted without affecting the meaning.
793      The <productname>Postgres</productname> parser requires this keyword when
794      renaming columns because the type extensibility features lead to
795      parsing ambiguities
796      in this context.</para>
797      
798     <para>
799      The DISTINCT ON phrase is not part of <acronym>SQL92</acronym>.
800      Nor are LIMIT and OFFSET.
801     </para>
802      
803     <para>
804      In <acronym>SQL92</acronym>, an ORDER BY clause may only use result
805      column names or numbers, while a GROUP BY clause may only use input
806      column names.
807      <productname>Postgres</productname> extends each of these clauses to
808      allow the other choice as well (but it uses the standard's interpretation
809      if there is ambiguity).
810      <productname>Postgres</productname> also allows both clauses to specify
811      arbitrary expressions.  Note that names appearing in an expression will
812      always be taken as input-column names, not as result-column names.
813     </para>
814    </refsect3>
815
816    <refsect3 id="R3-SQL-UNION-1">
817     <refsect3info>
818      <date>1998-09-24</date>
819     </refsect3info>
820     <title>
821      UNION Clause
822     </title>
823     <para>
824      The <acronym>SQL92</acronym> syntax for UNION allows an
825      additional CORRESPONDING BY clause:
826      <synopsis> 
827 <replaceable class="PARAMETER">table_query</replaceable> UNION [ALL]
828     [CORRESPONDING [BY (<replaceable class="PARAMETER">column</replaceable> [,...])]]
829     <replaceable class="PARAMETER">table_query</replaceable>
830      </synopsis></para>
831
832     <para>
833      The CORRESPONDING BY clause is not supported by
834      <productname>Postgres</productname>.
835     </para>
836    </refsect3>
837    
838   </refsect2>
839  </refsect1>
840 </refentry>
841
842 <!-- Keep this comment at the end of the file
843 Local variables:
844 mode: sgml
845 sgml-omittag:nil
846 sgml-shorttag:t
847 sgml-minimize-attributes:nil
848 sgml-always-quote-attributes:t
849 sgml-indent-step:1
850 sgml-indent-data:t
851 sgml-parent-document:nil
852 sgml-default-dtd-file:"../reference.ced"
853 sgml-exposed-tags:nil
854 sgml-local-catalogs:"/usr/lib/sgml/catalog"
855 sgml-local-ecat-files:nil
856 End:
857 -->