EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

SQL Except Select

By Priya PedamkarPriya Pedamkar

Home » Data Science » Data Science Tutorials » SQL Tutorial » SQL Except Select

SQL Except Select

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.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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 –

SQL Except Select-1.1

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-

Popular Course in this category
Sale
MS SQL Training (14 Courses, 11+ Projects)14 Online Courses | 11 Hands-on Projects | 62+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (9,032 ratings)
Course Price

View Course

Related Courses
JDBC Training (6 Courses, 7+ Projects)PHP Training (5 Courses, 3 Project)Windows 10 Training (4 Courses, 4+ Projects)SQL Training Program (7 Courses, 8+ Projects)PL SQL Training (4 Courses, 2+ Projects)Oracle Training (14 Courses, 8+ Projects)

SQL Except Select-1.2

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 –

SQL Except Select-1.3

SELECT * FROM UpdatedArticles;

The execution of the above query statement gives an output which is as shown below –

SQL Except Select-1.4

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 –

SQL Except Select-1.5

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 –

  1. For Loop in Unix
  2. Crontab in Unix
  3. Array in Unix
  4. VI Editor in Unix

MS SQL Training (13 Courses, 11+ Projects)

14 Online Courses

11 Hands-on Projects

62+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
SQL Tutorial
  • Queries
    • SQL Insert Query
    • SQL SELECT Query
    • SQL SELECT RANDOM
    • SQL Except Select
    • SQL Subquery
    • SQL SELECT DISTINCT
    • SQL WITH AS Statement
  • Basic
    • What is SQL
    • Careers in SQL
    • Careers in SQL Server
    • IS SQL Microsoft?
    • SQL Management Tools
    • What is SQL Developer
    • Uses of SQL
    • How to Install SQL Server
    • What is SQL Server
    • SQL Server Versions
    • SQL Case Insensitive
    • SQL Expressions
    • Database in SQL
    • SQL Data Types
    • SQL Keywords
    • Composite Key in SQL
    • SQL WAITFOR
    • SQL Constraints
    • Transactions in SQL
    • First Normal Form
    • SQL Server Data Types
    • SQL Administration
    • SQL Variables
    • SQL Enum
    • SQL GROUP BY WHERE
    • SQL ROW
    • SQL EXECUTE
    • SQL EXCLUDE
    • SQL Performance Tuning
    • SQL UUID
    • Begin SQL
    • SQL Update Join
    • Cheat sheet SQL
  • Operators
    • SQL Operators
    • SQL Arithmetic Operators
    • SQL Logical Operators
    • SQL String Operators
    • Ternary Operator in SQL
  • Commands
    • SQL Commands
    • sqlplus set commands
    • SQL Alter Command
    • SQL Commands Update
    • SQL DML Commands
    • SQL DDL Commands
    • FETCH in SQL
  • Clause
    • SQL Clauses
    • SQL IN Operator
    • SQL LIKE Clause
    • SQL NOT Operator
    • SQL Minus
    • SQL WHERE Clause
    • SQL with Clause
    • SQL HAVING Clause
    • GROUP BY clause in SQL
    • SQL GROUP BY DAY
    • ORDER BY Clause in SQL
    • SQL ORDER BY CASE
    • SQL ORDER BY DESC
    • SQL ORDER BY DATE
    • SQL ORDER BY Alphabetical
    • SQL ORDER BY Ascending
    • SQL Order by Count
    • SQL GROUP BY Month
    • SQL GROUP BY Multiple Columns
    • SQL GROUPING SETS
  • Keys
    • SQL Keys
    • Primary Key in SQL
    • Foreign Key in SQL
    • Unique Key in SQL
    • Alternate Key in SQL
    • SQL Super Key
  • Functions
    • SQL Date Function
    • SQL String Functions
    • SQL Compare String
    • Timestamp to Date in SQL
    • SQL Window Functions
    • SQL Timestamp
    • SQL TO_DATE()
    • SQL DATEADD()
    • SQL DATEDIFF()
    • SQL HOUR()
    • SQLite?functions
    • ANY in SQL
    • LIKE Query in SQL
    • SQL NOT NULL
    • SQL NOT IN
    • SQL MAX()
    • SQL MIN()
    • SQL SUM()
    • SQL COUNT
    • SQL identity
    • SQL DELETE Trigger
    • SQL Declare Variable
    • SQL Text Search
    • SQL COUNT DISTINCT
    • SQL TEXT
    • SQL Limit Order By
    • BETWEEN in SQL
    • LTRIM() in SQL
    • TOP in SQL
    • SQL Select Top
    • Merge SQL
    • SQL TRUNCATE()
    • SQL UNION
    • SQL ALL
    • SQL INTERSECT
    • SQL Alias
    • SQL Server Substring
    • CUBE in SQL
    • SQL RANK()
    • SQL MOD()
    • SQL CTE
    • SQL LAG()
    • SQL MID
    • SQL avg()
    • SQL WEEK
    • SQL DELETE
    • SQL DATEPART()
    • SQL DECODE()
    • SQL DENSE_RANK()
    • SQL NTILE()
    • SQL NULLIF()
    • SQL Stuff
    • SQL Ceiling
    • SQL EXISTS
    • SQL LEAD()
    • SQL COALESCE
    • SQL BLOB
    • SQL ROW_NUMBER
    • SQL Server Replace
    • SQL Server Permission
    • T-SQL INSERT
    • SQL Ranking Function
  • Joins
    • Join Query in SQL
    • Types of Joins in SQL
    • Types of Joins in SQL Server
    • SQL Inner Join
    • SQL Join Two Tables
    • SQL Delete Join
    • SQL Left Join
    • LEFT OUTER JOIN in SQL
    • SQL Right Join
    • SQL Cross Join
    • SQL Outer Join
    • SQL Full Join
    • SQL Self Join
    • Natural Join SQL
    • SQL Multiple Join
  • Advanced
    • SQL Formatter
    • SQL Injection Attack
    • Aggregate Functions in SQL
    • IF ELSE Statement in SQL
    • SQL CASE Statement
    • SQL While Loop
    • SQL BIGINT
    • SQL Crosstab
    • SQL Wildcard Character
    • SQLAlchemy Filter
    • SQLAlchemy create_engine
    • SQL INSTR()
    • SQL now
    • SQL synonyms
    • SQLite?export to csv
    • What is Procedure in SQL
    • Stored Procedure in SQL?
    • SQL Server Constraints
    • SQL DELETE ROW
    • Column in SQL
    • Table in SQL
    • SQL Virtual Table
    • SQL Merge Two Tables
    • SQL Table Partitioning
    • SQL Temporary Table
    • SQL Clone Table
    • SQL Rename Table
    • SQL LOCK TABLE
    • SQL Clear Table
    • SQL DESCRIBE TABLE
    • SQL Mapping
    • Cursors in SQL
    • AND in SQL
    • Wildcard in SQL
    • SQL FETCH NEXT
    • SQL Views
    • SQL Delete View
    • Triggers in SQL
    • SQL UPDATE Trigger
    • SQL AFTER UPDATE Trigger
    • SQL Update Statement
    • SQL DROP TRIGGER
    • Types of SQL Views
    • SQL Port
    • SQL Clustered Index
    • SQL COMMIT
    • Distinct Keyword in SQL
    • PARTITION BY in SQL
    • SQL Set Operators
    • SQL UNION ALL
    • Metadata in SQL
    • SQL Bulk Insert
    • Array in SQL
    • SQL REGEXP
    • JSON in SQL
    • SQL For loop
    • EXPLAIN in SQL
    • ROLLUP in SQL
    • Escape Character SQL
    • SQL Cluster
    • SQL Backup
    • SQL Pattern Matching
    • SQL Users
    • ISNULL SQL Server
    • SQL pivot
    • SQL Import CSV
    • SQL if then else
    • SQL ignore-case
    • SQL Matches
    • SQL Search String
    • SQL Column Alias
    • SQL extensions
    • SQL Substring Function
    • Charindex SQL
  • NoSQ
    • NoSQL Databases List
    • NoSQL Injection
    • NoSQL vs SQL Databases
  • Interview Questions
    • SQL Interview Questions
    • Advance SQL Interview Questions
    • SQL Joins Interview Questions
    • SQL Server Interview Questions

Related Courses

JDBC Training Course

PHP course

Windows 10 Training

SQL Course Training

PL/SQL Certification Courses

Oracle Certification Courses

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Special Offer - MS SQL Training (13 Courses, 11+ Projects) Learn More