I have an issue with not exists
sql query at w3schools
I want to select
all customers that work with shipperid = 1
BUT not shipperid = 3
.
I tried the following:
select o1.customerid, o1.shipperid
from orders o1
where o1.shipperid=1 and not exists
(select o2.customerid from orders o2
where o1.orderid=o2.orderid
and o2.shipperid=3)
order by customerid
;
The above query gives all customers that work with shipperid = 1
and does NOT exclude customers who work with shipperid = 3
. What is not correct with the query. (I need to speifically use not exists
)
PS: I know the in
solution:
select customerid, shipperid
from orders
where shipperid=1 and customerid not in (
select customerid
from orders
where shipperid=3
)
order by customerid;
Why does not the not exists
solution work?