# 5. Set Operations

### Union

**(*****combine result sets of multiple queries into a single result set.*****)**

*

```
<figure><img src="https://1920086362-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDfv51K0WXLZdwTryHQZc%2Fuploads%2FhRDSmQEfjdHYhdPAcRrj%2Fimage.png?alt=media&#x26;token=6ebf211b-6cb5-4733-9d79-b80d364a8b7a" alt=""><figcaption></figcaption></figure>
```

* Combines result sets of two or more `SELECT` statements into a single result set
* The queries must conform to the following rules:
  * The number and order of columns in the select list of both queries must be same
  * The data types must be compatible
* It also removes all the duplicate rows from the combined data set
  * To retain the duplicate rows, you should use the `UNION ALL` instead

#### UNION with ORDER BY clause

* `UNION` operator may place the rows from the result ret of the first query before or after or between the rows from the result set of the second query
* To sort the rows in the final result set, you use the `ORDER BY` caluse in the second query
  * If you place the ORDER BY clause at the end of each query, the combined result set will not be sorted as you expected.

#### Examples

We have a table `top_rated_films`

<figure><img src="https://1920086362-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDfv51K0WXLZdwTryHQZc%2Fuploads%2FcacHN8rT4zxP8SpilS10%2Fimage.png?alt=media&#x26;token=177d03b9-b81d-42ae-9ef0-b1ac039980a0" alt=""><figcaption></figcaption></figure>

We have a table `most_popular_films`

<figure><img src="https://1920086362-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDfv51K0WXLZdwTryHQZc%2Fuploads%2FMfBSHGpD8m7bFBHFlTtT%2Fimage.png?alt=media&#x26;token=4aa1b770-fa27-43f1-a57b-5a1d3d96ed31" alt=""><figcaption></figcaption></figure>

**1. Combine data from both tables**

```sql
SELECT * from top_rated_films
UNION
SELECT * from most_popular_films;
```

<figure><img src="https://1920086362-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDfv51K0WXLZdwTryHQZc%2Fuploads%2FrJOhaFWV26OpIqjotsyx%2Fimage.png?alt=media&#x26;token=c4e806bf-545e-48a7-9f5b-00351daeb324" alt=""><figcaption></figcaption></figure>

**2. Combine result sets from both tables including duplicates**

```sql
SELECT * from top_rated_films
UNION ALL
SELECT * from most_popular_films;
```

<figure><img src="https://1920086362-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDfv51K0WXLZdwTryHQZc%2Fuploads%2F0EbjHkhBXGHMXlzQKrhP%2Fimage.png?alt=media&#x26;token=bb10e6f9-416f-436c-aa3c-74bbdc71bdbe" alt=""><figcaption></figcaption></figure>

**3. Combine result sets from both tables including duplicates and ordering them**

```sql
SELECT * from top_rated_films
UNION ALL
SELECT * from most_popular_films
ORDER BY title;
```

<figure><img src="https://1920086362-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDfv51K0WXLZdwTryHQZc%2Fuploads%2FpkBZ7daHU528RF4McR5p%2Fimage.png?alt=media&#x26;token=232870a5-db17-4883-860f-b322d043207e" alt=""><figcaption></figcaption></figure>

### Intersect

**(*****combine the result sets of two or more queries and returns a single result set that has the rows appear in both result sets.*****)**

*

```
<figure><img src="https://1920086362-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDfv51K0WXLZdwTryHQZc%2Fuploads%2F48IZEAQ4agJDXwf70jAE%2Fimage.png?alt=media&#x26;token=9b1a6ecc-5591-4d63-84ce-749bef27d7f0" alt=""><figcaption></figcaption></figure>
```

* This operator combines result sets of two or more SELECT statements into a single result set
* It returns any rows that are available in both result sets.

#### PostgreSQL INTERSECT with ORDER BY clause

* If you want to sort the result set returned by the `INTERSECT` operator, you place the `ORDER BY` at the final query in the query list like in the `UNION` operator

#### Example (Get popular films which are also top rated films)

```sql
SELECT *
FROM most_popular_films
INTERSECT
SELECT *
FROM top_rated_films;
```

<figure><img src="https://1920086362-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDfv51K0WXLZdwTryHQZc%2Fuploads%2FMS1Cf3zNOBTdlO1FFV3d%2Fimage.png?alt=media&#x26;token=38af2115-2532-4c8e-96f4-f2d31112373f" alt=""><figcaption></figcaption></figure>

### Except

**(*****return the rows in the first query that does not appear in the output of the second query.*****)**

*

```
<figure><img src="https://1920086362-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDfv51K0WXLZdwTryHQZc%2Fuploads%2F2EXHGMsqdSHLZwIKoa7l%2Fimage.png?alt=media&#x26;token=22af1db3-3d6c-498e-b891-1d5b22a2b96a" alt=""><figcaption></figcaption></figure>
```

* The `EXCEPT` operator returns distinct rows from the first (left) query that are not in the output of the second (right) query.
* The queries need to follow these rules
  * The number of columns and their orders must be same in two queries
  * The data types of respective columns must be compatible

#### Example (Find top-rated films that are not popular)

```sql
SELECT * FROM top_rated_films
EXCEPT
SELECT * FROM most_popular_films
ORDER BY title;
```

***


---

# 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://notes.nomanaziz.me/development/database/sql/basics/5.-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.
