Sqlite Except Operator

SQLite: EXCEPT Operator

This SQLite post explains how to use the EXCEPT operator in SQLite with syntax and examples.

Description

The SQLite 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

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.

Syntax

The syntax for the EXCEPT operator in SQLite is:

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

Parameters or Arguments

expressions

The columns or calculations that you wish to compare between the two SELECT statements. They do not have to be the same fields in each of the SELECT statements, but the corresponding columns must be similar data types.

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. The 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 columns in each of the SELECT statements must have similar data types.
  • The EXCEPT operator returns all records from the first SELECT statement that are not in the second SELECT statement.
  • The EXCEPT operator in SQLite is equivalent to the MINUS operator in Oracle.

Example - With Single Expression

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

For example:

SELECT department_id
FROM departments
EXCEPT
SELECT department_id
FROM employees;

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

Example - With Multiple Expressions

Next, let's look at an example of an EXCEPT query in SQLite that returns more than one column.

For example:

SELECT contact_id, last_name, first_name
FROM contacts
WHERE contact_id >= 74
EXCEPT
SELECT employee_id, last_name, first_name
FROM employees
WHERE first_name = 'Sarah';

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 employee_id, last_name, and first_name value in the employees table.

Example - Using ORDER BY

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

For example:

SELECT supplier_id, state
FROM suppliers
WHERE state = 'Florida'
EXCEPT
SELECT company_id, state
FROM companies
WHERE company_id  2000
ORDER BY 1 DESC;

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_id / company_id in descending order, as denoted by the ORDER BY 1 DESC.

The supplier_id / company_id fields are in position #1 in the result set.