As per what I know, EXISTS returns true when a sub query contains atleast a row whereas NOT EXIST returns true if the subquery returns nothing. So for a given subquery either of the two should return true, right? For eg: 1) This returns as to what kind of store is present in one or more cities?
SELECT DISTINCT store_type FROM stores
WHERE EXISTS
(SELECT *
FROM cities_stores
WHERE cities_stores.store_type = stores.store_type);
2) And this returns as to what kind of store is present in no cities?
SELECT DISTINCT store_type FROM stores
WHERE NOT EXISTS
(SELECT *
FROM cities_stores
WHERE cities_stores.store_type = stores.store_type);
So how can the same sub query give output for both the queries? as one uses EXIST and the other uses NOT EXIST?
I took the examples from http://dev.mysql.com/doc/refman/5.1/en/exists-and-not-exists-subqueries.html
Also how is 2 NOT EXISTS helping here? Isn't this a kind of OR? 3) This returns what kind of store is present in all cities?
SELECT DISTINCT store_type FROM stores s1
WHERE NOT EXISTS (
SELECT * FROM cities
WHERE NOT EXISTS (
SELECT * FROM cities_stores
WHERE cities_stores.city = cities.city
AND cities_stores.store_type = stores.store_type));