Basic about EXCEPT Operator – using and example

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


The SQL EXCEPT operator is used to return all rows in the first SELECT declaration that are no longer returned with the aid of the 2nd SELECT statement. Each SELECT statement will outline a dataset. The EXCEPT operator will retrieve all information from the first dataset and then get rid of from the effects all information from the 2nd dataset.

Except Query

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

Each SELECT assertion within the EXCEPT question should have the equal variety of fields in the end result sets with comparable data types.


The syntax for the EXCEPT operator in SQL is:

SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions]
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 want to retrieve information from. There should be at least one table listed in the FROM clause. WHERE conditions Optional. These are prerequisites that ought to be met for the documents to be selected.


There have to be equal variety of expressions in each SELECT statements. The corresponding expressions have to have the same facts type in the SELECT statements. For example: expression1 need to be the identical statistics type in both the first and 2d SELECT statement.

Example – With Single Expression

Let’s look at an instance of how to use the EXCEPT operator in SQL that returns one field with the same information type.

For example:

SELECT product_id
FROM products
SELECT product_id
FROM inventory;

This EXCEPT operator instance returns all product_id values that are in the merchandise desk and not in the inventory table. What this capability is that if a product_id cost existed in the merchandise desk and additionally existed in the inventory table, the product_id value would no longer appear in the EXCEPT question results.

Example – With Multiple Expressions

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

For example:

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

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

Example – Using ORDER BY

Finally, let’s appear 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
SELECT company_id, company_name
FROM companies
WHERE state = 'Florida'

In this EXCEPT example, considering the column names are specific between the two SELECT statements, it is extra superb to reference the columns in the ORDER BY clause by way of their position in the result set. In this example, we’ve got sorted the outcomes by supplier_name / company_name in ascending order, as denoted by using the ORDER BY two

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