typestar

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

  1. EXCEPT keeps left rows with no match on the right.
  2. INTERSECT keeps only the rows present in both.
  3. Both compare whole rows, so select just the key you mean.

Keywords and builtins used here

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.

Type this snippet

Step 3 of 3 in Set operations, step 20 of 22 in Joins & aggregation.

← Previous Next →