Using EXISTS and NOT EXISTS to Find Intersection and Difference

Subqueries introduced with EXISTS and NOT EXISTS can be used for two set theory operations: intersection and difference. The intersection of two sets contains all elements that belong to both of the original sets. The difference contains elements that belong only to the first of the two sets.

The intersection of authors and publishers over the city column is the set of cities in which both an author and a publisher are located:

SELECT DISTINCT city
FROM authors
WHERE EXISTS
    (SELECT *
    FROM publishers
    WHERE authors.city = publishers.city)

city
--------
Berkeley

(1 row(s) affected)

This query is included for illustrative purposes. Of course, this query could be written as a simple join:

SELECT DISTINCT authors.city
FROM authors, publishers
WHERE authors.city = publishers.city

The difference between authors and publishers over the city column is the set of cities where an author lives but no publisher is located ¾ that is, all the cities except Berkeley:

SELECT DISTINCT city
FROM authors
WHERE not exists
    (SELECT *
    FROM publishers
    WHERE authors.city = publishers.city)

This query could also be written as:

SELECT DISTINCT city
FROM authors
WHERE city NOT IN
SELECT city FROM publishers