11<!--
2- $Header: /cvsroot/pgsql/doc/src/sgml/func.sgml,v 1.157 2003/06/27 00:33:25 tgl Exp $
2+ $Header: /cvsroot/pgsql/doc/src/sgml/func.sgml,v 1.158 2003/06/29 00:33:42 tgl Exp $
33PostgreSQL documentation
44-->
55
@@ -7457,52 +7457,14 @@ SELECT col1 FROM tab1
74577457 </sect2>
74587458
74597459 <sect2>
7460- <title><literal>IN</literal> (scalar form)</title>
7461-
7462- <synopsis>
7463- <replaceable>expression</replaceable> IN (<replaceable>value</replaceable><optional>, ...</optional>)
7464- </synopsis>
7465-
7466- <para>
7467- The right-hand side of this form of <token>IN</token> is a parenthesized list
7468- of scalar expressions. The result is <quote>true</> if the left-hand expression's
7469- result is equal to any of the right-hand expressions. This is a shorthand
7470- notation for
7471-
7472- <synopsis>
7473- <replaceable>expression</replaceable> = <replaceable>value1</replaceable>
7474- OR
7475- <replaceable>expression</replaceable> = <replaceable>value2</replaceable>
7476- OR
7477- ...
7478- </synopsis>
7479- </para>
7480-
7481- <para>
7482- Note that if the left-hand expression yields null, or if there are
7483- no equal right-hand values and at least one right-hand expression yields
7484- null, the result of the <token>IN</token> construct will be null, not false.
7485- This is in accordance with SQL's normal rules for Boolean combinations
7486- of null values.
7487- </para>
7488-
7489- <note>
7490- <para>
7491- This form of <token>IN</token> is not truly a subquery expression, but it
7492- seems best to document it in the same place as subquery <token>IN</token>.
7493- </para>
7494- </note>
7495- </sect2>
7496-
7497- <sect2>
7498- <title><literal>IN</literal> (subquery form)</title>
7460+ <title><literal>IN</literal></title>
74997461
75007462<synopsis>
75017463<replaceable>expression</replaceable> IN (<replaceable>subquery</replaceable>)
75027464</synopsis>
75037465
75047466 <para>
7505- The right-hand sideof this form of <token>IN</token> is a parenthesized
7467+ The right-hand side is a parenthesized
75067468 subquery, which must return exactly one column. The left-hand expression
75077469 is evaluated and compared to each row of the subquery result.
75087470 The result of <token>IN</token> is <quote>true</> if any equal subquery row is found.
75387500 </para>
75397501
75407502 <para>
7541- As usual, null values in theexpressions or subquery rows are combined per
7503+ As usual, null values in the rows are combined per
75427504 the normal rules of SQL Boolean expressions. Two rows are considered
75437505 equal if all their corresponding members are non-null and equal; the rows
75447506 are unequal if any corresponding members are non-null and unequal;
@@ -7549,55 +7511,14 @@ OR
75497511 </sect2>
75507512
75517513 <sect2>
7552- <title><literal>NOT IN</literal> (scalar form)</title>
7553-
7554- <synopsis>
7555- <replaceable>expression</replaceable> NOT IN (<replaceable>value</replaceable><optional>, ...</optional>)
7556- </synopsis>
7557-
7558- <para>
7559- The right-hand side of this form of <token>NOT IN</token> is a parenthesized list
7560- of scalar expressions. The result is <quote>true</quote> if the left-hand expression's
7561- result is unequal to all of the right-hand expressions. This is a shorthand
7562- notation for
7563-
7564- <synopsis>
7565- <replaceable>expression</replaceable> <> <replaceable>value1</replaceable>
7566- AND
7567- <replaceable>expression</replaceable> <> <replaceable>value2</replaceable>
7568- AND
7569- ...
7570- </synopsis>
7571- </para>
7572-
7573- <para>
7574- Note that if the left-hand expression yields null, or if there are
7575- no equal right-hand values and at least one right-hand expression yields
7576- null, the result of the <token>NOT IN</token> construct will be null, not true
7577- as one might naively expect.
7578- This is in accordance with SQL's normal rules for Boolean combinations
7579- of null values.
7580- </para>
7581-
7582- <tip>
7583- <para>
7584- <literal>x NOT IN y</literal> is equivalent to <literal>NOT (x IN y)</literal> in all
7585- cases. However, null values are much more likely to trip up the novice when
7586- working with <token>NOT IN</token> than when working with <token>IN</token>.
7587- It's best to express your condition positively if possible.
7588- </para>
7589- </tip>
7590- </sect2>
7591-
7592- <sect2>
7593- <title><literal>NOT IN </literal>(subquery form)</title>
7514+ <title><literal>NOT IN </literal></title>
75947515
75957516<synopsis>
75967517<replaceable>expression</replaceable> NOT IN (<replaceable>subquery</replaceable>)
75977518</synopsis>
75987519
75997520 <para>
7600- The right-hand sideof this form of <token>NOT IN</token> is a parenthesized
7521+ The right-hand side is a parenthesized
76017522 subquery, which must return exactly one column. The left-hand expression
76027523 is evaluated and compared to each row of the subquery result.
76037524 The result of <token>NOT IN</token> is <quote>true</> if only unequal subquery rows
@@ -7633,7 +7554,7 @@ AND
76337554 </para>
76347555
76357556 <para>
7636- As usual, null values in theexpressions or subquery rows are combined per
7557+ As usual, null values in the rows are combined per
76377558 the normal rules of SQL Boolean expressions. Two rows are considered
76387559 equal if all their corresponding members are non-null and equal; the rows
76397560 are unequal if any corresponding members are non-null and unequal;
@@ -7652,7 +7573,7 @@ AND
76527573</synopsis>
76537574
76547575 <para>
7655- The right-hand sideof this form of <token>ANY</token> is a parenthesized
7576+ The right-hand side is a parenthesized
76567577 subquery, which must return exactly one column. The left-hand expression
76577578 is evaluated and compared to each row of the subquery result using the
76587579 given <replaceable>operator</replaceable>, which must yield a Boolean
@@ -7700,7 +7621,7 @@ AND
77007621 </para>
77017622
77027623 <para>
7703- As usual, null values in theexpressions or subquery rows are combined per
7624+ As usual, null values in the rows are combined per
77047625 the normal rules of SQL Boolean expressions. Two rows are considered
77057626 equal if all their corresponding members are non-null and equal; the rows
77067627 are unequal if any corresponding members are non-null and unequal;
@@ -7718,7 +7639,7 @@ AND
77187639</synopsis>
77197640
77207641 <para>
7721- The right-hand sideof this form of <token>ALL</token> is a parenthesized
7642+ The right-hand side is a parenthesized
77227643 subquery, which must return exactly one column. The left-hand expression
77237644 is evaluated and compared to each row of the subquery result using the
77247645 given <replaceable>operator</replaceable>, which must yield a Boolean
@@ -7765,7 +7686,7 @@ AND
77657686 </para>
77667687
77677688 <para>
7768- As usual, null values in theexpressions or subquery rows are combined per
7689+ As usual, null values in the rows are combined per
77697690 the normal rules of SQL Boolean expressions. Two rows are considered
77707691 equal if all their corresponding members are non-null and equal; the rows
77717692 are unequal if any corresponding members are non-null and unequal;
@@ -7780,24 +7701,201 @@ AND
77807701
77817702<synopsis>
77827703(<replaceable>expression</replaceable> <optional>, <replaceable>expression</replaceable> ...</optional>) <replaceable>operator</replaceable> (<replaceable>subquery</replaceable>)
7783- (<replaceable>expression</replaceable> <optional>, <replaceable>expression</replaceable> ...</optional>) <replaceable>operator</replaceable> (<replaceable>expression</replaceable> <optional>, <replaceable>expression</replaceable> ...</optional>)
77847704</synopsis>
77857705
77867706 <para>
7787- The left-hand side is a list of scalar expressions. The right-hand side
7788- can be either a list of scalar expressions of the same length, or a
7789- parenthesized subquery, which must return exactly as many columns as there
7707+ The left-hand side is a list of scalar expressions. The right-hand side is
7708+ a parenthesized subquery, which must return exactly as many columns as there
77907709 are expressions on the left-hand side. Furthermore, the subquery cannot
77917710 return more than one row. (If it returns zero rows, the result is taken to
77927711 be null.) The left-hand side is evaluated and compared row-wise to the
7793- single subquery result row, or to the right-hand expression list.
7712+ single subquery result row.
7713+ Presently, only <literal>=</literal> and <literal><></literal> operators are allowed
7714+ in row-wise comparisons.
7715+ The result is <quote>true</> if the two rows are equal or unequal, respectively.
7716+ </para>
7717+
7718+ <para>
7719+ As usual, null values in the rows are combined per
7720+ the normal rules of SQL Boolean expressions. Two rows are considered
7721+ equal if all their corresponding members are non-null and equal; the rows
7722+ are unequal if any corresponding members are non-null and unequal;
7723+ otherwise the result of the row comparison is unknown (null).
7724+ </para>
7725+ </sect2>
7726+ </sect1>
7727+
7728+
7729+ <sect1 id="functions-comparisons">
7730+ <title>Row and Array Comparisons</title>
7731+
7732+ <indexterm>
7733+ <primary>in</primary>
7734+ </indexterm>
7735+
7736+ <indexterm>
7737+ <primary>not in</primary>
7738+ </indexterm>
7739+
7740+ <indexterm>
7741+ <primary>any</primary>
7742+ </indexterm>
7743+
7744+ <indexterm>
7745+ <primary>all</primary>
7746+ </indexterm>
7747+
7748+ <indexterm>
7749+ <primary>some</primary>
7750+ </indexterm>
7751+
7752+ <para>
7753+ This section describes several specialized constructs for making
7754+ multiple comparisons between groups of values. These forms are
7755+ syntactically related to the subquery forms of the previous section,
7756+ but do not involve subqueries.
7757+ The forms involving array subexpressions are
7758+ <productname>PostgreSQL</productname> extensions; the rest are
7759+ <acronym>SQL</acronym>-compliant.
7760+ All of the expression forms documented in this section return
7761+ Boolean (true/false) results.
7762+ </para>
7763+
7764+ <sect2>
7765+ <title><literal>IN</literal></title>
7766+
7767+ <synopsis>
7768+ <replaceable>expression</replaceable> IN (<replaceable>value</replaceable><optional>, ...</optional>)
7769+ </synopsis>
7770+
7771+ <para>
7772+ The right-hand side is a parenthesized list
7773+ of scalar expressions. The result is <quote>true</> if the left-hand expression's
7774+ result is equal to any of the right-hand expressions. This is a shorthand
7775+ notation for
7776+
7777+ <synopsis>
7778+ <replaceable>expression</replaceable> = <replaceable>value1</replaceable>
7779+ OR
7780+ <replaceable>expression</replaceable> = <replaceable>value2</replaceable>
7781+ OR
7782+ ...
7783+ </synopsis>
7784+ </para>
7785+
7786+ <para>
7787+ Note that if the left-hand expression yields null, or if there are
7788+ no equal right-hand values and at least one right-hand expression yields
7789+ null, the result of the <token>IN</token> construct will be null, not false.
7790+ This is in accordance with SQL's normal rules for Boolean combinations
7791+ of null values.
7792+ </para>
7793+ </sect2>
7794+
7795+ <sect2>
7796+ <title><literal>NOT IN</literal></title>
7797+
7798+ <synopsis>
7799+ <replaceable>expression</replaceable> NOT IN (<replaceable>value</replaceable><optional>, ...</optional>)
7800+ </synopsis>
7801+
7802+ <para>
7803+ The right-hand side is a parenthesized list
7804+ of scalar expressions. The result is <quote>true</quote> if the left-hand expression's
7805+ result is unequal to all of the right-hand expressions. This is a shorthand
7806+ notation for
7807+
7808+ <synopsis>
7809+ <replaceable>expression</replaceable> <> <replaceable>value1</replaceable>
7810+ AND
7811+ <replaceable>expression</replaceable> <> <replaceable>value2</replaceable>
7812+ AND
7813+ ...
7814+ </synopsis>
7815+ </para>
7816+
7817+ <para>
7818+ Note that if the left-hand expression yields null, or if there are
7819+ no equal right-hand values and at least one right-hand expression yields
7820+ null, the result of the <token>NOT IN</token> construct will be null, not true
7821+ as one might naively expect.
7822+ This is in accordance with SQL's normal rules for Boolean combinations
7823+ of null values.
7824+ </para>
7825+
7826+ <tip>
7827+ <para>
7828+ <literal>x NOT IN y</literal> is equivalent to <literal>NOT (x IN y)</literal> in all
7829+ cases. However, null values are much more likely to trip up the novice when
7830+ working with <token>NOT IN</token> than when working with <token>IN</token>.
7831+ It's best to express your condition positively if possible.
7832+ </para>
7833+ </tip>
7834+ </sect2>
7835+
7836+ <sect2>
7837+ <title><literal>ANY</literal>/<literal>SOME</literal> (array)</title>
7838+
7839+ <synopsis>
7840+ <replaceable>expression</replaceable> <replaceable>operator</replaceable> ANY (<replaceable>array expression</replaceable>)
7841+ <replaceable>expression</replaceable> <replaceable>operator</replaceable> SOME (<replaceable>array expression</replaceable>)
7842+ </synopsis>
7843+
7844+ <para>
7845+ The right-hand side is a parenthesized expression, which must yield an
7846+ array value.
7847+ The left-hand expression
7848+ is evaluated and compared to each element of the array using the
7849+ given <replaceable>operator</replaceable>, which must yield a Boolean
7850+ result.
7851+ The result of <token>ANY</token> is <quote>true</> if any true result is obtained.
7852+ The result is <quote>false</> if no true result is found (including the special
7853+ case where the array has zero elements).
7854+ </para>
7855+
7856+ <para>
7857+ <token>SOME</token> is a synonym for <token>ANY</token>.
7858+ </para>
7859+ </sect2>
7860+
7861+ <sect2>
7862+ <title><literal>ALL</literal> (array)</title>
7863+
7864+ <synopsis>
7865+ <replaceable>expression</replaceable> <replaceable>operator</replaceable> ALL (<replaceable>array expression</replaceable>)
7866+ </synopsis>
7867+
7868+ <para>
7869+ The right-hand side is a parenthesized expression, which must yield an
7870+ array value.
7871+ The left-hand expression
7872+ is evaluated and compared to each element of the array using the
7873+ given <replaceable>operator</replaceable>, which must yield a Boolean
7874+ result.
7875+ The result of <token>ALL</token> is <quote>true</> if all comparisons yield true
7876+ (including the special case where the array has zero elements).
7877+ The result is <quote>false</> if any false result is found.
7878+ </para>
7879+ </sect2>
7880+
7881+ <sect2>
7882+ <title>Row-wise Comparison</title>
7883+
7884+ <synopsis>
7885+ (<replaceable>expression</replaceable> <optional>, <replaceable>expression</replaceable> ...</optional>) <replaceable>operator</replaceable> (<replaceable>expression</replaceable> <optional>, <replaceable>expression</replaceable> ...</optional>)
7886+ </synopsis>
7887+
7888+ <para>
7889+ Each side is a list of scalar expressions; the two lists must be
7890+ of the same length. Each side is evaluated and they are compared
7891+ row-wise.
77947892 Presently, only <literal>=</literal> and <literal><></literal> operators are allowed
77957893 in row-wise comparisons.
77967894 The result is <quote>true</> if the two rows are equal or unequal, respectively.
77977895 </para>
77987896
77997897 <para>
7800- As usual, null values in theexpressions or subquery rows are combined per
7898+ As usual, null values in the rows are combined per
78017899 the normal rules of SQL Boolean expressions. Two rows are considered
78027900 equal if all their corresponding members are non-null and equal; the rows
78037901 are unequal if any corresponding members are non-null and unequal;