Loading
SQL INTERSECT & EXCEPT
In real world database systems, it is very common to compare data coming from different sources. Sometimes the same type of data is stored in multiple tables, sometimes the same data exists in different systems, and sometimes data is split across time periods such as current and historical records.

In such cases, the goal is not to join tables or merge columns. Instead, the requirement is to compare two result sets and find what is common or what is missing. SQL provides set operators for this exact purpose, and among them, INTERSECT and EXCEPT (called MINUS in Oracle) are extremely useful.


What is Set Operators

Set operators treat the output of SELECT queries as sets of rows and perform mathematical operations on them.

They are mainly used to:

  • Compare data between two queries
  • Find common records
  • Identify missing or extra records
  • Validate data consistency across systems


INTERSECT

INTERSECT is used when you want to extract only those rows that appear in both result sets. It helps in identifying overlap between two datasets.


When INTERSECT Use

  • Finding users present in two platforms
  • Matching records between old and new systems
  • Identifying common products, students, or employees


Student Table

online_students

name
Amit
Neha
Rahul


offline_students

name
Neha
Priya
Rahul


Example

SELECT name FROM online_students
INTERSECT
SELECT name FROM offline_students;


Output

name
Neha
Rahul

Key Observations

  • Only common records are returned
  • Duplicate rows are automatically removed
  • Order of records is not guaranteed


EXCEPT / MINUS

EXCEPT is used when you want records from the first result set that do not exist in the second. Oracle uses the keyword MINUS, but the behavior remains the same.


When EXCEPT Is Useful

  • Finding records missing in another table
  • Data audit and validation
  • Detecting mismatches between systems


Employees Table

payroll_employees

name
Amit
Neha
Rahul
Karan


attendance_employees

name
Amit
Neha


Example

SELECT name FROM payroll_employees
EXCEPT
SELECT name FROM attendance_employees;


(Oracle version)

SELECT name FROM payroll_employees
MINUS
SELECT name FROM attendance_employees;


Output

name
Rahul
Karan

Key Observations

  • Result comes only from the first query
  • Second query acts as a filter
  • Duplicate records are removed automatically


Difference Between INTERSECT and EXCEPT

AspectINTERSECTEXCEPT / MINUS
PurposeFind common dataFind missing data
DirectionBoth queriesFirst query only
Use caseOverlap detectionGap detection
Oracle supportYesMINUS keyword


Rules You Must Follow

For both INTERSECT and EXCEPT:

  • Same number of columns in all SELECT statements
  • Same column order
  • Compatible data types
  • ORDER BY is applied only at the end


When NOT to Use These Operators

Avoid INTERSECT or EXCEPT when:

  • Tables have different structures
  • You need column-wise combination (use JOIN)
  • Relationship logic is required
They are best suited for comparison, not relationship mapping.


Two Minute Drill

  • INTERSECT returns common records
  • EXCEPT / MINUS returns missing records
  • Both remove duplicates automatically
  • Column structure must match
  • Used for comparison, not joining
  • Very useful in audits and validation