Difference (–) is a set‑oriented relational‑algebra operation that returns tuples that appear in the first relation but not in the second. It is also called set difference or minus. In simple terms, the difference operation removes from the first table all rows that also exist in the second table.
Difference is very useful when you want to answer questions like “which employees are in department A but not in department B?” or “which customers are in list X but not in list Y?” It is the relational‑algebra way of expressing exclusion.
When Is Difference Allowed? (Union Compatibility)
Like union and intersection, difference requires that the two relations are union‑compatible:
They must have the same number of attributes.
The attributes must be in the same order.
Corresponding attributes must have compatible domains (same or compatible data types).
If the two relations are not union‑compatible, the difference operation is not defined and cannot be applied.
Syntax of Difference
The general syntax is:
: the first (left) relation.
: the second (right) relation.
The result is a new relation with the same schema as and , containing only the tuples that are in and not in .
Because relations are treated as sets, each tuple appears at most once in the result.
Example of Difference
Consider two tables storing temporary employee data with the same structure:
TEMP_A(emp_id, name, salary)
TEMP_B(emp_id, name, salary)
TEMP_A contains:
TEMP_B contains:
The difference:
Gives a result like:
These are the rows that exist in TEMP_A but not in TEMP_B. The row for Bob (102) is removed because it is present in both tables.
If you compute the reverse difference:
you get:
This shows that difference is not commutative. The order of the relations matters.
Key Properties of Difference
Non‑commutative:
Not idempotent in the usual sense:
because removing all tuples of from itself leaves an empty relation.
Difference only works on union‑compatible relations; otherwise the operation is invalid.
Why Difference Matters in DBMS?
It allows expressing exclusion in queries, such as “give me all employees in department A who are not in department B” or “customers in June who did not appear in May.”
It corresponds to SQL’s MINUS or EXCEPT operators in many databases, which are used to subtract one result set from another.
It helps students understand the full set of relational‑algebra operations and how they mirror set theory (union, intersection, and difference).