mysqldude

Implementing MINUS in MySQL

Version compatibility

StepMySQLMariaDB
5.65.78.08.4910.610.1111.411.8
LEFT JOIN ... IS NULL emulation (original)
original post
✓✓✓✓✓✓✓✓✓
Native EXCEPT (modern)
modern rewrite
✗ 1064✗ 1064✓✓✓✓✓✓✓
MINUS keyword, default sql_mode
version check
✗ 1064✗ 1064✗ 1064✗ 1064✗ 1064✗ 1064✗ 1064✗ 1064✗ 1064
MINUS keyword with sql_mode = ORACLE (MariaDB)
version check
✗ 1064✗ 1064✗ 1231✗ 1231✗ 1231✓✓✓✓
LEFT JOIN ... IS NULL emulation (original) original post
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
Native EXCEPT (modern) modern rewrite
MySQL 5.6✗ 10645.7✗ 10648.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
MINUS keyword, default sql_mode version check
MySQL 5.6✗ 10645.7✗ 10648.0✗ 10648.4✗ 10649✗ 1064
MariaDB 10.6✗ 106410.11✗ 106411.4✗ 106411.8✗ 1064
MINUS keyword with sql_mode = ORACLE (MariaDB) version check
MySQL 5.6✗ 10645.7✗ 10648.0✗ 12318.4✗ 12319✗ 1231
MariaDB 10.6✓10.11✓11.4✓11.8✓
✓works ✓ 1062fails with the error the step demonstrates ⚠works with a warning ≠runs, result differs ✗fails Tested 2026-11-23 on 5.6.51, 5.7.44, 8.0.46, 8.4.13, 9.7.4, 10.6.28, 10.11.19, 11.4.13, 11.8.9
What the warnings and failures say (5)
  • Native EXCEPT (modern) (MySQL 5.6, MySQL 5.7): error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'EXCEPT SELECT n, title FROM say2 WHERE n IN (
  • MINUS keyword, default sql_mode (MySQL 5.6, MySQL 5.7, MySQL 8.0, MySQL 8.4, MySQL 9): error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'MINUS SELECT n, title FROM say2 WHERE n IN (5
  • MINUS keyword, default sql_mode (MariaDB 10.6, MariaDB 10.11, MariaDB 11.4, MariaDB 11.8): error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'MINUS SELECT n, title FROM say2 WHERE n IN
  • MINUS keyword with sql_mode = ORACLE (MariaDB) (MySQL 5.6, MySQL 5.7): error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'MINUS SELECT n, title FROM say2 WHERE n IN (5
  • MINUS keyword with sql_mode = ORACLE (MariaDB) (MySQL 8.0, MySQL 8.4, MySQL 9): error 1231: Variable 'sql_mode' can't be set to the value of 'ORACLE'

Related errors: 1064

The original article

First published 2012-06-15. The text below is the original; the table above shows how it behaves on current versions.

This post is to show how we can implement Oracle’s MINUS set operator on sets of data using SQL. For an example of how the MINUS operator works in Oracle click here

The MINUS operator give the following result set. Consider the following, let’s imagine we have a result set A in blue and we want to get only those entries that are not in result set B. From the image below the resultset we are looking for is the set of data visible in blue.

Let’s start off by showing the set of data we will be working with:

mysql> SELECT n, title
    -> FROM say2
    -> WHERE n IN ( 1, 3, 5 )
    -> ;
+------+-------+
| n    | title |
+------+-------+
|    1 | Mom   |
|    3 | SUN   |
|    5 | Asia  |
+------+-------+
3 rows in set (0.00 sec)
mysql> SELECT n, title
    -> FROM say2
    -> WHERE n IN ( 5, 7, 8 )
    -> ;
+------+-------+
| n    | title |
+------+-------+
|    5 | Asia  |
|    7 | Venus |
|    8 | Korea |
+------+-------+
3 rows in set (0.00 sec)

mysql> SELECT n, title
    -> FROM say2
    -> WHERE n IN ( 1, 3, 5 )
    -> UNION
    -> SELECT n, title
    -> FROM say2
    -> WHERE n IN ( 5, 7, 8 )
    -> ;
+------+-------+
| n    | title |
+------+-------+
|    1 | Mom   |
|    3 | SUN   |
|    5 | Asia  |
|    7 | Venus |
|    8 | Korea |
+------+-------+
5 rows in set (0.00 sec)

The UNION operator shows the result set of both A and B in the diagram above. Now to get the difference we need to show the result set from A LEFT JOINed to the result set from B. Those that have matching entries will show data for the B fields. Fields that do not have a valid join will be NULL. We need to test on the NULL.

mysql> SELECT a.n,
    ->        a.title
    -> FROM   say2 a
    ->        LEFT JOIN say2 b
    ->               ON ( a.n = b.n
    ->                    AND a.title = b.title
    ->                    AND b.n IN ( 5, 7, 8 ) )
    -> WHERE  a.n IN ( 1, 3, 5 )
    ->        AND b.n IS NULL;
+------+-------+
| n    | title |
+------+-------+
|    1 | Mom   |
|    3 | SUN   |
+------+-------+
2 rows in set (0.00 sec)