EXCEPT and MINUS

Important

Both the EXCEPT and MINUS commands perform the exact same operation.

EXCEPT and MINUS are synonymous operators which can be used to compare the results of two queries and return the distinct rows from the first query that are not output by the second.

When comparing two result sets using EXCEPT or MINUS, the data types must be compatible and the number and order of the columns should match across queries.

Syntax

SELECT ...
{ EXCEPT | MINUS }
SELECT ...

Arguments

Accepts two or more queries that return data of comparable data types.

Remarks

In addition to being able to be used in queries, EXCEPT and MINUS can also be used in subqueries and common table expressions (CTEs).

The column names of the returned result set are the same as the column names returned by the query on the left side of the operator.

If using ORDER BY, column names must match those returned by the left-side query.

Examples

The following examples demonstrate how to compare two result sets using EXCEPT or MINUS.

Example 1

Consider the following scenario. You have two tables orders and delivered, where the orders table lists all the orders and the delivered table lists orders that have been delivered. You need a list of all the orders that haven’t been delivered yet.

CREATE TABLE ex_orders (id int, product_id varchar(10), stock_qty int, order_date datetime);
INSERT INTO ex_orders VALUES
(2,'sdfy',4,'2021-02-10'),
(4,'fjty',8,'2021-03-08'),
(1,'xrdf',5,'2021-01-12'),
(3,'qtvb',2,'2021-02-15');
SELECT * FROM ex_orders;
+----+------------+-----------+---------------------+
| id | product_id | stock_qty | order_date          |
+----+------------+-----------+---------------------+
|  2 | sdfv       |         4 | 2021-02-10 00:00:00 |
|  4 | fjty       |         8 | 2021-03-08 00:00:00 |
|  1 | xrdf       |         5 | 2021-01-12 00:00:00 |
|  3 | qtvb       |         2 | 2021-02-15 00:00:00 |
+----+------------+-----------+---------------------+
CREATE TABLE ex_delivered (id int, product_id varchar(10), delivery_date datetime);
INSERT INTO ex_delivered VALUES
(2,'sdfy','2021-02-14'),
(4,'fjty','2021-03-14');
SELECT * FROM ex_delivered;
+----+------------+------------------------+
| id | product_id | delivery_date.         |
+----+------------+------------------------+
|  2 | sdfv       | 2021-02-14 00:00:00    |
|  4 | fjty       | 2021-03-14 00:00:00    |
+----+------------+------------------------+

The following query lists all the orders that haven’t been delivered yet. Note: To match the number and order of columns, each column is specified in the query.

SELECT id, product_id FROM ex_orders
MINUS
SELECT id, product_id FROM ex_delivered;
+----+------------+
| id | product_id |
+----+------------+
|  1 | xrdf       |
|  3 | qtvb       |
+----+------------+

The following query uses EXCEPT to deliver the same results.

SELECT id, product_id FROM ex_orders
EXCEPT
SELECT id, product_id FROM ex_delivered;

The following query emulates the EXCEPT operator using the JOIN operator.

SELECT id, product_id
FROM ex_orders
WHERE id NOT IN
(SELECT ex_orders.id
FROM ex_orders INNER JOIN ex_delivered ON ex_orders.id = ex_delivered.id);

Example 2

The following example shows how to use EXCEPT in a subquery, using the tables orders and delivered from the previous example. You can also use MINUS to achieve similar results.

SELECT *
FROM (SELECT id, product_id FROM ex_orders
EXCEPT
SELECT id, product_id FROM ex_delivered) ndel
WHERE ndel.id > 2;
+----+------------+
| id | product_id |
+----+------------+
|  3 | qtvb       |
+----+------------+

Last modified: March 1, 2023

Was this article helpful?

Verification instructions

Note: You must install cosign to verify the authenticity of the SingleStore file.

Use the following steps to verify the authenticity of singlestoredb-server, singlestoredb-toolbox, singlestoredb-studio, and singlestore-client SingleStore files that have been downloaded.

You may perform the following steps on any computer that can run cosign, such as the main deployment host of the cluster.

  1. (Optional) Run the following command to view the associated signature files.

    curl undefined
  2. Download the signature file from the SingleStore release server.

    • Option 1: Click the Download Signature button next to the SingleStore file.

    • Option 2: Copy and paste the following URL into the address bar of your browser and save the signature file.

    • Option 3: Run the following command to download the signature file.

      curl -O undefined
  3. After the signature file has been downloaded, run the following command to verify the authenticity of the SingleStore file.

    echo -n undefined |
    cosign verify-blob --certificate-oidc-issuer https://oidc.eks.us-east-1.amazonaws.com/id/CCDCDBA1379A5596AB5B2E46DCA385BC \
    --certificate-identity https://kubernetes.io/namespaces/freya-production/serviceaccounts/job-worker \
    --bundle undefined \
    --new-bundle-format -
    Verified OK