Sub topic 5
INTERSECT: Rows Present in Both Results
This chapter assumes that you can write SELECT queries with WHERE, understand tables, rows, and columns, and know how DISTINCT, ORDER BY, and NULL work.
What `INTERSECT` does
INTERSECT is a set operator that returns rows found in both query results. It compares complete rows, not just one column. By default, it removes duplicate rows from the final result.
Imagine two lists:
customerscontains people who have accounts.newsletter_subscriberscontains people subscribed to a newsletter.
To find email addresses in both lists, write:
SELECT email
FROM customers
INTERSECT
SELECT email
FROM newsletter_subscribers;The first query produces one result set. The second query produces another. INTERSECT keeps only values that occur in both sets.
Making the queries compatible
The two SELECT statements must be union-compatible. This means they must return:
- The same number of columns.
- Corresponding columns with compatible data types.
- Columns in the same order.
The column names usually come from the first query.
For example, this is compatible:
SELECT customer_id, email
FROM customers
INTERSECT
SELECT subscriber_id, email
FROM newsletter_subscribers;Both queries return two columns, and the corresponding identifier and email columns have compatible types. However, matching rows requires both columns to be equal. If you want to compare only email addresses, select only email in both queries.
This is not compatible:
SELECT email
FROM customers
INTERSECT
SELECT subscriber_id, email
FROM newsletter_subscribers;The first query returns one column, while the second returns two.
Duplicates and `NULL`
INTERSECT behaves like a distinct set operation. If an email appears several times in either input, it appears at most once in the result.
Suppose the first query returns:
And the second query returns:
The intersection is:
| [email protected] |
Duplicate counts do not matter for ordinary INTERSECT.
Set operations also treat NULL values as matching for set comparison. Therefore, if both queries contain a row whose corresponding value is NULL, that row can appear in the intersection. This differs from an ordinary comparison such as email = other_email, because NULL = NULL evaluates to unknown rather than true.
Choosing `INTERSECT` or another form
Use INTERSECT when the question is naturally “which complete rows occur in both results?” It makes that intent direct and is often easier to read than a rewritten join.
A semantically similar query for email addresses can use EXISTS:
SELECT DISTINCT c.email
FROM customers AS c
WHERE EXISTS (
SELECT 1
FROM newsletter_subscribers AS s
WHERE s.email = c.email
);These queries can differ when email is NULL. The EXISTS version uses = and therefore does not match two NULL values. The INTERSECT version can include NULL when it appears in both results.
A database may choose different execution plans for these forms. Check the plan when performance matters, especially when the input tables are large. Indexes on the compared columns can help either form.
Some systems support INTERSECT ALL, which preserves duplicate counts. For example, if a value occurs twice in the first result and three times in the second, INTERSECT ALL returns it twice. Support varies, so check your database documentation before using it.
Exercise
What does this query return if customers contains 1, 1, and 2, while newsletter_subscribers contains 1, 2, and 2?
SELECT customerid
FROM customers
INTERSECT
SELECT customerid
FROM newsletter_subscribers;
Show answer
It returns 1 and 2, each once. Ordinary INTERSECT removes duplicates from the combined result.