SQL EXCEPT

The SQL EXCEPT is one of the Set operator provided by the SQL Server. This SQL Server except is used to return distinct rows from the left-hand side query that aren’t outputted by the right-hand side query.

Or, we can say, SQL except will return all the records from a left table, that is not present in the right side. The below image will help you to understand the SQL Server Except.

SQL EXCEPT 8

SQL EXCEPT Syntax

The syntax behind the SQL Server Except is as shown below:

SELECT Column_Name1, Column_Name2 ......., Column_NameN FROM Table1
EXCEPT
SELECT Column_Name1, Column_Name2 ......., Column_NameN FROM Table2

The following are the set of rules for except operator in SQL Server:

  1. The total columns must be the same in both the queries.
  2. The column data types must be compatible with each other.
  3. Order of the Columns must be the same in all the queries.
SQL EXCEPT 1

For this Sql Server Except operator demo, We are going to use two tables (Employ, and Employees 2016) present in our [SQL Tutorial] Database. From the below screenshot, you can observe that the [Employ] table has ten records

SQL EXCEPT 2

And [Employees 2016] table has four records. In this SQL Server table, there is only one distinct record (9, Rob, Verhoff) and the remaining records are the same.

SQL UNION Example 2

SQL Except example

The following except Query will return total records from Employ that are not present in Employees2016 table and display the result

-- Example for Except in SQL Server
SELECT [ID]
      ,[FirstName]
      ,[LastName]
      ,[Occupation]
      ,[YearlyIncome]
      ,[Sales]
  FROM [SQL Tutorial].[dbo].[Employ]
 EXCEPT
  SELECT [ID]
      ,[FirstName]
      ,[LastName]
      ,[Occupation]
      ,[YearlyIncome]
      ,[Sales]
  FROM [SQL Tutorial].[dbo].[Employees 2016]
SQL EXCEPT 3

Let us alter the table position. It means except query returns total the records from Employees2016 table that is not present in Employ table

-- Example for Except in SQL Server 
SELECT [ID]
      ,[FirstName]
      ,[LastName]
      ,[Occupation]
      ,[YearlyIncome]
      ,[Sales]
  FROM [SQL Tutorial].[dbo].[Employees 2016]
  EXCEPT
  SELECT [ID]
      ,[FirstName]
      ,[LastName]
      ,[Occupation]
      ,[YearlyIncome]
      ,[Sales]
  FROM [SQL Tutorial].[dbo].[Employ]
SQL EXCEPT 4

SQL Except Where Clause Example

How to use the except with the WHERE Clause. In this example, we are joining two statements:

  • The first result set selects all the records from Employ, whose yearly income is greater than or equal to 70000
  • The second result set picks the records from Employees 2016.
  • And the except operator selects existing records from the first result set that are not present in the second result set.
-- Example for Except in SQL Server
SELECT [ID]
      ,[FirstName]
      ,[LastName]
      ,[Occupation]
      ,[YearlyIncome]
      ,[Sales]
FROM [SQL Tutorial].[dbo].[Employ]
WHERE [YearlyIncome] >= 70000
 EXCEPT
SELECT [ID]
      ,[FirstName]
      ,[LastName]
      ,[Occupation]
      ,[YearlyIncome]
      ,[Sales]
FROM [SQL Tutorial].[dbo].[Employees 2016]
SQL EXCEPT 5

SQL Except Errors pf

The following Query will display the list of common errors that we encounter while writing the except query. For this, We are going to use two tables (Employee, and Employ) present in the [SQL Tutorial] Database. Our [Employee] table has 7 columns and 14 rows.

SQL EXCEPT 9

Let us see what will happen when we perform Sql Server except operation on an unequal length of columns.

-- Sql Server Except Example
SELECT [FirstName]
      ,[LastName]
      ,[Education]
      ,[Occupation]
      ,[YearlyIncome]
      ,[Sales]
      ,[HireDate]
  FROM [SQL Tutorial].[dbo].[Employee]
  EXCEPT
SELECT [ID]
      ,[FirstName]
      ,[LastName]
      ,[Occupation]
      ,[YearlyIncome]
      ,[Sales]
FROM [SQL Tutorial].[dbo].[Employ]
Messages
--------
Msg 205, Level 16, State 1, Line 2
All queries combined using a UNION, INTERSECT or EXCEPT operator 
must have an equal number of expressions in their target lists.

Now, let us modify the query to select an equal number of columns

-- Example for Except SQL  
SELECT [FirstName]
      ,[LastName]
      ,[Occupation]
      ,[YearlyIncome]
      ,[Sales]
  FROM [SQL Tutorial].[dbo].[Employee]
  EXCEPT
SELECT [FirstName]
      ,[LastName]
      ,[Occupation]
      ,[YearlyIncome]
      ,[Sales]
FROM [SQL Tutorial].[dbo].[Employ]
SQL EXCEPT 7

From the above image, see that it is returning seven rows because, except 2, 7, 9, 11 to 14 records, the remaining ten records in Employee are also there in Employ table. And our Except operator selects only distinct records from Employee.