バージョンごとのドキュメント一覧

9.23. 副問い合わせ式 #

<title>Subquery Expressions</title>

This section describes the <acronym>SQL</acronym>-compliant subquery expressions available in <productname>PostgreSQL</productname>. All of the expression forms documented in this section return Boolean (true/false) results. 本節ではPostgreSQLで使用できるSQL準拠の副問い合わせについて説明します。 本節で記載した全ての式は結果として論理値(真/偽)を返します。

9.23.1. EXISTS #

EXISTS (subquery)

The argument of <token>EXISTS</token> is an arbitrary <command>SELECT</command> statement, or <firstterm>subquery</firstterm>. The subquery is evaluated to determine whether it returns any rows. If it returns at least one row, the result of <token>EXISTS</token> is <quote>true</quote>; if the subquery returns no rows, the result of <token>EXISTS</token> is <quote>false</quote>. EXISTSの引数は、任意のSELECT文、つまり副問い合わせです。 副問い合わせはそれが何らかの行を返すか否かの決定のために評価されます。 もし1つでも行を返すのであれば、EXISTSの結果はtrue(真)となり、副問い合わせが行を返さない場合、EXISTSの結果はfalse(偽)となります。

The subquery can refer to variables from the surrounding query, which will act as constants during any one evaluation of the subquery. 副問い合わせは、取り囲んでいる問い合わせから変数を参照することができ、その値は副問い合わせの評価時には定数として扱われます。

The subquery will generally only be executed long enough to determine whether at least one row is returned, not all the way to completion. It is unwise to write a subquery that has side effects (such as calling sequence functions); whether the side effects occur might be unpredictable. この副問い合わせは通常、最後まで実行されず、少なくとも1つの行が返されたかどうかを判定し得るに足りる時点まで実行されます。 (シーケンス関数を呼び出すような)副作用のある副問い合わせを記述することは配慮不足です。副作用が生じるかどうかは予想できません。

Since the result depends only on whether any rows are returned, and not on the contents of those rows, the output list of the subquery is normally unimportant. A common coding convention is to write all <literal>EXISTS</literal> tests in the form <literal>EXISTS(SELECT 1 WHERE ...)</literal>. There are exceptions to this rule however, such as subqueries that use <token>INTERSECT</token>. 結果は何らかの行が返されるのかのみに依存し、それらの行の内容には依存しないことから、副問い合わせの出力リストは通常重要ではありません。 よく使われるコーディング規約は、全てのEXISTSテストをEXISTS(SELECT 1 WHERE ...)といった形式で記述することです。 とは言っても、INTERSECTを使う副問い合わせのようにこの規則には例外があります。

This simple example is like an inner join on <literal>col2</literal>, but it produces at most one output row for each <literal>tab1</literal> row, even if there are several matching <literal>tab2</literal> rows: 以下の簡単な例はcol2上の内部結合に似ていますが、たとえtab2の行といくつか一致したとしてもtab1のそれぞれの行に対して最大限1つの出力行を生成します。

SELECT col1
FROM tab1
WHERE EXISTS (SELECT 1 FROM tab2 WHERE col2 = tab1.col2);

9.23.2. IN #

expression IN (subquery)

The right-hand side is a parenthesized subquery, which must return exactly one column. The left-hand expression is evaluated and compared to each row of the subquery result. The result of <token>IN</token> is <quote>true</quote> if any equal subquery row is found. The result is <quote>false</quote> if no equal row is found (including the case where the subquery returns no rows). 右辺は括弧で括られた副問い合わせで、正確に1列を返すものでなければなりません。 左辺式は評価され、副問い合わせの結果行と比較されます。 副問い合わせの行のどれかと等しい場合、INの結果はtrue(真)です。 (副問い合わせが行を返さない場合を含め)等しい行が見つからない場合、結果はfalse(偽)です。

Note that if the left-hand expression yields null, or if there are no equal right-hand values and at least one right-hand row yields null, the result of the <token>IN</token> construct will be null, not false. This is in accordance with SQL's normal rules for Boolean combinations of null values. 左辺の式がNULLを生じる場合、または右側の値に等しいものがなくて少なくとも1つの右辺の行がNULLを持つ場合、IN構文の結果は偽ではなくNULLとなることに注意してください。 これは、NULL値の論理的な組み合わせに対するSQLの標準規則に従うものです。

As with <token>EXISTS</token>, it's unwise to assume that the subquery will be evaluated completely. EXISTSと同様、副問い合わせが完全に評価されることを前提としてはなりません。

row_constructor IN (subquery)

The left-hand side of this form of <token>IN</token> is a row constructor, as described in <xref linkend="sql-syntax-row-constructors"/>. The right-hand side is a parenthesized subquery, which must return exactly as many columns as there are expressions in the left-hand row. The left-hand expressions are evaluated and compared row-wise to each row of the subquery result. The result of <token>IN</token> is <quote>true</quote> if any equal subquery row is found. The result is <quote>false</quote> if no equal row is found (including the case where the subquery returns no rows). INのこの形式の左辺は、4.2.13で説明する、行のコンストラクタです。 右辺は括弧で括られた副問い合わせで、左辺の行にある式の数と正確に同じ数の列を返さなければなりません。 左辺の式は副問い合わせの結果のそれぞれの行に対し、行に関して評価、比較が行われます。 副問い合わせの行に等しいものが見つかった場合、IN の結果はtrue(真)となります。 (副問い合わせが行を返さない場合を含め)等しい行が見つからない場合、結果はfalse(偽)です。

As usual, null values in the rows are combined per the normal rules of SQL Boolean expressions. Two rows are considered equal if all their corresponding members are non-null and equal; the rows are unequal if any corresponding members are non-null and unequal; otherwise the result of that row comparison is unknown (null). If all the per-row results are either unequal or null, with at least one null, then the result of <token>IN</token> is null. 通常通り、行にあるNULL値はSQLの論理式の標準規則で結合されます。 2つの行は対応する全ての構成要素が非NULLかつ等しい場合に等しいとみなされます。 1つでも対応する構成要素が非NULLかつ等しくないものがあれば、2つの行は等しくないとみなされます。 それ以外の場合、その行の比較結果は不明(NULL)です。 行毎の結果すべてが不等もしくはNULLの場合、少なくとも1つのNULLがあると、INの結果はNULLとなります。

9.23.3. NOT IN #

expression NOT IN (subquery)

The right-hand side is a parenthesized subquery, which must return exactly one column. The left-hand expression is evaluated and compared to each row of the subquery result. The result of <token>NOT IN</token> is <quote>true</quote> if only unequal subquery rows are found (including the case where the subquery returns no rows). The result is <quote>false</quote> if any equal row is found. 右辺は括弧で括られた副問い合わせで、正確に1つの列を返さなければなりません。 左辺の式は副問い合わせ結果の行それぞれに対して評価、比較されます。 等しくない副問い合わせの行だけがある(副問い合わせが行を返さない場合を含む)と、NOT INの結果はtrue(真)です。 等しい行が1つでもあれば、結果はfalse(偽)です。

Note that if the left-hand expression yields null, or if there are no equal right-hand values and at least one right-hand row yields null, the result of the <token>NOT IN</token> construct will be null, not true. This is in accordance with SQL's normal rules for Boolean combinations of null values. 左辺の式でNULLが生じる場合、または右辺の値に等しいものがなく、少なくとも1つの右辺の式がNULLを生み出す場合、NOT IN構文の結果は真ではなくNULLとなることに注意してください。 これは、NULL値の論理的な組み合わせに対するSQLの標準規則に従うものです。

As with <token>EXISTS</token>, it's unwise to assume that the subquery will be evaluated completely. EXISTSと同様、副問い合わせが完全に評価されることを前提としてはなりません。

row_constructor NOT IN (subquery)

The left-hand side of this form of <token>NOT IN</token> is a row constructor, as described in <xref linkend="sql-syntax-row-constructors"/>. The right-hand side is a parenthesized subquery, which must return exactly as many columns as there are expressions in the left-hand row. The left-hand expressions are evaluated and compared row-wise to each row of the subquery result. The result of <token>NOT IN</token> is <quote>true</quote> if only unequal subquery rows are found (including the case where the subquery returns no rows). The result is <quote>false</quote> if any equal row is found. NOT INのこの形式の左辺は、4.2.13で説明する行コンストラクタです。 右辺は括弧で括られた副問い合わせで、左辺の行にある式の数と正確に同じ数の列を返さなければなりません。 左辺の式は副問い合わせの結果のそれぞれの行に対し、評価、比較が行われます。 副問い合わせの行に不等のもののみが見つかった場合(副問い合わせが行を返さない場合を含む)、NOT INの結果はtrue(真)となります。 等しい行が1つでも見つかった場合、結果はfalse(偽)です。

As usual, null values in the rows are combined per the normal rules of SQL Boolean expressions. Two rows are considered equal if all their corresponding members are non-null and equal; the rows are unequal if any corresponding members are non-null and unequal; otherwise the result of that row comparison is unknown (null). If all the per-row results are either unequal or null, with at least one null, then the result of <token>NOT IN</token> is null. 通常通り、行にあるNULL値はSQLの論理式の標準規則で結合されます。 2つの行は対応する全ての構成要素が非NULLかつ等しい場合に等しいとみなされます。 1つでも構成要素が非NULLかつ等しくない場合、2つの行は等しくないとみなされます。 それ以外の場合、その行の比較結果は不明(NULL)です。 行毎の結果すべてが不等もしくはNULLの場合、少なくとも1つのNULLがあると、NOT INの結果はNULLとなります。

9.23.4. ANY/SOME #

expression operator ANY (subquery)
expression operator SOME (subquery)

The right-hand side is a parenthesized subquery, which must return exactly one column. The left-hand expression is evaluated and compared to each row of the subquery result using the given <replaceable>operator</replaceable>, which must yield a Boolean result. The result of <token>ANY</token> is <quote>true</quote> if any true result is obtained. The result is <quote>false</quote> if no true result is found (including the case where the subquery returns no rows). 右辺は括弧で括られた副問い合わせで、正確に1つの列を返さなければなりません。 左辺の式は副問い合わせの結果行それぞれに対して、指定されたoperatorを使用して評価、比較されます。なお、operatorは結果として論理値を生成する必要があります。 真の結果が1つでもあると、ANYの結果はtrue(真)です。 真の結果がない(副問い合わせが行を返さない場合を含む)と、結果はfalse(偽)です。

<token>SOME</token> is a synonym for <token>ANY</token>. <token>IN</token> is equivalent to <literal>= ANY</literal>. SOMEANYの同義語です。 IN= ANYと等価です。

Note that if there are no successes and at least one right-hand row yields null for the operator's result, the result of the <token>ANY</token> construct will be null, not false. This is in accordance with SQL's normal rules for Boolean combinations of null values. 成功がなく、右辺の行が演算子の結果として1つでもNULLを生成した場合、ANY構文の結果は偽ではなくNULLになることに注意してください。 これは、NULL値の論理的な組み合わせに対するSQLの標準規則に従うものです。

As with <token>EXISTS</token>, it's unwise to assume that the subquery will be evaluated completely. EXISTSと同様、副問い合わせが完全に評価されることを前提としてはなりません。

row_constructor operator ANY (subquery)
row_constructor operator SOME (subquery)

The left-hand side of this form of <token>ANY</token> is a row constructor, as described in <xref linkend="sql-syntax-row-constructors"/>. The right-hand side is a parenthesized subquery, which must return exactly as many columns as there are expressions in the left-hand row. The left-hand expressions are evaluated and compared row-wise to each row of the subquery result, using the given <replaceable>operator</replaceable>. The result of <token>ANY</token> is <quote>true</quote> if the comparison returns true for any subquery row. The result is <quote>false</quote> if the comparison returns false for every subquery row (including the case where the subquery returns no rows). The result is NULL if no comparison with a subquery row returns true, and at least one comparison returns NULL. ANYのこの形式の左辺は、4.2.13で説明されている行コンストラクタです。 右辺は括弧で括られた副問い合わせで、左辺の行にある式の数と正確に同じ数の列を返さなければなりません。 左辺の式は副問い合わせの結果のそれぞれの行に対し、与えられたoperatorを使用して行に関する評価、比較が行われます。 比較の結果、副問い合わせの行のどれかに対して真となる場合、ANYの結果はtrue(真)です。 比較の結果、副問い合わせの全ての行に対して偽となる場合(副問い合わせが行を返さないという場合も含む)、結果はfalse(偽)です。 いかなる副問合せ行との比較の結果も偽を返さず、かつ、少なくとも1つの比較がNULLを返す場合、結果はNULLになります。

See <xref linkend="row-wise-comparison"/> for details about the meaning of a row constructor comparison. 行コンストラクタ比較の意味についての詳細は9.24.5を参照して下さい。

9.23.5. ALL #

expression operator ALL (subquery)

The right-hand side is a parenthesized subquery, which must return exactly one column. The left-hand expression is evaluated and compared to each row of the subquery result using the given <replaceable>operator</replaceable>, which must yield a Boolean result. The result of <token>ALL</token> is <quote>true</quote> if all rows yield true (including the case where the subquery returns no rows). The result is <quote>false</quote> if any false result is found. The result is NULL if no comparison with a subquery row returns false, and at least one comparison returns NULL. 右辺は括弧で括られた副問い合わせで、正確に1つの列を返さなければなりません。 左辺の式は副問い合わせの結果行それぞれに対して、指定されたoperatorを使用して評価、比較されます。なお、operatorは結果として論理値を生成する必要があります。 全ての行が真になる場合(副問い合わせが行を返さない場合を含む)、ALLの結果はtrue(真)です。 1つでも偽の結果があると、結果はfalse(偽)です。 比較がどの行でも偽を返さず、かつ、少なくとも1つの行でNULLを返した場合、結果はNULLとなります。

<token>NOT IN</token> is equivalent to <literal>&lt;&gt; ALL</literal>. NOT IN<> ALLと等価です。

As with <token>EXISTS</token>, it's unwise to assume that the subquery will be evaluated completely. EXISTSと同様、副問い合わせが完全に評価されることを前提としてはなりません。

row_constructor operator ALL (subquery)

The left-hand side of this form of <token>ALL</token> is a row constructor, as described in <xref linkend="sql-syntax-row-constructors"/>. The right-hand side is a parenthesized subquery, which must return exactly as many columns as there are expressions in the left-hand row. The left-hand expressions are evaluated and compared row-wise to each row of the subquery result, using the given <replaceable>operator</replaceable>. The result of <token>ALL</token> is <quote>true</quote> if the comparison returns true for all subquery rows (including the case where the subquery returns no rows). The result is <quote>false</quote> if the comparison returns false for any subquery row. The result is NULL if no comparison with a subquery row returns false, and at least one comparison returns NULL. ALLのこの形式の左辺は、4.2.13で説明する行コンストラクタです。 右辺は括弧で括られた副問い合わせで、左辺の行にある式の数と正確に同じ数の列を返さなければなりません。 左辺の式は副問い合わせの結果のそれぞれの行に対し、与えられたoperatorを使用して行に関する評価、比較が行われます。 比較した結果、すべての副問い合わせ行に対して真を返す場合(副問い合わせが行を返さないという場合も含む)、ALLの結果はtrue(真)となります。 比較した結果、いずれかの副問い合わせ行で偽を返す場合、この結果はfalse(偽)となります。 比較結果がすべての副問い合わせ行に対して偽を返さず、少なくとも1行でNULLを返す場合、結果はNULLとなります。

See <xref linkend="row-wise-comparison"/> for details about the meaning of a row constructor comparison. 行コンストラクタに関する比較の意味については9.24.5を参照してください。

9.23.6. 単独行に関する比較 #

<title>Single-Row Comparison</title>
row_constructor operator (subquery)

The left-hand side is a row constructor, as described in <xref linkend="sql-syntax-row-constructors"/>. The right-hand side is a parenthesized subquery, which must return exactly as many columns as there are expressions in the left-hand row. Furthermore, the subquery cannot return more than one row. (If it returns zero rows, the result is taken to be null.) The left-hand side is evaluated and compared row-wise to the single subquery result row. 左辺は、4.2.13で説明されている行コンストラクタです。 右辺は括弧で括られた副問い合わせで、左辺の行とまったく同じ数の列を返さなければなりません。さらに、副問い合わせは複数行を返すことはできません。 (行をまったく返さない場合、結果はNULLとみなされます。) 左辺は副問い合わせの結果の単一行に対し行全体で評価、比較が行われます。

See <xref linkend="row-wise-comparison"/> for details about the meaning of a row constructor comparison. 行コンストラクタに関する比較の意味についての詳細は9.24.5を参照してください。