Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.misc -> Re: ORACLE 8i Newbie DISTINCT and EXISTS problem

Re: ORACLE 8i Newbie DISTINCT and EXISTS problem

From: Odd Morten Sveås <odd.morten.sveas_at_accenture.com>
Date: 26 Sep 2002 12:33:16 -0700
Message-ID: <4306a83.0209261133.2c75fefb@posting.google.com>


hampsonpaul80_at_hotmail.com (stimpy) wrote in message news:<4ded334d.0209260212.187a2fc_at_posting.google.com>...
> Hi
> I'm a newbie and I am trying to get the common records between two
> large tables
> using the following query:
>
> Select distinct (a.begval) from table_1 a
> where exists (select distinct (b.begval) from table_2 b);
>

Two things:

1 Try some docs (You can dowload the sql reference from otn.oracle.com) 2 Keep it simple (Dont use exist an lot of subqueries)

Of cource you get all 15 rows. (Eventualy none) You select only from table_1 where TRUE.

Explanation:
The exist clause return true if the folowing subquery returns one or more rows. So if you have rows in table_2 then the exist clause is true and your sql becoms:

select distinct (a.begval)
from table_1 a
where true.

What you should use is a plain join.

select distinct a.begval
from table_1 a
, table_2 b
where a.begval = b.begval; Received on Thu Sep 26 2002 - 14:33:16 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US