Union (∪) is a set‑oriented relational‑algebra operation that combines tuples from two relations into a single relation. It works exactly like the mathematical union of two sets. In simple terms, the union operation takes two tables with the same structure and returns all distinct rows that appear in either of them.
Union is very useful when you want to merge data from two similar tables, such as results from different departments, temporary tables, or different time periods, without worrying about duplicates.
When Is Union Allowed? (Union Compatibility)
Before applying union, two relations must be union‑compatible, which means:
They must have the same number of attributes.
The attributes must be in the same order.
Corresponding attributes must have compatible domains (for example, both
emp_idfields are of type integer, bothnamefields are of type string, and so on).
If the two relations are not union‑compatible, the union operation is not defined and cannot be performed.
Syntax of Union
The general syntax is:
and : the two input relations (tables).
The result is a new relation with the same schema as and , and all distinct tuples that appear in either or .
Because relational algebra treats relations as sets, duplicate tuples are automatically removed from the result.
Example of Union
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 union:
Gives a result like:
Notice that the row for emp_id 102 appears only once, even though it exists in both tables, because duplicates are removed.
Key Properties of Union
Union is commutative:
Union is idempotent:
because adding the same set again does not change the result.
Union only works on union‑compatible relations; otherwise the operation is invalid.
Why Union Matters in DBMS?
It allows combining results from different tables or sub‑queries that share the same structure.
It corresponds directly to SQL’s UNION operator used to merge query outputs.
It helps students understand that relations are sets of tuples, so operations like union behave just like set union in mathematics.