How to use not exists in a sql query with w3schools? -
i have issue not exists
sql query @ w3schools
i want select
customers work shipperid = 1
not shipperid = 3
. tried following:
select o1.customerid, o1.shipperid orders o1 o1.shipperid=1 , not exists (select o2.customerid orders o2 o1.orderid=o2.orderid , o2.shipperid=3) order customerid ;
the above query gives customers work shipperid = 1
, not exclude customers work shipperid = 3
. not correct query. (i need speifically use not exists
)
ps: know in
solution:
select customerid, shipperid orders shipperid=1 , customerid not in ( select customerid orders shipperid=3 ) order customerid;
why not not exists
solution work?
i'm problem lies in way you're joining correlated subquery, on orderid = orderid. i'm not familiar dataset, seems surprising same order have different shippers, , adds condition not found in 'correct' answer. should work:
select o1.customerid ,o1.shipperid orders o1 o1.shipperid = 1 , not exists ( select o2.orderid orders o2 o1.customerid = o2.customerid , o2.shipperid = 3) order customerid ;
Comments
Post a Comment