select.sgml 22.4 KB
Newer Older
1 2 3
<refentry id="SQL-SELECT">
 <refmeta>
  <refentrytitle>
4
SELECT
5 6 7 8 9
  </refentrytitle>
  <refmiscinfo>SQL - Language Statements</refmiscinfo>
 </refmeta>
 <refnamediv>
  <refname>
10
SELECT
11 12 13 14 15 16
  </refname>
  <refpurpose>
   Retrieve rows from a table or view.
  </refpurpose></refnamediv>
 <refsynopsisdiv>
  <refsynopsisdivinfo>
17
   <date>1998-09-24</date>
18 19
  </refsynopsisdivinfo>
  <synopsis>
20
SELECT [ALL|DISTINCT [ON <replaceable class="PARAMETER">column</replaceable>] ]
21
    <replaceable class="PARAMETER">expression</replaceable> [ AS <replaceable class="PARAMETER">name</replaceable> ] [, ...]
22
    [ INTO [TEMP] [TABLE] <replaceable class="PARAMETER">new_table</replaceable> ]
23 24 25 26
    [ FROM <replaceable class="PARAMETER">table</replaceable> [<replaceable class="PARAMETER">alias</replaceable> ] [, ...] ]
    [ WHERE <replaceable class="PARAMETER">condition</replaceable> ]
    [ GROUP BY <replaceable class="PARAMETER">column</replaceable> [, ...] ]
    [ HAVING <replaceable class="PARAMETER">condition</replaceable> [, ...] ]
27
    [ { UNION [ALL] | INTERSECT | EXCEPT } <replaceable class="PARAMETER">select</replaceable> ]
28
    [ ORDER BY <replaceable class="PARAMETER">column</replaceable> [ ASC | DESC ] [, ...] ]
29 30 31 32
  </synopsis>
  
  <refsect2 id="R2-SQL-SELECT-1">
   <refsect2info>
33
    <date>1998-09-24</date>
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
   </refsect2info>
   <title>
    Inputs
   </title>
   
   <para>
    <variablelist>
     <varlistentry>
      <term>
       <replaceable class="PARAMETER">expression</replaceable>
      </term>
      <listitem>
       <para>
	The name of a table's column or an expression.
       </para>
      </listitem>
     </varlistentry>
     
     <varlistentry>
      <term>
       <replaceable class="PARAMETER">name</replaceable>
      </term>
      <listitem>
       <para>
	Specifies another name for a column or an expression using
	the AS clause. <replaceable class="PARAMETER">name</replaceable>
	cannot be used in the WHERE
	condition. It can, however, be referenced in associated
	ORDER BY or GROUP BY clauses.
       </para>
      </listitem>
     </varlistentry>
     
67 68 69 70 71 72 73 74 75 76 77 78
    <VARLISTENTRY>
     <TERM>
     TEMP
     </TERM>
     <LISTITEM>
      <PARA>
	The table is created unique to this session, and is
	automatically dropped on session exit.
      </PARA>
     </LISTITEM>
    </VARLISTENTRY>

79 80
     <varlistentry>
      <term>
81
       <replaceable class="PARAMETER">new_table</replaceable>
82 83 84 85 86 87
      </term>
      <listitem>
       <para>
	If the INTO TABLE clause is specified, the result of the
	query will be stored in another table with the indicated
	name.
T
Thomas G. Lockhart 已提交
88 89 90 91
	The target table (<replaceable class="PARAMETER">new_table</replaceable>) will
	be created automatically and should not exist before this command.
        Refer to <command>SELECT INTO</command> for more information.

92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
	<note>
	 <para>
	  The <command>CREATE TABLE AS</command> statement will also
	  create a new  table from a select query.
	 </para>
	</note>
       </para>
      </listitem>
     </varlistentry>
     
     <varlistentry>
      <term>
       <replaceable class="PARAMETER">table</replaceable>
      </term>
      <listitem>
       <para>
	The name of an existing table referenced by the FROM clause.
       </para>
      </listitem>
     </varlistentry>
     
     <varlistentry>
      <term>
       <replaceable class="PARAMETER">alias</replaceable>
      </term>
      <listitem>
       <para>
	An alternate name for the preceding
	<replaceable class="PARAMETER">table</replaceable>.
	It is used for brevity or to eliminate ambiguity for joins
	within a single table.
       </para>
      </listitem>
     </varlistentry>
     
     <varlistentry>
      <term>
       <replaceable class="PARAMETER">condition</replaceable>
      </term>
      <listitem>
       <para>
	A boolean expression giving a result of true or false.
	See the WHERE clause.
       </para>
      </listitem>
     </varlistentry>
     
     <varlistentry>
      <term>
       <replaceable class="PARAMETER">column</replaceable>
      </term>
      <listitem>
       <para>
	The name of a table's column.
       </para>
      </listitem>
     </varlistentry>
     
     <varlistentry>
      <term>
       <replaceable class="PARAMETER">select</replaceable>
      </term>
      <listitem>
       <para>
	A select statement with all features except the ORDER BY clause.
       </para>
      </listitem>
     </varlistentry>
     
    </variablelist>
   </para>
  </refsect2>
  
  <refsect2 id="R2-SQL-SELECT-2">
   <refsect2info>
167
    <date>1998-09-24</date>
168 169 170 171
   </refsect2info>
   <title>
    Outputs
   </title>
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
   <para>
	
	<variablelist>
	 <varlistentry>
	  <term>
	   Rows
	  </term>
	  <listitem>
	   <para>
		The complete set of rows resulting from the query specification.
	   </para>
	  </listitem>
	 </varlistentry>
	 
	 <varlistentry>
	  <term>
	   <returnvalue><replaceable>count</replaceable></returnvalue>
	  </term>
	  <listitem>
	   <para>
		The count of rows returned by the query.
	   </para>
	  </listitem>
	 </varlistentry>
	</variablelist>
   </para>
198 199 200 201 202
  </refsect2>
 </refsynopsisdiv>
 
 <refsect1 id="R1-SQL-SELECT-1">
  <refsect1info>
203
   <date>1998-09-24</date>
204 205 206 207 208
  </refsect1info>
  <title>
   Description
  </title>
  <para>
209 210 211 212 213 214 215 216
   <command>SELECT</command> will return rows from one or more tables.
   Candidates for selection are rows which satisfy the WHERE condition;
   if WHERE is omitted, all rows are candidates.</para>
  <para>
   <command>DISTINCT</command> will eliminate all duplicate rows from the
   selection.
   <command>DISTINCT ON <replaceable class="PARAMETER">column</replaceable></command> will eliminate all duplicates in the specified column; this is
equivalent to using <command>GROUP BY <replaceable class="PARAMETER">column</replaceable></command>.  <command>ALL</command> will return all candidate rows,
217 218
including duplicates.</para>

219
  <para>
220
   The GROUP BY clause allows a user to divide a table
221 222 223
   conceptually into groups. (See GROUP BY clause).</para>
   
  <para>
224 225
   The HAVING clause specifies a grouped table derived by the
   elimination of groups from the result of the previously
226 227 228
   specified clause. (See HAVING clause).</para>
   
  <para>
229 230
   The ORDER BY clause allows a user to specify that he/she
   wishes the rows sorted according to the ASCending or 
231 232 233
   DESCending mode operator. (See ORDER BY clause)</para>
   
  <para>
234 235 236 237 238 239 240 241 242 243
   The UNION clause allows the result to be the collection of rows
   returned by the queries involved. (See UNION clause).</para>
   
  <para>
   The INTERSECT give you the rows that are common to both queries.
   (See INTERSECT clause).</para>
   
  <para>
   The EXCEPT give you the rows in the upper query not in the lower query.
   (See EXCEPT clause).</para>
244 245
   
  <para>
246
   You must have SELECT privilege to a table to read its values
247 248
   (See <command>GRANT</command>/<command>REVOKE</command> statements).
</para>
249 250 251
   
  <refsect2 id="R2-SQL-WHERE-2">
   <refsect2info>
252
    <date>1998-09-24</date>
253 254
   </refsect2info>
   <title>
255
    WHERE Clause
256 257 258 259 260
   </title>
   <para>
    The optional WHERE condition has the general form:
    
    <synopsis>
261
WHERE <replaceable class="PARAMETER">expr</replaceable> <replaceable class="PARAMETER">cond_op</replaceable> <replaceable class="PARAMETER">expr</replaceable> [ <replaceable class="PARAMETER">log_op</replaceable> ... ]
262 263 264
    </synopsis>
    
    where <replaceable class="PARAMETER">cond_op</replaceable> can be
265 266 267
    one of: =, &lt;, &lt;=, &gt;, &gt;= or &lt;&gt;,
    a conditional operator like ALL, ANY, IN, LIKE, et cetera or a
    locally-defined operator, 
268 269 270 271 272 273 274 275 276 277
    and <replaceable class="PARAMETER">log_op</replaceable> can be one 
    of: AND, OR, NOT.
    The comparison returns either TRUE or FALSE and all
    instances will be discarded
    if the expression evaluates to FALSE.
   </para>
  </refsect2>
  
  <refsect2 id="R2-SQL-GROUPBY-2">
   <refsect2info>
278
    <date>1998-09-24</date>
279 280
   </refsect2info>
   <title>
281
    GROUP BY Clause
282 283
   </title>
   <para>
284
    GROUP BY specifies a grouped table derived by the application
285
    of this clause:
286
    <synopsis>
287
GROUP BY <replaceable class="PARAMETER">column</replaceable> [, ...]
288 289 290 291 292 293 294
    </synopsis></para>
    <para>
     GROUP BY will condense into a single row all rows that share the same values for the
     grouped columns; aggregates return values derived from all rows that make up the group.  The value returned for an ungrouped
     and unaggregated column is dependent on the order in which rows happen to be read from the database.
    </para>
</refsect2>
295 296 297
    
  <refsect2 id="R2-SQL-HAVING-2">
   <refsect2info>
298
    <date>1998-09-24</date>
299 300
   </refsect2info>
   <title>
301
    HAVING Clause
302 303 304 305 306 307 308 309 310 311 312 313
   </title>
   <para>
    The optional HAVING condition has the general form:
    
    <synopsis>
HAVING <replaceable class="PARAMETER">cond_expr</replaceable>
    </synopsis>
    
    where <replaceable class="PARAMETER">cond_expr</replaceable> is the same
    as specified for the WHERE clause.</para>
    
   <para>
314 315
    HAVING specifies a grouped table derived by the elimination
    of groups from the result of the previously specified clause
316 317 318
    that do not meet the <replaceable class="PARAMETER">cond_expr</replaceable>.</para>
    
   <para>
319 320
    Each column referenced in 
<replaceable class="PARAMETER">cond_expr</replaceable> shall unambiguously
321
    reference a grouping column.
322 323 324 325 326
   </para>
  </refsect2>
  
  <refsect2 id="R2-SQL-ORDERBYCLAUSE-2">
   <refsect2info>
327
    <date>1998-09-24</date>
328 329
   </refsect2info>
   <title>
330
    ORDER BY Clause
331 332 333
   </title>
   <para>
    <synopsis>
334
ORDER BY <replaceable class="PARAMETER">column</replaceable> [ ASC | DESC ] [, ...]
335 336 337 338 339 340
    </synopsis></para>
    
   <para>
    <replaceable class="PARAMETER">column</replaceable> can be either a column
    name or an ordinal number.</para>
   <para>
341 342 343
    The ordinal numbers refers to the ordinal (left-to-right) position
    of the column. This feature makes it possible to define an ordering
    on the basis of a column that does not have a proper name.
344 345
    This is never absolutely necessary because it is always possible
    assign a name
346
    to a calculated column using the AS clause, e.g.:
347
    <programlisting>
348
SELECT title, date_prod + 1 AS newlen FROM films ORDER BY newlen;
349 350 351
    </programlisting></para>
    
   <para>
352 353
    From release 6.4 of PostgreSQL, the columns in the ORDER BY clause do not need to appear in the SELECT clause.
    Thus the following statement is now legal:
354
    <programlisting>
355
SELECT name FROM distributors ORDER BY code;
356 357 358
    </programlisting></para>
    
   <para>
359 360
    Optionally one may add the keyword DESC (descending)
    or ASC (ascending) after each column name in the ORDER BY clause.
361 362 363 364 365
    If not specified, ASC is assumed by default.</para>
  </refsect2>
  
  <refsect2 id="R2-SQL-UNION-2">
   <refsect2info>
366
    <date>1998-09-24</date>
367 368
   </refsect2info>
   <title>
369
    UNION Clause
370 371 372
   </title>
   <para>
    <synopsis>
373 374
<replaceable class="PARAMETER">table_query</replaceable> UNION [ ALL ] <replaceable class="PARAMETER">table_query</replaceable>
     [ ORDER BY <replaceable class="PARAMETER">column</replaceable> [ ASC | DESC ] [, ...] ]
375 376 377 378 379 380 381
    </synopsis>
    
    where
    <replaceable class="PARAMETER">table_query</replaceable>
    specifies any select expression without an ORDER BY clause.</para>
    
   <para>
382 383
    The UNION clause allows the result to be the collection of rows
    returned by the queries involved. (See UNION clause).
384 385 386 387 388 389 390 391 392
    The two tables that represent the direct operands of the UNION must
    have the same number of columns, and corresponding columns must be
    of compatible data types.</para>
    
   <para>
    By default, the result of UNION does not contain any duplicate rows
    unless the ALL clause is specified.</para>
    
   <para>
393
    Multiple UNION operators in the same SELECT statement are
394
    evaluated left to right.
395
    Note that the ALL keyword is not global in nature, being 
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
    applied only for the current pair of table results.</para>
    
  </refsect2>

  <refsect2 id="R2-SQL-INTERSECT-2">
   <refsect2info>
    <date>1998-09-24</date>
   </refsect2info>
   <title>
    INTERSECT Clause
   </title>
   <para>
    <synopsis>
<replaceable class="PARAMETER">table_query</replaceable> INTERSECT <replaceable class="PARAMETER">table_query</replaceable>
     [ ORDER BY <replaceable class="PARAMETER">column</replaceable> [ ASC | DESC ] [, ...] ]
    </synopsis>
    
    where
    <replaceable class="PARAMETER">table_query</replaceable>
    specifies any select expression without an ORDER BY clause.</para>
    
   <para>
    The INTERSECT clause allows the result to be all rows that are 
    common to the involved queries.  (See INTERSECT clause).
    The two tables that represent the direct operands of the INTERSECT must
    have the same number of columns, and corresponding columns must be
    of compatible data types.</para>
    
   <para>
    Multiple INTERSECT operators in the same SELECT statement are
    evaluated left to right.
    </para>
    
  </refsect2>

  <refsect2 id="R2-SQL-EXCEPT-2">
   <refsect2info>
    <date>1998-09-24</date>
   </refsect2info>
   <title>
    EXCEPT Clause
   </title>
   <para>
    <synopsis>
<replaceable class="PARAMETER">table_query</replaceable> EXCEPT <replaceable class="PARAMETER">table_query</replaceable>
     [ ORDER BY <replaceable class="PARAMETER">column</replaceable> [ ASC | DESC ] [, ...] ]
    </synopsis>
443
    
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
    where
    <replaceable class="PARAMETER">table_query</replaceable>
    specifies any select expression without an ORDER BY clause.</para>
    
   <para>
    The EXCEPT clause allows the result to be rows from the upper query
    that are not in the lower query.  (See EXCEPT clause).
    The two tables that represent the direct operands of the EXCEPT must
    have the same number of columns, and corresponding columns must be
    of compatible data types.</para>
    
   <para>
    Multiple EXCEPT operators in the same SELECT statement are
    evaluated left to right.
    </para>
    
  </refsect2>

</refsect1>
463 464 465 466 467 468 469 470 471 472
  
 <refsect1 id="R1-SQL-SELECT-2">
  <title>
   Usage
  </title>
  <para>
   To join the table <literal>films</literal> with the table
   <literal>distributors</literal>:
  </para>
  <programlisting>
473 474 475
SELECT f.title, f.did, d.name, f.date_prod, f.kind
    FROM distributors d, films f
    WHERE f.did = d.did
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495

    title                    |did|name            | date_prod|kind
    -------------------------+---+----------------+----------+----------
    The Third Man            |101|British Lion    |1949-12-23|Drama
    The African Queen        |101|British Lion    |1951-08-11|Romantic
    Une Femme est une Femme  |102|Jean Luc Godard |1961-03-12|Romantic
    Vertigo                  |103|Paramount       |1958-11-14|Action
    Becket                   |103|Paramount       |1964-02-03|Drama
    48 Hrs                   |103|Paramount       |1982-10-22|Action
    War and Peace            |104|Mosfilm         |1967-02-12|Drama
    West Side Story          |105|United Artists  |1961-01-03|Musical
    Bananas                  |105|United Artists  |1971-07-13|Comedy
    Yojimbo                  |106|Toho            |1961-06-16|Drama
    There's a Girl in my Soup|107|Columbia        |1970-06-11|Comedy
    Taxi Driver              |107|Columbia        |1975-05-15|Action
    Absence of Malice        |107|Columbia        |1981-11-15|Action
    Storia di una donna      |108|Westward        |1970-08-15|Romantic
    The King and I           |109|20th Century Fox|1956-08-11|Musical
    Das Boot                 |110|Bavaria Atelier |1981-11-11|Drama
    Bed Knobs and Broomsticks|111|Walt Disney     |          |Musical
496 497 498
  </programlisting>
  <para>
   To sum the column <literal>len</literal> of all films and group
499
   the results by <literal>kind</literal>:
500 501
  </para>
  <programlisting>
502
SELECT kind, SUM(len) AS total FROM films GROUP BY kind;
503 504 505 506 507 508 509 510

    kind      |total
    ----------+------
    Action    | 07:34
    Comedy    | 02:58
    Drama     | 14:28
    Musical   | 06:42
    Romantic  | 04:38
511 512 513 514
  </programlisting>

  <para>
   To sum the column <literal>len</literal> of all films, group
515
   the results by <literal>kind</literal> and show those group totals
516 517 518
   that are less than 5 hours:
  </para>
  <programlisting>
519 520 521 522
SELECT kind, SUM(len) AS total
    FROM films
    GROUP BY kind
    HAVING SUM(len) < INTERVAL '5 hour';
523 524 525 526 527

    kind      |total
    ----------+------
    Comedy    | 02:58
    Romantic  | 04:38
528 529 530 531 532 533 534
  </programlisting>
  <para>
    The following two examples are identical ways of sorting the individual
   results according to the contents of the second column
   (<literal>name</literal>):
  </para>
  <programlisting>
535 536
SELECT * FROM distributors ORDER BY name;
SELECT * FROM distributors ORDER BY 2;
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552

    did|name
    ---+----------------
    109|20th Century Fox
    110|Bavaria Atelier
    101|British Lion
    107|Columbia
    102|Jean Luc Godard
    113|Luso films
    104|Mosfilm
    103|Paramount
    106|Toho
    105|United Artists
    111|Walt Disney
    112|Warner Bros.
    108|Westward
553 554 555 556 557 558 559 560 561 562 563
  </programlisting>

  <para>
   This example shows how to obtain the union of the tables
   <literal>distributors</literal> and
   <literal>actors</literal>, restricting the results to those that begin
   with letter W in each table.  Only distinct rows are to be used, so the
   ALL keyword is omitted:
  </para>
  <programlisting>
    --        distributors:                actors:
564 565 566 567 568 569
    --        did|name                     id|name
    --        ---+------------             --+--------------
    --        108|Westward                  1|Woody Allen
    --        111|Walt Disney               2|Warren Beatty
    --        112|Warner Bros.              3|Walter Matthau
    --        ...                           ...
570

571
SELECT distributors.name
572 573
    FROM   distributors
    WHERE  distributors.name LIKE 'W%'
574 575 576 577
UNION
SELECT actors.name
    FROM   actors
    WHERE  actors.name LIKE 'W%'
578 579 580 581 582 583 584 585 586

    name
    --------------
    Walt Disney
    Walter Matthau
    Warner Bros.
    Warren Beatty
    Westward
    Woody Allen
587 588 589 590 591 592 593 594 595 596 597 598
  </programlisting>
  
 </refsect1>
 
 <refsect1 id="R1-SQL-SELECT-3">
  <title>
   Compatibility
  </title>
  <para>
  </para>
  
  <refsect2 id="R2-SQL-SELECT-4">
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
   <refsect2info>
    <date>1998-09-24</date>
   </refsect2info>
   <title>
    <acronym>Extensions</acronym>
   </title>

   <para>
<productname>Postgres</productname> allows one to omit 
the <command>FROM</command> clause from a query. This feature
was retained from the original PostQuel query language:
  <programlisting>
SELECT distributors.* WHERE name = 'Westwood';

    did|name
    ---+----------------
    108|Westward
  </programlisting>
   </para>
618
  </refsect2>
619 620

  <refsect2 id="R2-SQL-SELECT-5">
621
   <refsect2info>
622
    <date>1998-09-24</date>
623 624 625 626 627 628 629 630 631 632 633 634
   </refsect2info>
   <title>
    <acronym>SQL92</acronym>
   </title>
   <para>
   </para>
   
   <refsect3 id="R3-SQL-SELECT-1">
    <refsect3info>
     <date>1998-04-15</date>
    </refsect3info>
    <title>
635
     SELECT Clause
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
    </title>
    <para>
     In the <acronym>SQL92</acronym> standard, the optional keyword "AS"
     is just noise and can be 
     omitted without affecting the meaning.
     The <productname>Postgres</productname> parser requires this keyword when
     renaming columns because the type extensibility features lead to
     parsing ambiguities
     in this context.</para>
     
    <para>
     In the <acronym>SQL92</acronym> standard, the new column name 
     specified in an
     "AS" clause may be referenced in GROUP BY and HAVING clauses.
     This is not currently
     allowed in <productname>Postgres</productname>.
    </para>
653 654 655 656
     
    <para>
     The DISTINCT ON phrase is not part of <acronym>SQL92</acronym>.
    </para>
657 658 659 660
   </refsect3>

   <refsect3 id="R3-SQL-UNION-1">
    <refsect3info>
661
     <date>1998-09-24</date>
662 663
    </refsect3info>
    <title>
664
     UNION Clause
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
    </title>
    <para>
     The <acronym>SQL92</acronym> syntax for UNION allows an
     additional CORRESPONDING BY clause:
     <synopsis> 
<replaceable class="PARAMETER">table_query</replaceable> UNION [ALL]
    [CORRESPONDING [BY (<replaceable class="PARAMETER">column</replaceable> [,...])]]
    <replaceable class="PARAMETER">table_query</replaceable>
     </synopsis></para>

    <para>
     The CORRESPONDING BY clause is not supported by
     <productname>Postgres</productname>.
    </para>
   </refsect3>
   
  </refsect2>
 </refsect1>
</refentry>

685 686 687
<refentry id="SQL-SELECTINTO">
 <refmeta>
  <refentrytitle>
688
SELECT INTO
689 690 691 692 693
  </refentrytitle>
  <refmiscinfo>SQL - Language Statements</refmiscinfo>
 </refmeta>
 <refnamediv>
  <refname>
694
SELECT INTO
695 696
  </refname>
  <refpurpose>
697
Create a new table from an existing table or view
698 699 700 701 702 703 704
  </refpurpose></refnamediv>
 <refsynopsisdiv>
  <refsynopsisdivinfo>
   <date>1998-09-22</date>
  </refsynopsisdivinfo>
  <synopsis>
SELECT [ ALL | DISTINCT ] <replaceable class="PARAMETER">expression</replaceable> [ AS <replaceable class="PARAMETER">name</replaceable> ] [, ...]
705
    INTO [TEMP] [ TABLE ] <replaceable class="PARAMETER">new_table</replaceable> ]
706 707 708 709
    [ FROM <replaceable class="PARAMETER">table</replaceable> [<replaceable class="PARAMETER">alias</replaceable>] [, ...] ]
    [ WHERE <replaceable class="PARAMETER">condition</replaceable> ]
    [ GROUP BY <replaceable class="PARAMETER">column</replaceable> [, ...] ]
    [ HAVING <replaceable class="PARAMETER">condition</replaceable> [, ...] ]
710
    [ { UNION [ALL] | INTERSECT | EXCEPT } <replaceable class="PARAMETER">select</replaceable>]
711 712 713 714 715 716 717 718 719 720 721 722
    [ ORDER BY <replaceable class="PARAMETER">column</replaceable> [ ASC | DESC ] [, ...] ]
  </synopsis>
  
  <refsect2 id="R2-SQL-SELECTINTO-1">
   <refsect2info>
    <date>1998-09-22</date>
   </refsect2info>
   <title>
    Inputs
   </title>
   <para>
All input fields are described in detail for SELECT.
723 724
   </para>
  </refsect2>
725 726 727 728 729 730 731 732 733 734

  <refsect2 id="R2-SQL-SELECTINTO-2">
   <refsect2info>
    <date>1998-09-22</date>
   </refsect2info>
   <title>
    Outputs
   </title>
   <para>
All output fields are described in detail for SELECT.
735 736 737
   </para>
  </refsect2>
 </refsynopsisdiv>
738

739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
 <refsect1 id="R1-SQL-SELECTINTO-1">
  <refsect1info>
   <date>1998-09-22</date>
  </refsect1info>
  <title>
   Description
  </title>
  <para>
   SELECT INTO creates a new table from the results of a query. Typically, this
   query draws data from an existing table, but any SQL query is allowed.
   <note>
	<para>
	 CREATE TABLE AS is functionally equivalent to the SELECT INTO command.
	</para>
   </note>
  </para>
755 756 757
 </refsect1>
</refentry>

758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
sgml-parent-document:nil
sgml-default-dtd-file:"../reference.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:"/usr/lib/sgml/catalog"
sgml-local-ecat-files:nil
End:
773
-->