---
title: Set operators
description: Combine query results with the UNION, INTERSECT, EXCEPT, and MINUS set operators in Databricks SQL and Databricks Runtime.
canonical_url: https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-setops
---

# Set operators

**Applies to:** ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks SQL ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks Runtime

Combines the preceding `subquery1` and `subquery2` into a single one. Databricks supports three types of set operators:

* `EXCEPT`
* `INTERSECT`
* `UNION`

## Syntax

```
{ UNION | INTERSECT | EXCEPT } [ ALL | DISTINCT ] subquery2 } [...]
```

* **[subquery2](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select.md)**

  Any two `subquery` clauses as specified in [`SELECT` (subselect)](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select.md).
  Both subqueries must have the same number of columns and share a least common type for each respective column.
  If the number of columns differs, Databricks raises [NUM\_COLUMNS\_MISMATCH](https://docs.databricks.com/aws/en/error-messages/error-classes.md#num_columns_mismatch). If corresponding columns have incompatible types, Databricks raises [INCOMPATIBLE\_COLUMN\_TYPE](https://docs.databricks.com/aws/en/error-messages/error-classes.md#incompatible_column_type).

* **UNION \[ALL | DISTINCT]**

  Returns the result of `subquery1` plus the rows of `subquery2`.

  If `ALL` is specified duplicate rows are preserved.

  If `DISTINCT` is specified the result does not contain any duplicate rows. This is the default.

* **INTERSECT \[ALL | DISTINCT]**

  Returns the set of rows which are in both subqueries.

  If `ALL` is specified a row that appears multiple times in the `subquery1` as well as in `subquery` will be returned multiple times.

  If `DISTINCT` is specified the result does not contain duplicate rows. This is the default.

* **EXCEPT \[ALL | DISTINCT ]**

  Returns the rows in `subquery1` which are not in `subquery2`.

  If `ALL` is specified, each row in `subquery2` will remove exactly one of possibly multiple matches from `subquery1`.

  If `DISTINCT` is specified, duplicate rows are removed from `subquery1` before applying the operation,
  so all matches are removed and the result will have no duplicate rows (matched or unmatched). This is the default.

  You can specify `MINUS` as a syntax alternative for `EXCEPT`.

When chaining set operations `INTERSECT` has a higher precedence than `UNION` and `EXCEPT`.

The type of each result column is the least common type of the respective columns in `subquery1` and `subquery2`.

If the result column is a `STRING` its collation is derived by applying the [collation precedence rules](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-collation.md#collation-precedence) to the columns in the subqueries.

## Common error conditions

* [INCOMPATIBLE\_COLUMN\_TYPE](https://docs.databricks.com/aws/en/error-messages/error-classes.md#incompatible_column_type)
* [NUM\_COLUMNS\_MISMATCH](https://docs.databricks.com/aws/en/error-messages/error-classes.md#num_columns_mismatch)
* [UNSUPPORTED\_FEATURE.SET\_OPERATION\_ON\_MAP\_TYPE](https://docs.databricks.com/aws/en/error-messages/unsupported-feature-error-class.md#set_operation_on_map_type)

## Examples

```sql
-- Use number1 and number2 tables to demonstrate set operators in this page.
> CREATE TEMPORARY VIEW number1(c) AS VALUES (3), (1), (2), (2), (3), (4);

> CREATE TEMPORARY VIEW number2(c) AS VALUES (5), (1), (1), (2);

> SELECT c FROM number1 EXCEPT SELECT c FROM number2;
  3
  4

> SELECT c FROM number1 MINUS SELECT c FROM number2;
  3
  4

> SELECT c FROM number1 EXCEPT ALL (SELECT c FROM number2);
  3
  3
  4

> SELECT c FROM number1 MINUS ALL (SELECT c FROM number2);
  3
  3
  4

> (SELECT c FROM number1) INTERSECT (SELECT c FROM number2);
  1
  2

> (SELECT c FROM number1) INTERSECT DISTINCT (SELECT c FROM number2);
  1
  2

> (SELECT c FROM number1) INTERSECT ALL (SELECT c FROM number2);
  1
  2
  2

> (SELECT c FROM number1) UNION (SELECT c FROM number2);
  1
  3
  5
  4
  2

> (SELECT c FROM number1) UNION DISTINCT (SELECT c FROM number2);
  1
  3
  5
  4
  2

> SELECT c FROM number1 UNION ALL (SELECT c FROM number2);
  3
  1
  2
  2
  3
  4
  5
  1
  1
  2

-- Different number of columns.
> SELECT c FROM number1 UNION SELECT c, c FROM number2;
  Error: NUM_COLUMNS_MISMATCH
```

## Related articles

* [SELECT](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select.md)
