# Set Operations

We can perform the following set operations in SQL:

* union
* intersect
* except (for difference)

```
 select * from r union/intersect/except select * from s
```

Each of these operations automatically eliminates duplicates. To retain duplicates, we must use **union all**, **intersect all** and **except all**.

Suppose a tuple occurs m times in r and n times in s, then, it occurs:

* m + n times in r union all s
* min(m,n) times in r intersect all s
* max(0, m–n) times in r except all s


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://vikram-bajaj.gitbook.io/cs-gy-6083-principles-of-database-systems/main-1/sql/dml-commands/set-operations.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
