---
title: "Set arithmetic operators"
description: "Union (+), difference (-) and intersection (*) of CoraDB collection values (SET, MULTISET, LIST), and the result type."
url: "https://www.coradb.ai/manual/sql/function/set_op.html"
markdown_url: "https://www.coradb.ai/manual/markdown/sql/function/set_op.md"
product: "CoraDB"
version: "1.0"
keywords: "set operators, collection union, collection difference, collection intersection, set, multiset, list, sequence, coradb"
---

# Set arithmetic operators

## SET, MULTISET, LIST

To compute union, difference or intersection of collections types (**SET**, **MULTISET**, and **LIST (SEQUENCE)**), you can use +, -, or \* operators, respectively.

```text
<value_expression>  <set_arithmetic_operator> <value_expression>

    <value_expression> ::=
        collection_value |
        NULL

    <set_arithmetic_operator> ::=
        + (union) |
        - (difference) |
        * (intersection)
```

The following table shows a result data type by the operator if collection type is an operand.

**Result data type by operand type**

|                          | SET                                   | MULTISET                              | LIST                                     |
|--------------------------|---------------------------------------|---------------------------------------|------------------------------------------|
| **SET**                  | **+** , **-** , **\*** : **SET**      | **+** , **-** , **\*** : **MULTISET** | **+** , **-** , **\*** : **MULTISET**    |
| **MULTISET**             | **+** , **-** , **\*** : **MULTISET** | **+** , **-** , **\*** : **MULTISET** | **+** , **-** , **\*** : **MULTISET**    |
| **LIST** **(=SEQUENCE)** | **+, -,**\* : **MULTISET**            | **+, -,**\* : **MULTISET**            | **+** : **LIST** **-,**\* : **MULTISET** |

The following are the examples which execute arithmetic operations with collection types.

```sql
SELECT ((CAST ({3,3,3,2,2,1} AS SET))+(CAST ({4,3,3,2} AS MULTISET)));
```

| ((CAST ({3,3,3,2,2,1} AS SET))+(CAST ({4,3,3,2} AS MULTISET))) |
|---|
| {1, 2, 2, 3, 3, 3, 4} |

```sql
SELECT ((CAST ({3,3,3,2,2,1} AS MULTISET))+(CAST ({4,3,3,2} AS MULTISET)));
```

| ((CAST ({3,3,3,2,2,1} AS MULTISET))+(CAST ({4,3,3,2} AS MULTISET))) |
|---|
| {1, 2, 2, 2, 3, 3, 3, 3, 3, 4} |

```sql
SELECT ((CAST ({3,3,3,2,2,1} AS LIST))+(CAST ({4,3,3,2} AS MULTISET)));
```

| ((CAST ({3,3,3,2,2,1} AS LIST))+(CAST ({4,3,3,2} AS MULTISET))) |
|---|
| {1, 2, 2, 2, 3, 3, 3, 3, 3, 4} |

```sql
SELECT ((CAST ({3,3,3,2,2,1} AS SET))-(CAST ({4,3,3,2} AS MULTISET)));
```

| ((CAST ({3,3,3,2,2,1} AS SET))-(CAST ({4,3,3,2} AS MULTISET))) |
|---|
| {1} |

```sql
SELECT ((CAST ({3,3,3,2,2,1} AS MULTISET))-(CAST ({4,3,3,2} AS MULTISET)));
```

| ((CAST ({3,3,3,2,2,1} AS MULTISET))-(CAST ({4,3,3,2} AS MULTISET))) |
|---|
| {1, 2, 3} |

```sql
SELECT ((CAST ({3,3,3,2,2,1} AS LIST))-(CAST ({4,3,3,2} AS MULTISET)));
```

| ((CAST ({3,3,3,2,2,1} AS LIST))-(CAST ({4,3,3,2} AS MULTISET))) |
|---|
| {1, 2, 3} |

```sql
SELECT ((CAST ({3,3,3,2,2,1} AS SET))*(CAST ({4,3,3,2} AS MULTISET)));
```

| ((CAST ({3,3,3,2,2,1} AS SET))*(CAST ({4,3,3,2} AS MULTISET))) |
|---|
| {2, 3} |

```sql
SELECT ((CAST ({3,3,3,2,2,1} AS MULTISET))*(CAST ({4,3,3,2} AS MULTISET)));
```

| ((CAST ({3,3,3,2,2,1} AS MULTISET))*(CAST ({4,3,3,2} AS MULTISET))) |
|---|
| {2, 3, 3} |

```sql
SELECT ((CAST ({3,3,3,2,2,1} AS LIST))*(CAST ({4,3,3,2} AS MULTISET)));
```

| ((CAST ({3,3,3,2,2,1} AS LIST))*(CAST ({4,3,3,2} AS MULTISET))) |
|---|
| {2, 3, 3} |

### Assigning collection value to variable

For a collection value to be assigned to a variable, the outer query must return a single row as a result.

The following example shows how to assign a collection value to a variable. The outer query must return only a single row as follows:

```sql
CREATE TABLE people (
    ssn VARCHAR(10),
    name VARCHAR(255)
);

INSERT INTO people 
VALUES ('1234', 'Ken'), ('5678', 'Dan'), ('9123', 'Jones');

SELECT SET(SELECT name
FROM people
WHERE ssn in {'1234', '5678'})
TO :name_group;
```
