In SQL, EXCEPT returns those tuples that are returned by the first SELECT operation, and not returned by the second SELECT operation.
This is the same as using a subtract operator in relational algebra.
Say we have two relations, Students and TA (Teaching Assistant). We want to return all those students who are not teaching assistants. The query can be formulated as:
SELECT Name FROM Students EXCEPT SELECT NAME FROM TA;
Rohan Mansi Megha
To retain duplicates, we must explicitly write EXCEPTALL instead of EXCEPT.
SELECT Name FROM Students EXCEPTALL SELECT Name FROM TA;
Rohan Mansi Mansi Megha
Difference between EXCEPT and NOT IN Clause
EXCEPT automatically removes all duplicates in the final result, whereas NOT IN retains duplicate tuples. It is also important to note that EXCEPT is not supported by MySQL.
This article is contributed by Anannya Uberoi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
- SQL | ON Clause
- SQL | USING Clause
- SQL | WHERE Clause
- SQL | WITH clause
- Difference between From and Where Clause in SQL
- Difference between Where and Having Clause in SQL
- PHP | MySQL WHERE Clause
- SQL | Intersect & Except clause
- SQL | Distinct Clause
- SQL | SELECT TOP Clause
- SQL | Union Clause
- SQL | LIMIT Clause
- SQL | With Ties Clause
- SQL | Sub queries in From Clause
- SQL query using COUNT and HAVING clause
Improved By : lonewolf_ab