EXCEPT and INTERSECT in SQL
Set difference and set overlap, straight from relational algebra.
SELECT email
FROM newsletter_signups
EXCEPT
SELECT email
FROM customers;
SELECT email
FROM newsletter_signups
INTERSECT
SELECT email
FROM customers;
How it works
EXCEPTkeeps left rows with no match on the right.INTERSECTkeeps only the rows present in both.- Both compare whole rows, so select just the key you mean.
Keywords and builtins used here
EXCEPTFROMINTERSECTSELECT
The run, in numbers
- Lines
- 11
- Characters to type
- 149
- Tokens
- 20
- Three-star pace
- 90 tpm
At the three-star pace of 90 tokens a minute, this run takes about 13 seconds.
Step 3 of 3 in Set operations, step 20 of 22 in Joins & aggregation.