Re: using EXISTS instead of IN: how?

From: Mike Mascari <mascarm(at)mascari(dot)com>
To: Rory Campbell-Lange <rory(at)campbell-lange(dot)net>
Cc: Postgres General <pgsql-general(at)postgresql(dot)org>
Subject: Re: using EXISTS instead of IN: how?
Date: 2003-07-22 16:39:09
Message-ID: 3F1D68AD.1040103@mascari.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Rory Campbell-Lange wrote:

> I have been informed that at present (postgres 7.3.2) using IN is not
> advised, and I should replace it with EXISTS. I can't seem to get it to
> work.

...

>
> SELECT
> name
> FROM
> people
> WHERE
> exists (
> SELECT
> 1
> FROM
> states
> WHERE
> name ~* 'r'
> );

You should correlate the subquery with the outer query:

SELECT name
FROM people
WHERE EXISTS (
SELECT 1
FROM states
WHERE people.state = states.id AND
states.name ~* 'r'
);

But I don't see why you just don't use a join:

SELECT people.name
FROM people, states
WHERE people.state = states.id AND
states.name ~* 'r';

Hope that helps,

Mike Mascari
mascarm(at)mascari(dot)com

>
> However the second example simply finds all records in people.
>
> Thanks for any help,
> Rory
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Csaba Nagy 2003-07-22 16:39:30 Re: using EXISTS instead of IN: how?
Previous Message Lincoln Yeoh 2003-07-22 16:38:53 Re: A doubt w.r.t WAL