---
title: "Understanding SQL Set Operators: UNION, INTERSECT, MINUS"
description: "Here are top rules of set operators in SQL with examples"
url: https://srinimf.com/2021/03/21/10-top-sql-set-operator-rules/
date: 2021-03-21
modified: 2026-10-10
author: "Srini"
image: https://i0.wp.com/srinimf.com/wp-content/uploads/2021/03/10-sql-set-operator-rules.png?fit=2240%2C1260&ssl=1
categories: ["SQL"]
tags: ["DB2 Set Operators", "Rules"]
type: post
lang: en
---

# Understanding SQL Set Operators: UNION, INTERSECT, MINUS

**Set operators useful **when you want to read or filter data of multiple tables. You can’t apply set operators on a single table.

The popular set operators are **INTERSECT**, **UNION** and **MINUS.** Set operators useful for data analytics projects.

## Set operator rules

1. Each query will have at least two SELECT Statements separated by a SET Operator
2. SET Operators are UNION, INTERSECT, or EXCEPT/MINUS
3. Must specify the same number of columns from the same domain (data type/range)
4. If using Aggregates, both SELECTs much have their own GROUP BY
5. Both SELECTS must have a FROM Clause
6. The First SELECT is used for all ALIAS and FORMAT Statements
7. The Second SELECT will have the ORDER BY statement which must be a number
8. When multiple operators the order of precedence is INTERSECT, UNION, and MINUS
9. Parentheses can change the order of Precedence
10. Duplicate rows are eliminated in the spool unless the ALL keyword is used

## SQL Examples for SET Operators

Here are **set operators in sql with examples**.

### 1. UNION Example

```
SELECT * FROM MY_TABLE
UNION
SELECT * FROM SRINIMF_TAB;
```

Related[Understanding SQL LIKE, ILIKE, and RLIKE Operators](https://srinimf.com/2025/01/16/databricks-sql-using-like-ilike-rlike-effectively/)

The results of this query is it pulls all the unique records. You need [to use](https://srinimf.com/2017/12/13/db2-set-operators-to-never-miss-these/) UNION ALL to get duplicate records.

### 2. Intersect Example

```
SELECT * FROM MY_TABLE 
INTERSECT
 SELECT * FROM SRINIMF_TAB;
```

The result of this query is only matching rows you will get.

### 3. Minus Example

```
SELECT * FROM MY_TABLE
MINUS
SELECT * FROM SRINIMF_TAB;
```

The result of this query is it excludes matching rows, and pulls remaining rows.

References

- [Tera-Tom Genius Series – DB2 SQL](https://amzn.to/3saLHdK)

### Share this:

-
- [Tweet](https://twitter.com/share)
- [![](https://i0.wp.com/assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png)](https://www.pinterest.com/pin/create/link/?url=https%3A%2F%2Fsrinimf.com%2F2021%2F03%2F21%2F10-top-sql-set-operator-rules%2F&media=https%3A%2F%2Fi0.wp.com%2Fsrinimf.com%2Fwp-content%2Fuploads%2F2021%2F03%2F10-sql-set-operator-rules.png%3Ffit%3D1200%252C675%26ssl%3D1&description=Understanding%20SQL%20Set%20Operators%3A%20UNION%2C%20INTERSECT%2C%20MINUS)
-
- [Share on Tumblr](https://www.tumblr.com/share)
- [Share on WhatsApp (Opens in new window) WhatsApp](https://srinimf.com/2021/03/21/10-top-sql-set-operator-rules/?share=jetpack-whatsapp)
- [More](#)
-

- [Print (Opens in new window) Print](https://srinimf.com/2021/03/21/10-top-sql-set-operator-rules/?share=print)
- [Share on Reddit (Opens in new window) Reddit](https://srinimf.com/2021/03/21/10-top-sql-set-operator-rules/?share=reddit)
- [Share on Telegram (Opens in new window) Telegram](https://srinimf.com/2021/03/21/10-top-sql-set-operator-rules/?share=telegram)
- [Email a link to a friend (Opens in new window) Email](mailto:?subject=%5BShared%20Post%5D%20Understanding%20SQL%20Set%20Operators%3A%20UNION%2C%20INTERSECT%2C%20MINUS&body=https%3A%2F%2Fsrinimf.com%2F2021%2F03%2F21%2F10-top-sql-set-operator-rules%2F&share=email)
-

### Like this:

Like Loading…
