totn SQL

SQL: EXCEPT Operator

This SQL tutorial explains how to use the SQL EXCEPT operator with syntax and examples.

Description

The SQL EXCEPT operator is used to return all rows in the first SELECT statement that are not returned by the second SELECT statement. Each SELECT statement will define a dataset. The EXCEPT operator will retrieve all records from the first dataset and then remove from the results all records from the second dataset.

Except Query

SQL

Explanation: The EXCEPT query will return the records in the blue shaded area. These are the records that exist in Dataset1 and not in Dataset2.

Each SELECT statement within the EXCEPT query must have the same number of fields in the result sets with similar data types.

TIP: The EXCEPT operator is not supported in all SQL databases. It can be used in databases such as SQL Server, PostgreSQL, and SQLite.

For databases such as Oracle, use the MINUS operator to perform this type of query.

Syntax

The syntax for the EXCEPT operator in SQL is:

SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions]
EXCEPT
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions];

Parameters or Arguments

expression1, expression2, expression_n
The columns or calculations that you wish to retrieve.
tables
The tables that you wish to retrieve records from. There must be at least one table listed in the FROM clause.
WHERE conditions
Optional. These are conditions that must be met for the records to be selected.

Note

  • There must be same number of expressions in both SELECT statements.
  • The corresponding expressions must have the same data type in the SELECT statements. For example: expression1 must be the same data type in both the first and second SELECT statement.

Example - With Single Expression

Let's look at an example of how to use the EXCEPT operator in SQL that returns one field with the same data type.

For example:

SELECT product_id
FROM products
EXCEPT
SELECT product_id
FROM inventory;

This EXCEPT operator example returns all product_id values that are in the products table and not in the inventory table. What this means is that if a product_id value existed in the products table and also existed in the inventory table, the product_id value would not appear in the EXCEPT query results.

Example - With Multiple Expressions

Next, let's look at an example of how to use the EXCEPT query in SQL that returns more than one column.

For example:

SELECT contact_id, last_name, first_name
FROM contacts
WHERE last_name = 'Johnson'
EXCEPT
SELECT customer_id, last_name, first_name
FROM customers
WHERE customer_id > 45;

In this EXCEPT example, the query will return the records in the contacts table with a contact_id, last_name, and first_name value that does not match the customer_id, last_name, and first_name value in the customers table.

Example - Using ORDER BY

Finally, let's look at how to use the ORDER BY clause in an EXCEPT query in SQL.

For example:

SELECT supplier_id, supplier_name
FROM suppliers
WHERE supplier_id < 30
EXCEPT
SELECT company_id, company_name
FROM companies
WHERE state = 'Florida'
ORDER BY 2;

In this EXCEPT example, since the column names are different between the two SELECT statements, it is more advantageous to reference the columns in the ORDER BY clause by their position in the result set. In this example, we've sorted the results by supplier_name / company_name in ascending order, as denoted by the ORDER BY 2.

The supplier_name / company_name fields are in position #2 in the result set.