SQLite EXISTS

Summary: in this tutorial, you will learn how to use the SQLite EXISTS operator to test for the existence of rows returned by a subquery.

Introduction to SQLite EXISTS operator


The EXISTS operator is a logical operator that checks whether a subquery returns any row.

概要:このチュートリアルでは、SQLite EXISTS 演算子を使用して、副問い合わせが返す行の存在をテストする方法を学びます。
SQLite EXISTS 演算子の紹介
EXISTS 演算子は、副問い合わせが何らかの行を返すかどうかを検査する論理演算子です。

Here is the basic syntax of the EXISTS operator:

以下にEXISTS演算子の基本的な構文を示します。
EXISTS(subquery)


In this syntax, the subquery is a SELECT statement that returns zero or more rows.

この構文では、副問い合わせは0行以上の行を返すSELECT文です。

If the subquery returns one or more row, the EXISTS operator return true. 

Otherwise, the EXISTS operator returns false or NULL.

副問い合わせが1つ以上の行を返した場合、EXISTS演算子はtrueを返します。 
そうでない場合、EXISTS演算子は偽またはNULLを返します。

Note that if the subquery returns one row with NULL, the result of the EXISTS operator is still true because the result set contains one row with NULL.

副問い合わせがNULLを含む1つの行を返しても、結果セットにはNULLを含む1つの行が含まれているため、EXISTS演算子の結果は真であることに注意してください。

To negate the EXISTS operator, you use the NOT EXISTS operator as follows:

EXISTS演算子を否定するには、次のようにNOT EXISTS演算子を使います。
NOT EXISTS (subquery)

The NOT EXISTS operator returns true if the subquery returns no row.

NOT EXISTS演算子は、副問い合わせが行を返さない場合、真を返します。






SQLite EXISTS operator example

See the following Customers and Invoices tables from the sample database:

サンプルデータベースの次のCustomsテーブルとInvoicesテーブルをご覧ください。

The following statement finds customers who have invoices:

次の文は、請求書を持っている顧客を検索します。

画像1

The following picture shows the partial result set:

次の図は、部分的な結果セットを示しています。

画像2

In this example, for each customer, the EXISTS operator checks if the customer id exists in the invoices table.

この例では、各顧客について、EXISTS演算子は顧客IDがinvoicesテーブルに存在するかどうかを確認します。

If yes, the subquery returns one row with value 1 that causes the EXISTS operator evaluate to true. Therefore, the query includes the curstomer in the result set.

存在する場合、副問い合わせは値1を持つ1つの行を返し、EXISTS演算子の評価を真にします。したがって、この問い合わせは結果セットに顧客を含みます。

In case the customer id does not exist in the Invoices table, the subquery returns no rows which causes the EXISTS operator to evaluate to false, hence the query does not include the customer in the result set.

顧客IDがInvoicesテーブルに存在しない場合、副問い合わせはEXISTS演算子の評価を偽にする行を返さないため、問い合わせは顧客を結果セットに含めません
Notice that you can use the IN operator instead of EXISTS operator in this case to achieve the same result:




この場合、EXISTS演算子の代わりにIN演算子を使用しても同じ結果になることに注意してください。

画像3

Once the subquery returns the first row, the EXISTS operator stops searching because it can determine the result. 

On the other hand, the IN operator must scan all rows returned by the subquery to determine the result.

副問い合わせが最初の行を返すと、EXISTS演算子は結果を決定できるため、検索を停止します。
一方、IN演算子は結果を決定するために副問い合わせが返す全ての行をスキャンしなければなりません。

Generally speaking, the EXISTS operator is faster than IN operator if the result set returned by the subquery is large. By contrast, the IN operator is faster than the EXISTS operator if the result set returned by the subquery is small.


一般的に、副問い合わせが返す結果セットが大きい場合、EXISTS演算子はIN演算子よりも高速です。対照的に、副問い合わせが返す結果セットが小さい場合、IN演算子はEXISTS演算子よりも高速です。


SQLite NOT EXISTS operator example

この記事が気に入ったらサポートをしてみませんか?