DB2: How to Use Set Operators Example SQL

Guys welcome to DB2 SQL. There are mostly used popular set operators. These set operators you always need to keep handy especially on how to use in your SQL while writing Queries for your project.

Set Operators

  1. Union
  2. Union all
  3. Except
  4. Minus
  5. Intersect

And how to use multiple operators is also you need to learn. In my previous post I have explained the real use of these operators.

Before going in detail about all operators, I am giving here some good comparison on UNION Vs UNION ALL

How to Use UNION

The simple UNION is the same operator you had in high school set theory; it returns the rows that appear in either or both tables and removes redundant duplicates from the result table.

(SELECT a1 FROM S1
 UNION
 SELECT a2 FROM S2);

How to Use UNION All

The UNION ALL preserves the duplicates from both tables in the result table.

(SELECT a1 FROM S1
 UNION ALL
 SELECT a2 FROM S2)

Keep Reading

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.