Re: Re: Does PostgreSQL support EXISTS? - Mailing list pgsql-general

From Tom Lane
Subject Re: Re: Does PostgreSQL support EXISTS?
Date
Msg-id 26043.992440306@sss.pgh.pa.us
Whole thread Raw
In response to Re: Re: Does PostgreSQL support EXISTS?  (Martijn van Oosterhout <kleptog@svana.org>)
List pgsql-general
Martijn van Oosterhout <kleptog@svana.org> writes:
> On postgres at least, exists is faster than in.
> They are equivalent though.

Not really.

For one thing, IN can return a NULL (don't know) result; EXISTS cannot.

regression=# select * from foo;
 f1
----
  1

(2 rows)

regression=# select 2 in (select f1 from foo);
 ?column?
----------

(1 row)

regression=# select exists (select 1 from foo where f1 = 2);
 ?column?
----------
 f
(1 row)


Yes, this behavior is spec-compliant.  Think: we don't know what the NULL
represents, therefore we don't know whether 2 is in the column or not,
therefore IN should return NULL.

            regards, tom lane

pgsql-general by date:

Previous
From: "Gregory Wood"
Date:
Subject: Re: PLPGSQL: Using Transactions and locks
Next
From: Michael Meskes
Date:
Subject: Re: Re: Does PostgreSQL support EXISTS?