Sub topic 6
EXCEPT: rows in the first result but not the second
Assume you can write basic SELECT queries with WHERE, understand tables and columns, and know that NULL means a missing or unknown value. This chapter uses those skills to compare two query results.
What EXCEPT does
EXCEPT returns rows produced by the first query that do not appear in the second query. The order matters:
SELECT email
FROM newsletter_subscribers
EXCEPT
SELECT email
FROM unsubscribed;This query finds newsletter subscribers whose email does not appear in the unsubscribed table. Swapping the queries asks a different question: which unsubscribed emails are not newsletter subscribers?
You can think of EXCEPT as subtraction between result sets:
first result - second result = remaining rowsA set operator combines the results of two or more SELECT statements. The queries on both sides of EXCEPT must be union-compatible.
Making queries compatible
The two queries must return:
- The same number of columns.
- Corresponding columns with compatible data types.
- Columns in the same logical order.
For example, this is compatible because both queries return one text column:
SELECT email
FROM newsletter_subscribers
EXCEPT
SELECT email
FROM unsubscribed;This is not compatible because the first query returns two columns and the second returns one:
SELECT email, signup_date
FROM newsletter_subscribers
EXCEPT
SELECT email
FROM unsubscribed;Column names usually come from the first query. The second query supplies matching values by position, not by column name.
Duplicate rows and NULL values
EXCEPT normally removes duplicate rows. If the first query returns the same email several times, the final result contains that email at most once. Repeated rows in the second query do not change the result.
SELECT email
FROM newsletter_subscribers
EXCEPT
SELECT email
FROM unsubscribed;The result is a distinct list even if either table contains duplicates.
Set operations also compare NULL values differently from ordinary equality predicates. In most SQL systems, EXCEPT treats two NULL values in corresponding positions as equal for duplicate elimination and set comparison. Therefore, a row containing NULL in the first result is removed if a matching row containing NULL appears in the second result.
A rewrite using NOT EXISTS may behave differently:
SELECT DISTINCT s.email
FROM newsletter_subscribers AS s
WHERE NOT EXISTS (
SELECT 1
FROM unsubscribed AS u
WHERE u.email = s.email
);The equality condition u.email = s.email is not true when both values are NULL. If NULL emails are possible, choose the form whose comparison rules match your requirement.
Ordering and several operations
An ORDER BY normally belongs after the complete set operation:
SELECT email
FROM newsletter_subscribers
EXCEPT
SELECT email
FROM unsubscribed
ORDER BY email;This sorts the final remaining rows. A LIMIT or similar row-count clause is also commonly placed at the end, although exact syntax varies by database.
When several set operators appear together, use parentheses to show the intended order:
(SELECT email
FROM newsletter_subscribers
UNION
SELECT email
FROM customers)
EXCEPT
SELECT email
FROM unsubscribed;This first combines subscribers and customers, then removes unsubscribed emails. Parentheses avoid relying on database-specific operator precedence.
Dialect differences
PostgreSQL, SQL Server, and SQLite support EXCEPT. Oracle traditionally uses MINUS for the same basic operation:
SELECT email
FROM newsletter_subscribers
MINUS
SELECT email
FROM unsubscribed;Check your database documentation before relying on EXCEPT ALL. Some systems do not support it.
Exercise
What does this query return: SELECT email FROM customers EXCEPT SELECT email FROM newsletter_subscribers?
Show answer
It returns each distinct email found in customers but not found in newsletter_subscribers. The result is different if the two queries are swapped.