Sub topic 10
Equivalent Queries with Joins and EXISTS
Assume you can write SELECT queries with WHERE, understand DISTINCT, and know that NULL means an unknown or missing value. Set operators are often the clearest way to compare two result sets, but joins and EXISTS can express the same questions.
Use `EXISTS` for membership
INTERSECT keeps rows that occur in both query results. For example:
SELECT email FROM customers
INTERSECT
SELECT email FROM newsletter_subscribers;You can express the same membership test with EXISTS:
SELECT DISTINCT c.email
FROM customers c
WHERE EXISTS (
SELECT 1
FROM newsletter_subscribers s
WHERE s.email = c.email
);The inner query is a correlated subquery. It refers to c.email from the outer query. For each customer row, EXISTS is true when at least one subscriber has the same email.
DISTINCT matters here. INTERSECT returns distinct rows, but the outer query could contain the same email more than once.
An inner join is another common rewrite:
SELECT DISTINCT c.email
FROM customers c
JOIN newsletter_subscribers s
ON s.email = c.email;The join combines matching rows. If a customer email matches three subscriber rows, the join can produce three rows. DISTINCT removes those repeated email values.
Use `NOT EXISTS` for exclusion
EXCEPT returns rows from its first result that do not occur in its second result:
SELECT email FROM newsletter_subscribers
EXCEPT
SELECT email FROM unsubscribed;The NOT EXISTS form is:
SELECT DISTINCT s.email
FROM newsletter_subscribers s
WHERE NOT EXISTS (
SELECT 1
FROM unsubscribed u
WHERE u.email = s.email
);This reads as “return each subscriber email for which no matching unsubscribe row exists.” The order is important. Reversing the outer and inner tables changes the question.
A LEFT JOIN can also express this anti-match:
SELECT DISTINCT s.email
FROM newsletter_subscribers s
LEFT JOIN unsubscribed u
ON u.email = s.email
WHERE u.email IS NULL;This works when unsubscribed.email is non-null. If the joined column can itself be NULL, the test can become ambiguous. NOT EXISTS usually states the intent more directly.
Preserve the meaning of the comparison
For one column, the comparison is simple. For several columns, every corresponding column must match:
SELECT DISTINCT a.account_id, a.region
FROM active_accounts a
WHERE EXISTS (
SELECT 1
FROM trial_accounts t
WHERE t.account_id = a.account_id
AND t.region = a.region
);This corresponds to an INTERSECT between two two-column results. Omitting region would produce a different result.
NULL needs special care. In ordinary SQL comparison, NULL = NULL is not true. Therefore, this query does not treat two null emails as a match:
WHERE u.email = s.emailSet operators generally treat corresponding NULL values as equal for duplicate elimination and row matching. If your database supports it, use a null-safe comparison such as IS NOT DISTINCT FROM:
WHERE u.email IS NOT DISTINCT FROM s.emailOtherwise, write an explicit null condition:
WHERE u.email = s.email
OR (u.email IS NULL AND s.email IS NULL)Choose the clearest form
| Need | Preferred form | Reason |
|---|---|---|
| Combine complete result sets | `UNION`, `INTERSECT`, or `EXCEPT` | States set logic directly |
| Test whether a related row exists | `EXISTS` | Avoids duplicate matches |
| Exclude matching rows | `NOT EXISTS` | Expresses anti-matching clearly |
| Return columns from both sides | `JOIN` | Makes matched columns available |
The database optimizer may turn these forms into similar execution plans. Choose based on meaning first, then inspect the plan if performance matters.
Exercise
Rewrite SELECT email FROM customers INTERSECT SELECT email FROM newsletter_subscribers using EXISTS, preserving distinct results.
Show answer
SELECT DISTINCT c.email FROM customers c WHERE EXISTS (SELECT 1 FROM newsletter_subscribers s WHERE s.email = c.email);