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:

RSR - S
  • RR: the first (left) relation.

  • SS: the second (right) relation.

  • The result is a new relation with the same schema as RR and SS, containing only the tuples that are in RR and not in SS.

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:

emp_idnamesalary
101Alice45000
102Bob50000
103Charlie55000

TEMP_B contains:

emp_idnamesalary
102Bob50000
104David60000

The difference:

TEMPATEMPBTEMP_A - TEMP_B

Gives a result like:

emp_idnamesalary
101Alice45000
103Charlie55000

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:

TEMPBTEMPATEMP_B - TEMP_A

you get:

emp_idnamesalary
104David60000

This shows that difference is not commutative. The order of the relations matters.

Key Properties of Difference

  • Non‑commutative:

    RSSR(in general)R - S \ne S - R \quad \text{(in general)}
  • Not idempotent in the usual sense:

    RR=R - R = \emptyset

    because removing all tuples of RR 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).