select.sgml 18.7 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 22 23 24 25 26 27 28
    <replaceable class="PARAMETER">expression</replaceable> [ AS <replaceable class="PARAMETER">name</replaceable> ] [, ...]
    [ INTO [TABLE] <replaceable class="PARAMETER">new_table</replaceable> ]
    [ 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> [, ...] ]
    [ UNION [ALL] <replaceable class="PARAMETER">select</replaceable> ]
    [ 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 67 68
   </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>
     
     <varlistentry>
      <term>
69
       <replaceable class="PARAMETER">new_table</replaceable>
70 71 72 73 74 75
      </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.
76
	If <replaceable class="PARAMETER">new_table</replaceable> does
77
	not exist, it will be created automatically.
78
	Refer to <command>SELECT INTO</command> for more information.
79 80 81 82 83 84 85 86 87 88 89 90 91 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
	<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>
154
    <date>1998-09-24</date>
155 156 157 158
   </refsect2info>
   <title>
    Outputs
   </title>
159 160
<para>

161 162 163 164 165 166 167 168 169 170 171 172
   <variablelist>
    <varlistentry>
     <term>
      Rows
     </term>
     <listitem>
      <para>
       The complete set of rows resulting from the query specification.
      </para>
     </listitem>
    </varlistentry>
    
173 174 175
    <varlistentry>
     <term>
      <returnvalue><replaceable>count</replaceable></returnvalue>
176 177 178 179
     </term>
     <listitem>
      <para>
       The count of rows returned by the query.
180
   </variablelist>
181 182 183 184 185 186
   
  </refsect2>
 </refsynopsisdiv>
 
 <refsect1 id="R1-SQL-SELECT-1">
  <refsect1info>
187
   <date>1998-09-24</date>
188 189 190 191 192
  </refsect1info>
  <title>
   Description
  </title>
  <para>
193 194
   <command>SELECT</command> will get all rows which satisfy the
 WHERE condition
195 196 197
   or all rows of a table if WHERE is omitted.</para>
   
  <para>
198
   The GROUP BY clause allows a user to divide a table
199 200 201
   conceptually into groups. (See GROUP BY clause).</para>
   
  <para>
202 203
   The HAVING clause specifies a grouped table derived by the
   elimination of groups from the result of the previously
204 205 206
   specified clause. (See HAVING clause).</para>
   
  <para>
207 208
   The ORDER BY clause allows a user to specify that he/she
   wishes the rows sorted according to the ASCending or 
209 210 211
   DESCending mode operator. (See ORDER BY clause)</para>
   
  <para>
212
   The UNION clause specifies a table derived from a Cartesian
213 214 215
   product union join. (See UNION clause).</para>
   
  <para>
216
   You must have SELECT privilege to a table to read its values
217 218
   (See <command>GRANT</command>/<command>REVOKE</command> statements).
</para>
219 220 221
   
  <refsect2 id="R2-SQL-WHERE-2">
   <refsect2info>
222
    <date>1998-09-24</date>
223 224
   </refsect2info>
   <title>
225
    WHERE Clause
226 227 228 229 230
   </title>
   <para>
    The optional WHERE condition has the general form:
    
    <synopsis>
231
WHERE <replaceable class="PARAMETER">expr</replaceable> <replaceable class="PARAMETER">cond_op</replaceable> <replaceable class="PARAMETER">expr</replaceable> [ <replaceable class="PARAMETER">log_op</replaceable> ... ]
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
    </synopsis>
    
    where <replaceable class="PARAMETER">cond_op</replaceable> can be
    one of: =, &lt;, &lt;=, &gt;, &gt;=, &lt;&gt;
    or a conditional operator like ALL, ANY, IN, LIKE, et cetera
    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>
247
    <date>1998-09-24</date>
248 249
   </refsect2info>
   <title>
250
    GROUP BY Clause
251 252
   </title>
   <para>
253 254
    GROUP BY specifies a grouped table derived by the application
    of the this clause:
255
    <synopsis>
256
GROUP BY <replaceable class="PARAMETER">column</replaceable> [, ...]
257 258 259 260
    </synopsis></para></refsect2>
    
  <refsect2 id="R2-SQL-HAVING-2">
   <refsect2info>
261
    <date>1998-09-24</date>
262 263
   </refsect2info>
   <title>
264
    HAVING Clause
265 266 267 268 269 270 271 272 273 274 275 276
   </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>
277 278
    HAVING specifies a grouped table derived by the elimination
    of groups from the result of the previously specified clause
279 280 281
    that do not meet the <replaceable class="PARAMETER">cond_expr</replaceable>.</para>
    
   <para>
282 283
    Each column referenced in 
<replaceable class="PARAMETER">cond_expr</replaceable> shall unambiguously
284
    reference a grouping column.
285 286 287 288 289
   </para>
  </refsect2>
  
  <refsect2 id="R2-SQL-ORDERBYCLAUSE-2">
   <refsect2info>
290
    <date>1998-09-24</date>
291 292
   </refsect2info>
   <title>
293
    ORDER BY Clause
294 295 296
   </title>
   <para>
    <synopsis>
297
ORDER BY <replaceable class="PARAMETER">column</replaceable> [ ASC | DESC ] [, ...]
298 299 300 301 302 303
    </synopsis></para>
    
   <para>
    <replaceable class="PARAMETER">column</replaceable> can be either a column
    name or an ordinal number.</para>
   <para>
304 305 306
    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.
307 308
    This is never absolutely necessary because it is always possible
    assign a name
309
    to a calculated column using the AS clause, e.g.:
310
    <programlisting>
311
SELECT title, date_prod + 1 AS newlen FROM films ORDER BY newlen;
312 313 314
    </programlisting></para>
    
   <para>
315 316
    The columns in the ORDER BY must appear in the SELECT clause.
    Thus the following statement is illegal:
317
    <programlisting>
318
SELECT name FROM distributors ORDER BY code;
319 320 321
    </programlisting></para>
    
   <para>
322 323
    Optionally one may add the keyword DESC (descending)
    or ASC (ascending) after each column name in the ORDER BY clause.
324 325 326 327 328
    If not specified, ASC is assumed by default.</para>
  </refsect2>
  
  <refsect2 id="R2-SQL-UNION-2">
   <refsect2info>
329
    <date>1998-09-24</date>
330 331
   </refsect2info>
   <title>
332
    UNION Clause
333 334 335
   </title>
   <para>
    <synopsis>
336 337
<replaceable class="PARAMETER">table_query</replaceable> UNION [ ALL ] <replaceable class="PARAMETER">table_query</replaceable>
     [ ORDER BY <replaceable class="PARAMETER">column</replaceable> [ ASC | DESC ] [, ...] ]
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
    </synopsis>
    
    where
    <replaceable class="PARAMETER">table_query</replaceable>
    specifies any select expression without an ORDER BY clause.</para>
    
   <para>
    The UNION operator specifies a table derived from a Cartesian product.
    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>
355 356 357 358
    Multiple UNION operators in the same SELECT statement are
evaluated left to right.
    Note that the ALL keyword is not global in nature, being 
applied only for the current pair of table results.</para>
359 360 361 362 363 364 365 366 367 368 369 370
    
  </refsect2></refsect1>
  
 <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>
371 372 373
SELECT f.title, f.did, d.name, f.date_prod, f.kind
    FROM distributors d, films f
    WHERE f.did = d.did
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393

    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
394 395 396 397 398 399
  </programlisting>
  <para>
   To sum the column <literal>len</literal> of all films and group
   the reults by <literal>kind</literal>:
  </para>
  <programlisting>
400
SELECT kind, SUM(len) AS total FROM films GROUP BY kind;
401 402 403 404 405 406 407 408

    kind      |total
    ----------+------
    Action    | 07:34
    Comedy    | 02:58
    Drama     | 14:28
    Musical   | 06:42
    Romantic  | 04:38
409 410 411 412 413 414 415 416
  </programlisting>

  <para>
   To sum the column <literal>len</literal> of all films, group
   the reults by <literal>kind</literal> and show those group totals
   that are less than 5 hours:
  </para>
  <programlisting>
417 418 419 420
SELECT kind, SUM(len) AS total
    FROM films
    GROUP BY kind
    HAVING SUM(len) < INTERVAL '5 hour';
421 422 423 424 425

    kind      |total
    ----------+------
    Comedy    | 02:58
    Romantic  | 04:38
426 427 428 429 430 431 432
  </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>
433 434
SELECT * FROM distributors ORDER BY name;
SELECT * FROM distributors ORDER BY 2;
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450

    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
451 452 453 454 455 456 457 458 459 460 461
  </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:
462 463 464 465 466 467
    --        did|name                     id|name
    --        ---+------------             --+--------------
    --        108|Westward                  1|Woody Allen
    --        111|Walt Disney               2|Warren Beatty
    --        112|Warner Bros.              3|Walter Matthau
    --        ...                           ...
468

469
SELECT distributors.name
470 471
    FROM   distributors
    WHERE  distributors.name LIKE 'W%'
472 473 474 475
UNION
SELECT actors.name
    FROM   actors
    WHERE  actors.name LIKE 'W%'
476 477 478 479 480 481 482 483 484

    name
    --------------
    Walt Disney
    Walter Matthau
    Warner Bros.
    Warren Beatty
    Westward
    Woody Allen
485 486 487 488 489 490 491 492 493 494 495 496
  </programlisting>
  
 </refsect1>
 
 <refsect1 id="R1-SQL-SELECT-3">
  <title>
   Compatibility
  </title>
  <para>
  </para>
  
  <refsect2 id="R2-SQL-SELECT-4">
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
   <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>

  <refsect2 id="R2-SQL-SELECT-5">
518
   <refsect2info>
519
    <date>1998-09-24</date>
520 521 522 523 524 525 526 527 528 529 530 531
   </refsect2info>
   <title>
    <acronym>SQL92</acronym>
   </title>
   <para>
   </para>
   
   <refsect3 id="R3-SQL-SELECT-1">
    <refsect3info>
     <date>1998-04-15</date>
    </refsect3info>
    <title>
532
     SELECT Clause
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
    </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>
   </refsect3>

   <refsect3 id="R3-SQL-UNION-1">
    <refsect3info>
554
     <date>1998-09-24</date>
555 556
    </refsect3info>
    <title>
557
     UNION Clause
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
    </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>

578 579 580
<refentry id="SQL-SELECTINTO">
 <refmeta>
  <refentrytitle>
581
SELECT INTO
582 583 584 585 586
  </refentrytitle>
  <refmiscinfo>SQL - Language Statements</refmiscinfo>
 </refmeta>
 <refnamediv>
  <refname>
587
SELECT INTO
588 589
  </refname>
  <refpurpose>
590
Create a new table from an existing table or view
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
  </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> ] [, ...]
    INTO [ TABLE ] <replaceable class="PARAMETER">new_table</replaceable> ]
    [ 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> [, ...] ]
    [ UNION [ ALL ] <replaceable class="PARAMETER">select</replaceable>]
    [ 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.

  <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.

  <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.
</note>

 </refsect1>
</refentry>

645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
<!-- 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:
660
-->