> For the complete documentation index, see [llms.txt](https://vikram-bajaj.gitbook.io/cs-gy-6083-principles-of-database-systems/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://vikram-bajaj.gitbook.io/cs-gy-6083-principles-of-database-systems/sql/dml-commands/set-operations.md).

# 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
