EXCEPT
Return rows from the first result set that do not appear in the second. This set operator performs a subtraction of two datasets.
EXCEPT
The result of EXCEPT contains all records of the left SELECT result set except records which are in right SELECT result set. In other words, it is the subtraction of two result sets.
MINUS is a synonym when SQL_MODE=ORACLE is set.
MINUS is a synonym is not available.
Syntax
SELECT ...
(INTERSECT [ALL | DISTINCT] | EXCEPT [ALL | DISTINCT] | UNION [ALL | DISTINCT])
SELECT ...
[(INTERSECT [ALL | DISTINCT] | EXCEPT [ALL | DISTINCT] | UNION [ALL | DISTINCT])
SELECT ...]
[ORDER BY [{col_name | expr | position} [ASC | DESC]
[, {col_name | expr | position} [ASC | DESC] ...]]]
[LIMIT {[offset,] row_count | row_count OFFSET offset}
| OFFSET start { ROW | ROWS }
| FETCH { FIRST | NEXT } [ count ] { ROW | ROWS } { ONLY | WITH TIES } ]Brackets for explicit operation precedence are not supported; use a subquery in the FROM clause as a workaround.
Description
MariaDB supports EXCEPT and INTERSECT in addition to UNION.
The queries before and after EXCEPT must be SELECT or VALUES statements.
All behavior for naming columns, ORDER BY and LIMIT is the same as for UNION. Note that the alternative SELECT ... OFFSET ... FETCH syntax is only supported. This allows us to use the WITH TIES clause.
EXCEPT implicitly supposes a DISTINCT operation.
The result of EXCEPT is all records of the left SELECT result except records which are in right SELECT result set, i.e. it is subtraction of two result sets.
EXCEPT and UNION have the same operation precedence and INTERSECT has a higher precedence, unless running in Oracle mode, in which case all three have the same precedence.
Parentheses
Parentheses can be used to specify precedence. Before this, a syntax error would be returned.
MariaDB starting with 10.5.0
ALL/DISTINCT
EXCEPT ALL and EXCEPT DISTINCT . The ALL operator leaves duplicates intact, while the DISTINCT operator removes duplicates. DISTINCT is the default behavior if neither operator is supplied.
Only EXCEPT DISTINCT is available.
Examples
Show customers which are not employees:
Difference between UNION, EXCEPT and INTERSECT:
Parentheses for specifying precedence:
Here is an example that makes use of the SEQUENCE storage engine and the VALUES statement, to generate a numeric sequence and remove some arbitrary numbers from it:
See Also
Get Set for Set Theory: UNION, INTERSECT and EXCEPT in SQL (video tutorial)
This page is licensed: CC BY-SA / Gnu FDL
Last updated
Was this helpful?

