Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.misc -> Re: How do i check if records exits in other tables?
K Allen wrote:
> I have a select statement by a single key that needs to check if that key or
> records with that key exist in another table, If they do fine, return
> 'yes' in a column, If they dont then return 'no'
This is not a wonderful bit of SQL but it will do the job.
SELECT a.field1, 'Yes'
FROM table1 a, table2 b
WHERE a.pk_field = b.pk_field
UNION
SELECT field1, 'No'
FROM table1
WHERE pk_field NOT IN (
SELECT pk_field
FROM table2);
I suspect a few seconds with DECODE and NOT EXISTS would yield a far more elegant result.
Daniel Morgan Received on Mon May 27 2002 - 10:31:45 CDT