Introduction to SQL Except Select
SQL except select is useful while retrieving data from the query and you have to mention that certain data should not be added in the final resultset. Manier times while fetching the data from the database, updating the records in the database or deleting the multiple records from the database we have to mention that certain records from the retrieved resultset of the execution of the query should be excluded from the final resultset on which either of the operations will get performed i.e select, update or delete. Using the except clause after your select statement you can either mention the hardcoded values that are to be excluded from the final resultset or you can even provide another query statement after the except keyword whose resultset will be excluded from the final resultset.
In this article, we will study the general syntax of the except clause inside the select clause and also learn about its implementation using multiple examples. Further, we will learn about the difference in the working of the not in the clause and except. Microsoft SQL Server 2005 introduced except clause and its functionality. Note that except clause is not supported by some of the DBMS supporting SQL query language such as MySQL DBMS.
Syntax of SQL Except Select
The syntax of except select clause in the select operation of the SQL query statement is as shown below –
SELECT column name 1 [, column name 2 ] FROM table name 1 [, table name 2 ] [WHERE condition or restriction]
EXCEPT
SELECT column name 1 [, column name 2 ] FROM table name 1 [, table name 2 ] [WHERE condition or restriction]
In the above syntax, the first select statement that is on the right side is the main query that retrieves the resultset from the table named table name 1 and the retrieved values contain values of columns with names column name 1, column name 2 and so on. Now what the EXCEPT keyword does is that the rows that are retrieved from the second select query statement on the left side should be removed from the result set obtained after execution of the right side query if present. Note that both the queries on the left and right sides should retrieve the same number of and comparative data typed value from each other similar to union clause usage.
The execution of except keyword goes like this the intersection of the resultsets of both the queries on the right side and left side of it are obtained and then the final result will consist of all the records of the right side query and then removing the intersected record resultset from it to obtain the final resultset.
Examples of SQL Except Select
Let us firstly consider a simple example where we have one existing table named developers which has the structure and contents of its table that are as shown in the output of the below query statement –
Now, we have to exclude the records whose salary is less than 20000 from the result set of the query which retrieves name, position, and technology from developers table and with the use of EXCEPT keyword we can make the use of the following query statement –
SELECT
NAME,
POSITION,
technology
FROM
`developers`
EXCEPT
SELECT
NAME,
POSITION,
technology
FROM
`developers`
WHERE salary < 20000;
The execution of the above query statement gives an output which is as shown below-

4.5 (9,032 ratings)
View Course
We can observe that all the developers having a salary greater than or equal to 2000 are included in the final result set and the other with less than 20000 are excluded because those many records were retrieved from the second select statement after except. Now, let us consider one more example where we have two tables two existing tables named Articles and UpdatedArticles whose contents and structure are as shown in the output of the following query statement –
SELECT * FROM Articles;
The execution of the above query statement gives an output which is as shown below –
SELECT * FROM UpdatedArticles;
The execution of the above query statement gives an output which is as shown below –
Now, we have to retrieve the records from the updated articles table such that those records should not be present in the table named articles. Note that both the tables contain the same columns and we have to retrieve the topic of the article and its rate. For this, we can make the use of EXCEPT clause and build our query statement as follows –
SELECT
`ArticleTopic`,
`Rate`
FROM
`UpdatedArticles`
EXCEPT
SELECT
`ArticleTopic`,
`Rate`
FROM
`Articles` ;
The execution of the above query statement gives an output which is as shown below –
We can observe that only the single record is retrieved a this is the only one that was present in updated articles but not in articles table records.
Difference between EXCEPT and NOT IN Clauses
We can see that the functionality of both the clauses is the same that is they help in specifying certain resultsets that should not be included in the final resultset of the query. But there lie many differences in their working.
Whenever we use the EXCEPT clause it is necessary to mention all the column values that the query will retrieve in the query that will mention the records to be excluded and hence the constraint this records should be excepted is applicable for all the column values while in case of NOT IN clause only single column value is being restricted for its valuable contents which specify that the resultset having that column this set of values should not be included in the final resultset.
Besides this, the use of EXCEPT clause removes all the duplicate records that will be retrieved from the resultset while the NOT IN clause will retain duplicate entries in the final resultset.
Recommended Articles
This is a guide to SQL Except Select. Here we also discuss the introduction and difference between except and not in clauses along with different examples and its code implementation. You may also have a look at the following articles to learn more –