Open In App

SQL | Except Clause

Last Updated : 06 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The SQL EXCEPT operator is used to return the rows from the first SELECT statement that are not present in the second SELECT statement. This operator is conceptually similar to the subtract operator in relational algebra. It is particularly useful for excluding specific data from your result set.

In this article, we will learn how the SQL EXCEPT operator works, and its syntax, and compare it with similar operators like NOT IN. We will also look at practical examples to help you better understand its usage.

What is SQL EXCEPT?

The SQL EXCEPT operator allows you to return the rows that exist in the first result set but not in the second. It is useful for finding records in one table that do not have corresponding records in another table.

Syntax:

SELECT column_name(s)

FROM table1

EXCEPT

SELECT column_name(s)

FROM table2;

Note: The two SELECT queries must return the same number of columns and the data types must be compatible.

Examples of SQL EXCEPT

Let’s consider two tables, Students and Teaching Assistant. We will perform all the examples based on these two tables.

Students Table

-- Create Students Table
CREATE TABLE Students (
    StudentID INT PRIMARY KEY,
    Name VARCHAR(100),
    Course VARCHAR(100)
);

-- Insert Data into Students Table
INSERT INTO Students (StudentID, Name, Course) VALUES
(1, 'Rohan', 'DBMS'),
(2, 'Kevin', 'OS'),
(3, 'Mansi', 'DBMS'),
(4, 'Mansi', 'ADA'),
(5, 'Rekha', 'ADA'),
(6, 'Megha', 'OS');

Output:

StudentIDNameCourse
1RohanDBMS
2KevinOS
3MansiDBMS
4MansiADA
5RekhaADA
6MeghaOS

Teaching Assistant Table

-- Create Teaching Assistant Table
CREATE TABLE TA (
    StudentID INT PRIMARY KEY,
    Name VARCHAR(100),
    Course VARCHAR(100)
);

-- Insert Data into TA Table
INSERT INTO TA (StudentID, Name, Course) VALUES
(1, 'Kevin', 'TOC'),
(2, 'Sita', 'IP'),
(3, 'Manik', 'AP'),
(4, 'Rekha', 'SNS');

Output:

StudentIDNameCourse
1KevinTOC
2SitaIP
3ManikAP
4RekhaSNS

Example 1: Filter Student

We want to find all the students who are not teaching assistants.

Query:

SELECT Name
FROM Students
EXCEPT
SELECT Name
FROM TA;

Output:

Name
Mansi
Megha
Rohan

Explanation: The EXCEPT operator returns the names that are present in the Students table but not in the TA table. Notice that "Rohan", "Mansi", and "Megha" are returned, as these students are not listed in the TA table.

Example 2: Retaining Duplicates with EXCEPTALL

By default, EXCEPT removes duplicates from the result set. To retain duplicates, you can use EXCEPT ALL instead.

Query:

SELECT NameFROM StudentsEXCEPTSELECT NameFROM TA;

Output:

Name
Rohan
Mansi
Mansi
Megha

Explanation: In this case, "Mansi" appears twice in the output because it appears twice in the Students table and is not in the TA table. EXCEPT ALL retains duplicates from the first result set.

SQL EXCEPT vs. SQL NOT IN

While both EXCEPT and NOT IN are used to exclude certain records, there are important differences between them.

FeatureEXCEPTNOT IN
Duplicate HandlingRemoves duplicates from the resultRetains duplicates in the result
PerformanceGenerally more efficient for large datasets as it processes only the required rowsMay be slower for large datasets, especially when checking multiple conditions
Use CaseWhen you need to find rows that exist in one result set but not the otherWhen you need to check a specific column’s values against a list
SQL SupportNot supported by MySQLSupported by most SQL databases

Key Points to Remember

  • EXCEPT returns rows from the first result set that are not in the second result set.
  • EXCEPT automatically removes duplicates, while EXCEPT ALL retains duplicates.
  • EXCEPT requires both queries to have the same number of columns and compatible data types.
  • EXCEPT is not supported in MySQL, while NOT IN can be used as an alternative.
  • Use EXCEPT when you need to exclude certain rows efficiently from the first result set, especially in larger datasets.

Conclusion

The SQL EXCEPT operator is a powerful tool for excluding rows from one query that exist in another. It helps eliminate unwanted data, making it valuable for various analytical tasks. Understanding how EXCEPT works, as well as its differences from other operators like NOT IN, allows you to make better decisions when structuring your SQL queries for optimal performance.


Next Article
Article Tags :
Practice Tags :

Similar Reads