mirror of
https://github.com/postgres/postgres.git
synced 2025-07-27 12:41:57 +03:00
Minor wording and formatting cleanups.
This commit is contained in:
@ -1,367 +1,372 @@
|
|||||||
<REFENTRY ID="SQL-SELECT-1">
|
<refentry id="SQL-SELECT">
|
||||||
<REFMETA>
|
<refmeta>
|
||||||
<REFENTRYTITLE>
|
<refentrytitle>
|
||||||
SELECT
|
SELECT
|
||||||
</REFENTRYTITLE>
|
</refentrytitle>
|
||||||
<REFMISCINFO>SQL - Language Statements</REFMISCINFO>
|
<refmiscinfo>SQL - Language Statements</refmiscinfo>
|
||||||
</REFMETA>
|
</refmeta>
|
||||||
<REFNAMEDIV>
|
<refnamediv>
|
||||||
<REFNAME>
|
<refname>
|
||||||
SELECT
|
SELECT
|
||||||
</REFNAME>
|
</refname>
|
||||||
<REFPURPOSE>
|
<refpurpose>
|
||||||
Retrieve rows from a table or view
|
Retrieve rows from a table or view.
|
||||||
</REFPURPOSE>
|
</refpurpose></refnamediv>
|
||||||
<REFSYNOPSISDIV>
|
<refsynopsisdiv>
|
||||||
<REFSYNOPSISDIVINFO>
|
<refsynopsisdivinfo>
|
||||||
<DATE>1998-04-15</DATE>
|
<date>1998-09-06</date>
|
||||||
</REFSYNOPSISDIVINFO>
|
</refsynopsisdivinfo>
|
||||||
<SYNOPSIS>
|
<synopsis>
|
||||||
<REPLACEABLE CLASS="PARAMETER">
|
|
||||||
</REPLACEABLE>
|
|
||||||
SELECT [ALL|DISTINCT]
|
SELECT [ALL|DISTINCT]
|
||||||
<REPLACEABLE CLASS="PARAMETER">expression</REPLACEABLE> [AS <REPLACEABLE CLASS="PARAMETER">name</REPLACEABLE>] [, ...]
|
<replaceable class="PARAMETER">expression</replaceable> [AS <replaceable class="PARAMETER">name</replaceable>] [, ...]
|
||||||
[INTO [TABLE] <REPLACEABLE CLASS="PARAMETER">intable</REPLACEABLE>]
|
[INTO [TABLE] <replaceable class="PARAMETER">intable</replaceable>]
|
||||||
[FROM <REPLACEABLE CLASS="PARAMETER">table</REPLACEABLE> [<REPLACEABLE CLASS="PARAMETER">alias</REPLACEABLE>] [, ...] ]
|
[FROM <replaceable class="PARAMETER">table</replaceable> [<replaceable class="PARAMETER">alias</replaceable>] [, ...] ]
|
||||||
[WHERE <REPLACEABLE CLASS="PARAMETER">condition</REPLACEABLE>]
|
[WHERE <replaceable class="PARAMETER">condition</replaceable>]
|
||||||
[GROUP BY <REPLACEABLE CLASS="PARAMETER">column</REPLACEABLE> [, ...] ]
|
[GROUP BY <replaceable class="PARAMETER">column</replaceable> [, ...] ]
|
||||||
[HAVING <REPLACEABLE CLASS="PARAMETER">condition</REPLACEABLE> [, ...] ]
|
[HAVING <replaceable class="PARAMETER">condition</replaceable> [, ...] ]
|
||||||
[UNION [ALL] <REPLACEABLE CLASS="PARAMETER">select</REPLACEABLE>]
|
[UNION [ALL] <replaceable class="PARAMETER">select</replaceable>]
|
||||||
[ORDER BY <REPLACEABLE CLASS="PARAMETER">column</REPLACEABLE> [ASC | DESC] [, ...] ]
|
[ORDER BY <replaceable class="PARAMETER">column</replaceable> [ASC | DESC] [, ...] ]
|
||||||
</SYNOPSIS>
|
</synopsis>
|
||||||
|
|
||||||
<REFSECT2 ID="R2-SQL-SELECT-1">
|
<refsect2 id="R2-SQL-SELECT-1">
|
||||||
<REFSECT2INFO>
|
<refsect2info>
|
||||||
<DATE>1998-04-15</DATE>
|
<date>1998-09-06</date>
|
||||||
</REFSECT2INFO>
|
</refsect2info>
|
||||||
<TITLE>
|
<title>
|
||||||
Inputs
|
Inputs
|
||||||
</TITLE>
|
</title>
|
||||||
|
|
||||||
<PARA>
|
<para>
|
||||||
<VARIABLELIST>
|
<variablelist>
|
||||||
<VARLISTENTRY>
|
<varlistentry>
|
||||||
<TERM>
|
<term>
|
||||||
<REPLACEABLE CLASS="PARAMETER">expression</REPLACEABLE>
|
<replaceable class="PARAMETER">expression</replaceable>
|
||||||
</TERM>
|
</term>
|
||||||
<LISTITEM>
|
<listitem>
|
||||||
<PARA>
|
<para>
|
||||||
The name of a table's column or an expression.
|
The name of a table's column or an expression.
|
||||||
</PARA>
|
</para>
|
||||||
</LISTITEM>
|
</listitem>
|
||||||
</VARLISTENTRY>
|
</varlistentry>
|
||||||
|
|
||||||
<VARLISTENTRY>
|
<varlistentry>
|
||||||
<TERM>
|
<term>
|
||||||
<REPLACEABLE CLASS="PARAMETER">name</REPLACEABLE>
|
<replaceable class="PARAMETER">name</replaceable>
|
||||||
</TERM>
|
</term>
|
||||||
<LISTITEM>
|
<listitem>
|
||||||
<PARA>
|
<para>
|
||||||
Specifies another name for a column or an expression using
|
Specifies another name for a column or an expression using
|
||||||
the AS clause. <REPLACEABLE CLASS="PARAMETER">name</REPLACEABLE> cannot be used in the WHERE
|
the AS clause. <replaceable class="PARAMETER">name</replaceable>
|
||||||
condition. It can, however, be referenced in associated
|
cannot be used in the WHERE
|
||||||
ORDER BY or GROUP BY clauses.
|
condition. It can, however, be referenced in associated
|
||||||
</PARA>
|
ORDER BY or GROUP BY clauses.
|
||||||
</LISTITEM>
|
</para>
|
||||||
</VARLISTENTRY>
|
</listitem>
|
||||||
|
</varlistentry>
|
||||||
<VARLISTENTRY>
|
|
||||||
<TERM>
|
<varlistentry>
|
||||||
<REPLACEABLE CLASS="PARAMETER">intable</REPLACEABLE>
|
<term>
|
||||||
</TERM>
|
<replaceable class="PARAMETER">intable</replaceable>
|
||||||
<LISTITEM>
|
</term>
|
||||||
<PARA>
|
<listitem>
|
||||||
If the INTO TABLE clause is specified, the result of the
|
<para>
|
||||||
query will be stored in another table with the indicated
|
If the INTO TABLE clause is specified, the result of the
|
||||||
name.
|
query will be stored in another table with the indicated
|
||||||
If <REPLACEABLE CLASS="PARAMETER">intable</REPLACEABLE> does not exist, it will be created automatically.
|
name.
|
||||||
|
If <replaceable class="PARAMETER">intable</replaceable> does
|
||||||
<Note>
|
not exist, it will be created automatically.
|
||||||
<Para>
|
|
||||||
The CREATE TABLE AS statement will also create a new table from a select query.
|
<note>
|
||||||
</Para>
|
<para>
|
||||||
</Note>
|
The <command>CREATE TABLE AS</command> statement will also
|
||||||
</PARA>
|
create a new table from a select query.
|
||||||
</LISTITEM>
|
</para>
|
||||||
</VARLISTENTRY>
|
</note>
|
||||||
|
</para>
|
||||||
<VARLISTENTRY>
|
</listitem>
|
||||||
<TERM>
|
</varlistentry>
|
||||||
<REPLACEABLE CLASS="PARAMETER">table</REPLACEABLE>
|
|
||||||
</TERM>
|
<varlistentry>
|
||||||
<LISTITEM>
|
<term>
|
||||||
<PARA>
|
<replaceable class="PARAMETER">table</replaceable>
|
||||||
The name of an existing table referenced by FROM clause.
|
</term>
|
||||||
</PARA>
|
<listitem>
|
||||||
</LISTITEM>
|
<para>
|
||||||
</VARLISTENTRY>
|
The name of an existing table referenced by the FROM clause.
|
||||||
|
</para>
|
||||||
<VARLISTENTRY>
|
</listitem>
|
||||||
<TERM>
|
</varlistentry>
|
||||||
<REPLACEABLE CLASS="PARAMETER">alias</REPLACEABLE>
|
|
||||||
</TERM>
|
<varlistentry>
|
||||||
<LISTITEM>
|
<term>
|
||||||
<PARA>
|
<replaceable class="PARAMETER">alias</replaceable>
|
||||||
An alternate name for the preceeding <REPLACEABLE CLASS="PARAMETER">table</REPLACEABLE>.
|
</term>
|
||||||
Used for brevity or to eliminate ambiguity for joins within a single table.
|
<listitem>
|
||||||
</PARA>
|
<para>
|
||||||
</LISTITEM>
|
An alternate name for the preceding
|
||||||
</VARLISTENTRY>
|
<replaceable class="PARAMETER">table</replaceable>.
|
||||||
|
It is used for brevity or to eliminate ambiguity for joins
|
||||||
<VARLISTENTRY>
|
within a single table.
|
||||||
<TERM>
|
</para>
|
||||||
<REPLACEABLE CLASS="PARAMETER">condition</REPLACEABLE>
|
</listitem>
|
||||||
</TERM>
|
</varlistentry>
|
||||||
<LISTITEM>
|
|
||||||
<PARA>
|
<varlistentry>
|
||||||
An expression leading to a boolean (true/false) result.
|
<term>
|
||||||
See the WHERE clause.
|
<replaceable class="PARAMETER">condition</replaceable>
|
||||||
</PARA>
|
</term>
|
||||||
</LISTITEM>
|
<listitem>
|
||||||
</VARLISTENTRY>
|
<para>
|
||||||
|
A boolean expression giving a result of true or false.
|
||||||
<VARLISTENTRY>
|
See the WHERE clause.
|
||||||
<TERM>
|
</para>
|
||||||
<REPLACEABLE CLASS="PARAMETER">column</REPLACEABLE>
|
</listitem>
|
||||||
</TERM>
|
</varlistentry>
|
||||||
<LISTITEM>
|
|
||||||
<PARA>
|
<varlistentry>
|
||||||
The name of a table's column.
|
<term>
|
||||||
</PARA>
|
<replaceable class="PARAMETER">column</replaceable>
|
||||||
</LISTITEM>
|
</term>
|
||||||
</VARLISTENTRY>
|
<listitem>
|
||||||
|
<para>
|
||||||
<VARLISTENTRY>
|
The name of a table's column.
|
||||||
<TERM>
|
</para>
|
||||||
<REPLACEABLE CLASS="PARAMETER">select</REPLACEABLE>
|
</listitem>
|
||||||
</TERM>
|
</varlistentry>
|
||||||
<LISTITEM>
|
|
||||||
<PARA>
|
<varlistentry>
|
||||||
A select statement with all features except the ORDER BY clause.
|
<term>
|
||||||
</PARA>
|
<replaceable class="PARAMETER">select</replaceable>
|
||||||
</LISTITEM>
|
</term>
|
||||||
</VARLISTENTRY>
|
<listitem>
|
||||||
|
<para>
|
||||||
</VARIABLELIST>
|
A select statement with all features except the ORDER BY clause.
|
||||||
</PARA>
|
</para>
|
||||||
</REFSECT2>
|
</listitem>
|
||||||
|
</varlistentry>
|
||||||
<REFSECT2 ID="R2-SQL-SELECT-2">
|
|
||||||
<REFSECT2INFO>
|
</variablelist>
|
||||||
<DATE>1998-04-15</DATE>
|
</para>
|
||||||
</REFSECT2INFO>
|
</refsect2>
|
||||||
<TITLE>
|
|
||||||
Outputs
|
<refsect2 id="R2-SQL-SELECT-2">
|
||||||
</TITLE>
|
<refsect2info>
|
||||||
<VARIABLELIST>
|
<date>1998-09-06</date>
|
||||||
<VARLISTENTRY>
|
</refsect2info>
|
||||||
<TERM>
|
<title>
|
||||||
Rows
|
Outputs
|
||||||
</TERM>
|
</title>
|
||||||
<LISTITEM>
|
<variablelist>
|
||||||
<PARA>
|
<varlistentry>
|
||||||
The complete set of rows resulting from the query specification.
|
<term>
|
||||||
</PARA>
|
Rows
|
||||||
</LISTITEM>
|
</term>
|
||||||
</VARLISTENTRY>
|
<listitem>
|
||||||
|
<para>
|
||||||
<VARLISTENTRY>
|
The complete set of rows resulting from the query specification.
|
||||||
<TERM>
|
</para>
|
||||||
<ReturnValue>count</ReturnValue>
|
</listitem>
|
||||||
</TERM>
|
</varlistentry>
|
||||||
<LISTITEM>
|
|
||||||
<PARA>
|
<varlistentry>
|
||||||
The count of rows returned by the query.
|
<term>
|
||||||
</PARA>
|
<returnvalue>count</returnvalue>
|
||||||
</LISTITEM>
|
</term>
|
||||||
</VARLISTENTRY>
|
<listitem>
|
||||||
</VARIABLELIST>
|
<para>
|
||||||
|
The count of rows returned by the query.
|
||||||
</REFSECT2>
|
</para>
|
||||||
</REFSYNOPSISDIV>
|
</listitem>
|
||||||
|
</varlistentry>
|
||||||
<REFSECT1 ID="R1-SQL-SELECT-1">
|
</variablelist>
|
||||||
<REFSECT1INFO>
|
|
||||||
<DATE>1998-04-15</DATE>
|
</refsect2>
|
||||||
</REFSECT1INFO>
|
</refsynopsisdiv>
|
||||||
<TITLE>
|
|
||||||
Description
|
<refsect1 id="R1-SQL-SELECT-1">
|
||||||
</TITLE>
|
<refsect1info>
|
||||||
<PARA>
|
<date>1998-09-06</date>
|
||||||
SELECT is the principal means to access information
|
</refsect1info>
|
||||||
in <productname>Postgres</productname>.
|
<title>
|
||||||
|
Description
|
||||||
|
</title>
|
||||||
|
<para>
|
||||||
SELECT will get all rows which satisfy the WHERE condition
|
SELECT will get all rows which satisfy the WHERE condition
|
||||||
or all rows of a table if WHERE is omitted.
|
or all rows of a table if WHERE is omitted.</para>
|
||||||
|
|
||||||
<PARA>
|
<para>
|
||||||
The GROUP BY clause allows a user to divide a table
|
The GROUP BY clause allows a user to divide a table
|
||||||
conceptually into groups. (See GROUP BY clause).
|
conceptually into groups. (See GROUP BY clause).</para>
|
||||||
|
|
||||||
|
<para>
|
||||||
The HAVING clause specifies a grouped table derived by the
|
The HAVING clause specifies a grouped table derived by the
|
||||||
elimination of groups from the result of the previously
|
elimination of groups from the result of the previously
|
||||||
specified clause. (See HAVING clause).
|
specified clause. (See HAVING clause).</para>
|
||||||
|
|
||||||
|
<para>
|
||||||
The ORDER BY clause allows a user to specify that he/she
|
The ORDER BY clause allows a user to specify that he/she
|
||||||
wishes the rows sorted according to the ASCending or
|
wishes the rows sorted according to the ASCending or
|
||||||
DESCending mode operator. (See ORDER BY clause)
|
DESCending mode operator. (See ORDER BY clause)</para>
|
||||||
|
|
||||||
|
<para>
|
||||||
The UNION clause specifies a table derived from a Cartesian
|
The UNION clause specifies a table derived from a Cartesian
|
||||||
product union join. (See UNION clause).
|
product union join. (See UNION clause).</para>
|
||||||
|
|
||||||
<PARA>
|
<para>
|
||||||
You must have SELECT privilege to a table to read its values
|
You must have SELECT privilege to a table to read its values
|
||||||
(See GRANT/REVOKE statements).
|
(See GRANT/REVOKE statements).</para>
|
||||||
|
|
||||||
<REFSECT2 ID="R2-SQL-WHERE-2">
|
<refsect2 id="R2-SQL-WHERE-2">
|
||||||
<REFSECT2INFO>
|
<refsect2info>
|
||||||
<DATE>1998-04-15</DATE>
|
<date>1998-09-06</date>
|
||||||
</REFSECT2INFO>
|
</refsect2info>
|
||||||
<TITLE>
|
<title>
|
||||||
WHERE clause
|
WHERE clause
|
||||||
</TITLE>
|
</title>
|
||||||
<PARA>
|
<para>
|
||||||
The optional WHERE condition has the general form:
|
The optional WHERE condition has the general form:
|
||||||
|
|
||||||
<Synopsis>
|
<synopsis>
|
||||||
WHERE <REPLACEABLE CLASS="PARAMETER">expr</REPLACEABLE> <REPLACEABLE CLASS="PARAMETER">cond_op</REPLACEABLE> <REPLACEABLE CLASS="PARAMETER">expr</REPLACEABLE> [<REPLACEABLE CLASS="PARAMETER">log_op</REPLACEABLE> ...]
|
WHERE <replaceable class="PARAMETER">expr</replaceable> <replaceable class="PARAMETER">cond_op</replaceable> <replaceable class="PARAMETER">expr</replaceable> [<replaceable class="PARAMETER">log_op</replaceable> ...]
|
||||||
</Synopsis>
|
</synopsis>
|
||||||
|
|
||||||
where <REPLACEABLE CLASS="PARAMETER">cond_op</REPLACEABLE> can be one of: =, <, <=, >, >=, <>
|
where <replaceable class="PARAMETER">cond_op</replaceable> can be
|
||||||
or a conditional operator like ALL, ANY, IN, LIKE, et cetera
|
one of: =, <, <=, >, >=, <>
|
||||||
and <REPLACEABLE CLASS="PARAMETER">log_op</REPLACEABLE> can be one of: AND, OR, NOT.
|
or a conditional operator like ALL, ANY, IN, LIKE, et cetera
|
||||||
The comparison returns either TRUE or FALSE and all
|
and <replaceable class="PARAMETER">log_op</replaceable> can be one
|
||||||
instances will be discarded
|
of: AND, OR, NOT.
|
||||||
if the expression evaluates to FALSE.
|
The comparison returns either TRUE or FALSE and all
|
||||||
</PARA>
|
instances will be discarded
|
||||||
</REFSECT2>
|
if the expression evaluates to FALSE.
|
||||||
|
</para>
|
||||||
<REFSECT2 ID="R2-SQL-GROUPBY-2">
|
</refsect2>
|
||||||
<REFSECT2INFO>
|
|
||||||
<DATE>1998-04-15</DATE>
|
<refsect2 id="R2-SQL-GROUPBY-2">
|
||||||
</REFSECT2INFO>
|
<refsect2info>
|
||||||
<TITLE>
|
<date>1998-09-06</date>
|
||||||
GROUP BY clause
|
</refsect2info>
|
||||||
</TITLE>
|
<title>
|
||||||
<PARA>
|
GROUP BY clause
|
||||||
|
</title>
|
||||||
|
<para>
|
||||||
GROUP BY specifies a grouped table derived by the application
|
GROUP BY specifies a grouped table derived by the application
|
||||||
of the this clause:
|
of the this clause:
|
||||||
<SYNOPSIS>
|
<synopsis>
|
||||||
GROUP BY <REPLACEABLE CLASS="PARAMETER">column</REPLACEABLE> [, ...]
|
GROUP BY <replaceable class="PARAMETER">column</replaceable> [, ...]
|
||||||
</SYNOPSIS>
|
</synopsis></para></refsect2>
|
||||||
|
|
||||||
<REFSECT2 ID="R2-SQL-HAVING-2">
|
<refsect2 id="R2-SQL-HAVING-2">
|
||||||
<REFSECT2INFO>
|
<refsect2info>
|
||||||
<DATE>1998-04-15</DATE>
|
<date>1998-09-06</date>
|
||||||
</REFSECT2INFO>
|
</refsect2info>
|
||||||
<TITLE>
|
<title>
|
||||||
HAVING clause
|
HAVING clause
|
||||||
</TITLE>
|
</title>
|
||||||
<PARA>
|
<para>
|
||||||
The optional HAVING condition has the general form:
|
The optional HAVING condition has the general form:
|
||||||
|
|
||||||
<Synopsis>
|
<synopsis>
|
||||||
HAVING <REPLACEABLE CLASS="PARAMETER">cond_expr</REPLACEABLE>
|
HAVING <replaceable class="PARAMETER">cond_expr</replaceable>
|
||||||
</Synopsis>
|
</synopsis>
|
||||||
|
|
||||||
where <REPLACEABLE CLASS="PARAMETER">cond_expr</REPLACEABLE> is the same
|
where <replaceable class="PARAMETER">cond_expr</replaceable> is the same
|
||||||
as specified for the WHERE clause.
|
as specified for the WHERE clause.</para>
|
||||||
|
|
||||||
<Para>
|
<para>
|
||||||
HAVING specifies a grouped table derived by the elimination
|
HAVING specifies a grouped table derived by the elimination
|
||||||
of groups from the result of the previously specified clause
|
of groups from the result of the previously specified clause
|
||||||
that do not meet the <REPLACEABLE CLASS="PARAMETER">cond_expr</REPLACEABLE>.
|
that do not meet the <replaceable class="PARAMETER">cond_expr</replaceable>.</para>
|
||||||
|
|
||||||
<Para>
|
<para>
|
||||||
Each column referenced in <REPLACEABLE CLASS="PARAMETER">cond_expr</REPLACEABLE> shall unambiguously
|
Each column referenced in <replaceable class="PARAMETER">cond_expr</replaceable> shall unambiguously
|
||||||
reference a grouping column.
|
reference a grouping column.
|
||||||
</PARA>
|
</para>
|
||||||
</REFSECT2>
|
</refsect2>
|
||||||
|
|
||||||
<REFSECT2 ID="R2-SQL-ORDERBYCLAUSE-2">
|
<refsect2 id="R2-SQL-ORDERBYCLAUSE-2">
|
||||||
<REFSECT2INFO>
|
<refsect2info>
|
||||||
<DATE>1998-04-15</DATE>
|
<date>1998-09-06</date>
|
||||||
</REFSECT2INFO>
|
</refsect2info>
|
||||||
<TITLE>
|
<title>
|
||||||
ORDER BY clause
|
ORDER BY clause
|
||||||
</TITLE>
|
</title>
|
||||||
<PARA>
|
<para>
|
||||||
<Synopsis>
|
<synopsis>
|
||||||
ORDER BY <REPLACEABLE CLASS="PARAMETER">column</REPLACEABLE> [ASC | DESC] [, ...]
|
ORDER BY <replaceable class="PARAMETER">column</replaceable> [ASC | DESC] [, ...]
|
||||||
</Synopsis>
|
</synopsis></para>
|
||||||
|
|
||||||
<PARA>
|
<para>
|
||||||
<REPLACEABLE CLASS="PARAMETER">column</REPLACEABLE> can be either a column
|
<replaceable class="PARAMETER">column</replaceable> can be either a column
|
||||||
name or an ordinal number.
|
name or an ordinal number.</para>
|
||||||
<PARA>
|
<para>
|
||||||
The ordinal numbers refers to the ordinal (left-to-right) position
|
The ordinal numbers refers to the ordinal (left-to-right) position
|
||||||
of the column. This feature makes it possible to define an ordering
|
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.
|
on the basis of a column that does not have a proper name.
|
||||||
This is never absolutely necessary because it is always possible assign a name
|
This is never absolutely necessary because it is always possible assign a name
|
||||||
to a calculated column using the AS clause, e.g.:
|
to a calculated column using the AS clause, e.g.:
|
||||||
<ProgramListing>
|
<programlisting>
|
||||||
SELECT title, date_prod + 1 AS newlen FROM films ORDER BY newlen;
|
SELECT title, date_prod + 1 AS newlen FROM films ORDER BY newlen;
|
||||||
</ProgramListing>
|
</programlisting></para>
|
||||||
|
|
||||||
<PARA>
|
<para>
|
||||||
The columns in the ORDER BY must appear in the SELECT clause.
|
The columns in the ORDER BY must appear in the SELECT clause.
|
||||||
Thus the following statement is illegal:
|
Thus the following statement is illegal:
|
||||||
<ProgramListing>
|
<programlisting>
|
||||||
SELECT name FROM distributors ORDER BY code;
|
SELECT name FROM distributors ORDER BY code;
|
||||||
</ProgramListing>
|
</programlisting></para>
|
||||||
|
|
||||||
<PARA>
|
<para>
|
||||||
Optionally one may add the keyword DESC (descending)
|
Optionally one may add the keyword DESC (descending)
|
||||||
or ASC (ascending) after each column name in the ORDER BY clause.
|
or ASC (ascending) after each column name in the ORDER BY clause.
|
||||||
If not specified, ASC is assumed by default.
|
If not specified, ASC is assumed by default.</para>
|
||||||
</REFSECT2>
|
</refsect2>
|
||||||
|
|
||||||
<REFSECT2 ID="R2-SQL-UNION-2">
|
<refsect2 id="R2-SQL-UNION-2">
|
||||||
<REFSECT2INFO>
|
<refsect2info>
|
||||||
<DATE>1998-04-15</DATE>
|
<date>1998-09-06</date>
|
||||||
</REFSECT2INFO>
|
</refsect2info>
|
||||||
<TITLE>
|
<title>
|
||||||
UNION clause
|
UNION clause
|
||||||
</TITLE>
|
</title>
|
||||||
<PARA>
|
<para>
|
||||||
<Synopsis>
|
<synopsis>
|
||||||
<REPLACEABLE CLASS="PARAMETER">table_query</REPLACEABLE> UNION [ALL] <REPLACEABLE CLASS="PARAMETER">table_query</REPLACEABLE>
|
<replaceable class="PARAMETER">table_query</replaceable> UNION [ALL] <replaceable class="PARAMETER">table_query</replaceable>
|
||||||
[ORDER BY <REPLACEABLE CLASS="PARAMETER">column</REPLACEABLE> [ASC | DESC] [, ...] ]
|
[ORDER BY <replaceable class="PARAMETER">column</replaceable> [ASC | DESC] [, ...] ]
|
||||||
</Synopsis>
|
</synopsis>
|
||||||
|
|
||||||
where
|
where
|
||||||
<REPLACEABLE CLASS="PARAMETER">table_query</REPLACEABLE>
|
<replaceable class="PARAMETER">table_query</replaceable>
|
||||||
specifies any select expression without an ORDER BY clause.
|
specifies any select expression without an ORDER BY clause.</para>
|
||||||
|
|
||||||
<PARA>
|
<para>
|
||||||
The UNION operator specifies a table derived from a Cartesian product.
|
The UNION operator specifies a table derived from a Cartesian product.
|
||||||
The two tables that represent the direct operands of the UNION must
|
The two tables that represent the direct operands of the UNION must
|
||||||
have the same number of columns, and corresponding columns must be
|
have the same number of columns, and corresponding columns must be
|
||||||
of compatible data types.
|
of compatible data types.</para>
|
||||||
|
|
||||||
<PARA>
|
<para>
|
||||||
By default, the result of UNION does not contain any duplicate rows
|
By default, the result of UNION does not contain any duplicate rows
|
||||||
unless the ALL clause is specified.
|
unless the ALL clause is specified.</para>
|
||||||
|
|
||||||
<Para>
|
<para>
|
||||||
Multiple UNION operators in the same SELECT statement are evaluated left to right.
|
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
|
Note that the ALL keyword is not global in nature, being applied only for the current pair of
|
||||||
table results.
|
table results.</para>
|
||||||
|
|
||||||
</REFSECT2>
|
</refsect2></refsect1>
|
||||||
|
|
||||||
<REFSECT1 ID="R1-SQL-SELECT-2">
|
<refsect1 id="R1-SQL-SELECT-2">
|
||||||
<TITLE>
|
<title>
|
||||||
Usage
|
Usage
|
||||||
</TITLE>
|
</title>
|
||||||
<PARA>
|
<para>
|
||||||
</PARA>
|
To join the table <literal>films</literal> with the table
|
||||||
<ProgramListing>
|
<literal>distributors</literal>:
|
||||||
--Join table films with their distributors:
|
</para>
|
||||||
--
|
<programlisting>
|
||||||
SELECT f.title, f.did, d.name, f.date_prod, f.kind
|
SELECT f.title, f.did, d.name, f.date_prod, f.kind
|
||||||
FROM distributors d, films f
|
FROM distributors d, films f
|
||||||
WHERE f.did = d.did;
|
WHERE f.did = d.did
|
||||||
|
|
||||||
title |did|name | date_prod|kind
|
title |did|name | date_prod|kind
|
||||||
-------------------------+---+----------------+----------+----------
|
-------------------------+---+----------------+----------+----------
|
||||||
@ -382,11 +387,12 @@ Usage
|
|||||||
The King and I |109|20th Century Fox|1956-08-11|Musical
|
The King and I |109|20th Century Fox|1956-08-11|Musical
|
||||||
Das Boot |110|Bavaria Atelier |1981-11-11|Drama
|
Das Boot |110|Bavaria Atelier |1981-11-11|Drama
|
||||||
Bed Knobs and Broomsticks|111|Walt Disney | |Musical
|
Bed Knobs and Broomsticks|111|Walt Disney | |Musical
|
||||||
</ProgramListing>
|
</programlisting>
|
||||||
|
<para>
|
||||||
<ProgramListing>
|
To sum the column <literal>len</literal> of all films and group
|
||||||
--sum column "len" of all films group by "kind":
|
the reults by <literal>kind</literal>:
|
||||||
--
|
</para>
|
||||||
|
<programlisting>
|
||||||
SELECT kind, SUM(len) AS total FROM films GROUP BY kind;
|
SELECT kind, SUM(len) AS total FROM films GROUP BY kind;
|
||||||
|
|
||||||
kind |total
|
kind |total
|
||||||
@ -396,12 +402,14 @@ Usage
|
|||||||
Drama | 14:28
|
Drama | 14:28
|
||||||
Musical | 06:42
|
Musical | 06:42
|
||||||
Romantic | 04:38
|
Romantic | 04:38
|
||||||
</ProgramListing>
|
</programlisting>
|
||||||
|
|
||||||
<ProgramListing>
|
<para>
|
||||||
--sum column length of all films group by "kind"
|
To sum the column <literal>len</literal> of all films, group
|
||||||
--having total duration < 5 hours:
|
the reults by <literal>kind</literal> and show those group totals
|
||||||
--
|
that are less than 5 hours:
|
||||||
|
</para>
|
||||||
|
<programlisting>
|
||||||
SELECT kind, SUM(len) AS total
|
SELECT kind, SUM(len) AS total
|
||||||
FROM films
|
FROM films
|
||||||
GROUP BY kind
|
GROUP BY kind
|
||||||
@ -411,11 +419,13 @@ Usage
|
|||||||
----------+------
|
----------+------
|
||||||
Comedy | 02:58
|
Comedy | 02:58
|
||||||
Romantic | 04:38
|
Romantic | 04:38
|
||||||
</ProgramListing>
|
</programlisting>
|
||||||
|
<para>
|
||||||
<ProgramListing>
|
The following two examples are identical ways of sorting the individual
|
||||||
--The following two examples are identicals:
|
results according to the contents of the second column
|
||||||
--
|
(<literal>name</literal>):
|
||||||
|
</para>
|
||||||
|
<programlisting>
|
||||||
SELECT * FROM distributors ORDER BY name;
|
SELECT * FROM distributors ORDER BY name;
|
||||||
SELECT * FROM distributors ORDER BY 2;
|
SELECT * FROM distributors ORDER BY 2;
|
||||||
|
|
||||||
@ -434,29 +444,31 @@ Usage
|
|||||||
111|Walt Disney
|
111|Walt Disney
|
||||||
112|Warner Bros.
|
112|Warner Bros.
|
||||||
108|Westward
|
108|Westward
|
||||||
</ProgramListing>
|
</programlisting>
|
||||||
|
|
||||||
<ProgramListing>
|
<para>
|
||||||
--union of table distributors and table actors:
|
This example shows how to obtain the union of the tables
|
||||||
-- (only those that begin with letter W):
|
<literal>distributors</literal> and
|
||||||
--
|
<literal>actors</literal>, restricting the results to those that begin
|
||||||
-- distributors: actors:
|
with letter W in each table. Only distinct rows are to be used, so the
|
||||||
|
ALL keyword is omitted:
|
||||||
|
</para>
|
||||||
|
<programlisting>
|
||||||
|
-- distributors: actors:
|
||||||
-- did|name id|name
|
-- did|name id|name
|
||||||
-- ---+------------ --+--------------
|
-- ---+------------ --+--------------
|
||||||
-- 108|Westward 1|Woody Allen
|
-- 108|Westward 1|Woody Allen
|
||||||
-- 111|Walt Disney 2|Warren Beatty
|
-- 111|Walt Disney 2|Warren Beatty
|
||||||
-- 112|Warner Bros. 3|Walter Matthau
|
-- 112|Warner Bros. 3|Walter Matthau
|
||||||
-- ... ...
|
-- ... ...
|
||||||
--
|
|
||||||
--select only distinct rows (without ALL):
|
|
||||||
--
|
|
||||||
SELECT distributors.name
|
SELECT distributors.name
|
||||||
FROM distributors
|
FROM distributors
|
||||||
WHERE distributors.name LIKE 'W%'
|
WHERE distributors.name LIKE 'W%'
|
||||||
UNION
|
UNION
|
||||||
SELECT actors.name
|
SELECT actors.name
|
||||||
FROM actors
|
FROM actors
|
||||||
WHERE actors.name LIKE 'W%';
|
WHERE actors.name LIKE 'W%'
|
||||||
|
|
||||||
name
|
name
|
||||||
--------------
|
--------------
|
||||||
@ -466,88 +478,91 @@ Usage
|
|||||||
Warren Beatty
|
Warren Beatty
|
||||||
Westward
|
Westward
|
||||||
Woody Allen
|
Woody Allen
|
||||||
</ProgramListing>
|
</programlisting>
|
||||||
|
|
||||||
|
</refsect1>
|
||||||
|
|
||||||
|
<refsect1 id="R1-SQL-SELECT-3">
|
||||||
|
<title>
|
||||||
|
Compatibility
|
||||||
|
</title>
|
||||||
|
<para>
|
||||||
|
</para>
|
||||||
|
|
||||||
|
<refsect2 id="R2-SQL-SELECT-4">
|
||||||
|
<refsect2info>
|
||||||
|
<date>1998-09-06</date>
|
||||||
|
</refsect2info>
|
||||||
|
<title>
|
||||||
|
<acronym>SQL92</acronym>
|
||||||
|
</title>
|
||||||
|
<para>
|
||||||
|
</para>
|
||||||
|
|
||||||
|
<refsect3 id="R3-SQL-SELECT-1">
|
||||||
|
<refsect3info>
|
||||||
|
<date>1998-04-15</date>
|
||||||
|
</refsect3info>
|
||||||
|
<title>
|
||||||
|
SELECT clause
|
||||||
|
</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>
|
||||||
|
|
||||||
</REFSECT1>
|
<refsect3 id="R3-SQL-UNION-1">
|
||||||
|
<refsect3info>
|
||||||
|
<date>1998-09-06</date>
|
||||||
|
</refsect3info>
|
||||||
|
<title>
|
||||||
|
UNION clause
|
||||||
|
</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>
|
||||||
|
|
||||||
<REFSECT1 ID="R1-SQL-SELECT-3">
|
<para>
|
||||||
<TITLE>
|
The CORRESPONDING BY clause is not supported by
|
||||||
Compatibility
|
<productname>Postgres</productname>.
|
||||||
</TITLE>
|
</para>
|
||||||
<PARA>
|
</refsect3>
|
||||||
</PARA>
|
|
||||||
|
</refsect2>
|
||||||
|
</refsect1>
|
||||||
|
</refentry>
|
||||||
|
|
||||||
<REFSECT2 ID="R2-SQL-SELECT-4">
|
<!-- Keep this comment at the end of the file
|
||||||
<REFSECT2INFO>
|
Local variables:
|
||||||
<DATE>1998-04-15</DATE>
|
mode: sgml
|
||||||
</REFSECT2INFO>
|
sgml-omittag:t
|
||||||
<TITLE>
|
sgml-shorttag:t
|
||||||
<Acronym>SQL92</Acronym>
|
sgml-minimize-attributes:nil
|
||||||
</TITLE>
|
sgml-always-quote-attributes:t
|
||||||
<PARA>
|
sgml-indent-step:1
|
||||||
</PARA>
|
sgml-indent-data:t
|
||||||
|
sgml-parent-document:nil
|
||||||
<REFSECT3 ID="R3-SQL-SELECT-1">
|
sgml-default-dtd-file:"../reference.ced"
|
||||||
<REFSECT3INFO>
|
sgml-exposed-tags:nil
|
||||||
<DATE>1998-04-15</DATE>
|
sgml-local-catalogs:"/usr/lib/sgml/catalog"
|
||||||
</REFSECT3INFO>
|
sgml-local-ecat-files:nil
|
||||||
<TITLE>
|
End:
|
||||||
SELECT clause
|
|
||||||
</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>
|
|
||||||
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>
|
|
||||||
<DATE>1998-04-15</DATE>
|
|
||||||
</REFSECT3INFO>
|
|
||||||
<TITLE>
|
|
||||||
UNION clause
|
|
||||||
</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>
|
|
||||||
The CORRESPONDING BY clause is not supported by <ProductName>Postgres</ProductName>.
|
|
||||||
</PARA>
|
|
||||||
</REFSECT3>
|
|
||||||
|
|
||||||
</REFSECT2>
|
|
||||||
</REFSECT1>
|
|
||||||
</REFENTRY>
|
|
||||||
|
|
||||||
<!--
|
|
||||||
<REPLACEABLE CLASS="PARAMETER">
|
|
||||||
</REPLACEABLE>
|
|
||||||
<ReturnValue></ReturnValue>
|
|
||||||
<PARA>
|
|
||||||
</PARA>
|
|
||||||
<VARIABLELIST>
|
|
||||||
<VARLISTENTRY>
|
|
||||||
<TERM>•
|
|
||||||
</TERM>
|
|
||||||
<LISTITEM>
|
|
||||||
<PARA>
|
|
||||||
</PARA>
|
|
||||||
</LISTITEM>
|
|
||||||
</VARLISTENTRY>
|
|
||||||
</VARIABLELIST>
|
|
||||||
<PARA>
|
|
||||||
</PARA>
|
|
||||||
-->
|
-->
|
||||||
|
Reference in New Issue
Block a user