How do I replace NOT EXISTS with JOIN?
Posted
by
YelizavetaYR
on Stack Overflow
See other posts from Stack Overflow
or by YelizavetaYR
Published on 2014-06-10T15:15:41Z
Indexed on
2014/06/10
15:24 UTC
Read the original article
Hit count: 233
I've got the following query:
select distinct a.id, a.name
from Employee a
join Dependencies b on a.id = b.eid
where not exists
(
select *
from Dependencies d
where b.id = d.id
and d.name = 'Apple'
)
and exists
(
select *
from Dependencies c
where b.id = c.id
and c.name = 'Orange'
);
I have two tables, relatively simple. The first Employee has an id column and a name column The second table Dependencies has 3 column, an id, an eid (employee id to link) and names (apple, orange etc).
the data looks like this Employee table looks like this
id | name
-----------
1 | Pat
2 | Tom
3 | Rob
4 | Sam
Dependencies
id | eid | Name
--------------------
1 | 1 | Orange
2 | 1 | Apple
3 | 2 | Strawberry
4 | 2 | Apple
5 | 3 | Orange
6 | 3 | Banana
As you can see Pat has both Orange and Apple and he needs to be excluded and it has to be via joins and i can't seem to get it to work. Ultimately the data should only return Rob
© Stack Overflow or respective owner