Difference and Intersection: EXCEPT and INTERSECT
The List Comparison
Marketing asked: *"We have a list of all newsletter subscribers and a list of paying customers. How many subscribers are NOT customers? And how many are both?"*
The Quest: Set Operations
SQL has set operations that work like Venn diagrams.
The Implementation: The Syntax
Who Subscribe But Never Paid?
SELECT email FROM newsletter_subscribers
EXCEPT
SELECT email FROM paying_customers;
Who Are Both Subscribers AND Customers?
SELECT email FROM newsletter_subscribers
INTERSECT
SELECT email FROM paying_customers;
The "Oops" Moment
I forgot that `EXCEPT` is directional. `A EXCEPT B` is different from `B EXCEPT A`.
**Pro Tip**: Think of `EXCEPT` as "Give me everything from the first set, but remove anything that's also in the second."
The Victory
Marketing discovered that 2,000 subscribers had never converted. They launched a targeted discount campaign and saw a 10% conversion uplift. Set operations are perfect for list analysis.
Your Task for Today
Use `EXCEPT` to find users who are in Table A but not in Table B. Then, use `INTERSECT` to find users who are in both.
*Day 46: Introduction to Subqueries.*