Learning Objective
The objective of this SQL Server tutorial is to teach you how to use the EXCEPT operator to extract unique records from a combination.
What is EXCEPT in SQL Server?
The EXCEPT operator is used to extract unique records from the first query resultset discarding those records which are common to the second query resultset. It is a set operation which subtracts the second query resultset.
Prerequisites: The prerequisite for a successful EXCEPT operation is that the query resultset should have the same number of columns in the same order and with same or similar datatype (i.e. the column datatypes should be either exactly same or it should be possible to convert one datatype into another without casting error).
The EXCEPT operation can be pictorially represented as below.
Operation
As discussed above the EXCEPT operator can only be used with SELECT queries and the query result sets should be of same size. It can be used with two or more queries. When used with more than two queries it first operates on the first two queries and the resultset from that becomes the input for comparison with the third query resultset and so on.
SQL Server EXCEPT Syntax
The basic syntax of the EXCEPT operator is given below.
SELECT_QUERY_1 EXCEPT SELECT_QUERY_2;
In this syntax,
- SELECT_QUERY – SQL query which selects rows of information (i.e. records or tuples) from a table. It can be a simple or complex query with conditions.
- EXCEPT – SQL keyword to combine query result sets of two or more queries and extract the unique records from the first or left or preceding query.
SQL Server EXCEPT Examples
Let us consider an example to understand its practical usage and implications. Suppose we have two tables – a table called persons and another table called customers. The persons table contains prospect information and the customers table contains customer information. Data wise the customers table is a subset of the persons table with the persons table containing 10 records and the customers table containing 8 records. The sample tables are shown below.
Please note that the column names are different in the tables and that the persons table is one column short. But however, it does not violate the prerequisites since they relate to the resultset and not to the table. The tables can have dissimilar number of columns and column names.
first_name | last_name | pers_city |
Herbert | Einstein | New York |
Jackie | Frost | Seattle |
Stephen | George | Washington |
James | Mare | New York |
Angel | Reeves | Dallas |
Selena | Spears | Detroit |
Sammy | Louise | Dallas |
Brandon | Powell | Seattle |
Kim | Fox | Washington |
Val | Costner | New York |
fname | lname | city | contact |
Stephen | George | Washington | 16553120210 |
James | Mare | New York | 16277212992 |
Angela | Crawford | Washington | 16633775159 |
Selena | Spears | Detroit | 16616165325 |
Patty | Campbell | Detroit | 16107575525 |
Brandon | Powell | Seattle | 16101110778 |
Kim | Fox | Washington | 16529929936 |
Edith | Poe | Seattle | 16767335231 |
1) SQL Server EXCEPT example
We will do an EXCEPT operation between the SELECT queries retuning the first and last names from the two tables.
SELECT first_name, last_name FROM persons EXCEPT SELECT fname, lname FROM customers;
It will run successfully and generate the following resultset with the column names from the first table persons as the column heading. These are the unique distinct names in the persons table which do not exist in the customers table.
first_name | last_name |
Angel | Reeves |
Herbert | Einstein |
Jackie | Frost |
Sammy | Louise |
Val | Costner |
Now let us try to run the following EXCEPT query with the SELECT queries returning all columns from the tables. As we can see below the query fails with error because of * trying to fetch all columns from the tables and the number of columns being different in the two tables – 3 in the persons table and 4 in customers.
SELECT * FROM persons EXCEPT SELECT * FROM customers; Msg 205, Level 16, State 1, Server MY-HP245G6, Line 1 All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.