MySQL 8.0.31 added INTERSECT and EXCEPT to augment the long-lived
UNION operator. That is the good news. The bad news is that you
have to be careful using the EXCEPT operator as there is a trick.
Let’s start with some simple tables and load some simple data.
SQL > create table a (id int, nbr int);
Query OK, 0 rows affected (0.0180 sec)
SQL > create table b (id int, nbr int);
Query OK, 0 rows affected (0.0199 sec)
SQL > insert into a (id,nbr) values (1,10),(3,30),(5,50),(7,70);
Query OK, 4 rows affected (0.0076 sec)
Records: 4 Duplicates: 0 Warnings: 0
SQL > insert into b (id,nbr) values (1,10),(2,20),(3,30),(4,40);
Query OK, 4 rows affected (0.0159 sec)
Records: 4 Duplicates: 0 Warnings: 0
So each table has four rows of data with two rows – (1,10) and
(3,30) – appearing in both.
SQL > select id,nbr from a;
+----+-----+
| id | nbr |
+----+-----+
| 1 | 10 |
| 3 | 30 |
| 5 | 50 |
| 7 | 70 | …
[Read more]