Example 1
Find customers who appear in both the previous and current year
Alice and Carol appear in both tables. Bob is only in prev_customers and Dave is only in curr_customers, so neither appears in the intersection. Use EXCEPT to find customers who were in the previous year but not the current one.
CREATE TABLE prev_customers (id INT, name VARCHAR(50));
CREATE TABLE curr_customers (id INT, name VARCHAR(50));
INSERT INTO
prev_customers (id, name)
VALUES
(1, 'Alice'),
(2, 'Bob'),
(3, 'Carol');
INSERT INTO
curr_customers (id, name)
VALUES
(1, 'Alice'),
(4, 'Dave'),
(3, 'Carol');SELECT
name
FROM
prev_customers
INTERSECT
SELECT
name
FROM
curr_customers
ORDER BY
name;| name |
|---|
| Alice |
| Carol |
Output is identical across all engines.