JournalSQLFiltering and sets2 min
For Those Who Do Not Know INTERSECT and EXCEPT
INTERSECT retrieves the common rows between both tables. EXCEPT retrieves the rows that are not present in the second table.
- INTERSECT
- EXCEPT
- set operators
- common rows
Introduction
I previously explained how we use UNION and UNION ALL and the difference between them with examples in [Explanation] For Those Who Do Not Know UNION and UNION ALL, and I talked about the types of set operators:
We have roughly 6 types of Set operators, though I will only talk about the first two for now:
UNION UNION ALL EXCEPT EXCEPT ALL INTERSECT INTERSECT ALL
Of course, as you can see, they are basically 3 main types: Union, Except, and Intersect.
What Are INTERSECT and EXCEPT?
Regarding INTERSECT and EXCEPT, if we imagine that these two circles represent two tables as shown in the image:
Where:
INTERSECTretrieves the common rows between both tables.EXCEPTretrieves the rows that are not present in the second table.
Example
Suppose we have a countries table with two columns:
- A column for the country name:
country_name - A column for the capital city name:
capital_city
country_name |capital_city
--------------------|--------------
Mexico |Mexico
San Marino |San Marino
Egypt |Cairo
Scotland |Edinburgh
United Kingdom |London
Saudi Arabia |Riyadh
Singapore |Singapore
United Arab Emirates|Abu Dhabi
Yemen |Sanaa
Luxembourg |Luxembourg
Kuwait |Kuwait
Algeria |Algiers
Guatemala |Guatemala
Djibouti |Djibouti
Tunisia |Tunis
Morocco |RabatNotice that there are many countries in the table whose name is the same as their capital city [Reference].
Using INTERSECT
If we want to get the names of countries whose name is the same as their capital city, we write:
SELECT country_name
FROM countries
INTERSECT
SELECT capital_city
FROM countriesThe output will be:
country_name |capital_city
--------------------|--------------
Mexico |Mexico
San Marino |San Marino
Singapore |Singapore
Luxembourg |Luxembourg
Kuwait |Kuwait
Guatemala |Guatemala
Djibouti |DjiboutiNotice that the query pulled the rows that match each other in both columns, and even if they were in different tables, it would be the same. Because before INTERSECT I write a query (meaning this is a table), and after INTERSECT I write another query (meaning this is a second table).
I apologize to Algeria and Tunisia, of course鈥擨 know your capital name is your country name, but this is English, so spelling matters here 馃檪 If anyone knows how to write a query for them, please share 馃槃
Using EXCEPT
If we want to get the names of countries whose capital is not the same as their name, we write:
SELECT country_name
FROM countries
EXCEPT
SELECT capital_city
FROM countriesAnd the output will be all the countries that were not in the previous result:
country_name |capital_city
--------------------|--------------
Egypt |Cairo
Scotland |Edinburgh
United Kingdom |London
Saudi Arabia |Riyadh
United Arab Emirates|Abu Dhabi
Yemen |Sanaa
Algeria |Algiers
Tunisia |Tunis
Morocco |RabatIt is the opposite of INTERSECT conceptually. In reality, the first query we wrote before EXCEPT is the one whose rows we pull all of, and then we remove the rows that are common between the two queries.