---
title: "Collection types: SET, MULTISET, LIST/SEQUENCE"
description: "CoraDB collection types SET, MULTISET and LIST: collection columns and literals, duplicates and order, conversions and restrictions."
url: "https://www.coradb.ai/manual/sql/datatype_collection.html"
markdown_url: "https://www.coradb.ai/manual/markdown/sql/datatype_collection.md"
product: "CoraDB"
version: "1.0"
keywords: "coradb set, multiset, list, sequence, collection types, collection literal, subseteq"
---

# Collection types: SET, MULTISET, LIST/SEQUENCE

## Collection types

A collection column stores several values of the same element type in one column of one row. CoraDB has three collection types, which differ in how they treat duplicates and order:

| Type | Duplicates | Order | Input | Stored |
|---|---|---|---|---|
| **SET** | Removed | Sorted | `{'c','c','c','b','b','a'}` | `{'a', 'b', 'c'}` |
| **MULTISET** | Kept | Sorted | `{'c','c','c','b','b','a'}` | `{'a', 'b', 'b', 'c', 'c', 'c'}` |
| **LIST** or **SEQUENCE** | Kept | As entered | `{'c','c','c','b','b','a'}` | `{'c', 'c', 'c', 'b', 'b', 'a'}` |

A collection column is declared with the element type after the collection keyword, with or without parentheses:

```sql
col_name SET VARCHAR(20)
col_name MULTISET (VARCHAR(20))
col_name LIST SHORT
col_name SEQUENCE (INT)
```

A collection value is written in braces, with the elements separated by commas: `{'running', 'shoes'}`, `{42, 40, 41}`. A literal in braces on its own is a **SEQUENCE**; it is converted to the column's type on **INSERT** or **UPDATE**, or with [CAST](https://www.coradb.ai/manual/sql/function/typecast_fn.html#cast):

```sql
SELECT CAST({'c','c','c','b','b','a'} AS SET) AS s, CAST({'c','c','c','b','b','a'} AS MULTISET) AS m, CAST({'c','c','c','b','b','a'} AS LIST) AS l;
```

| s | m | l |
|---|---|---|
| {'a', 'b', 'c'} | {'a', 'b', 'b', 'c', 'c', 'c'} | {'c', 'c', 'c', 'b', 'b', 'a'} |

**Restrictions**

- Elements can be of any type except **BLOB** and **CLOB** (`Can not define 'set of clob' as a column type`).
- A collection column cannot have a collation: `SET (STRING) COLLATE utf8_en_ci` is a syntax error.
- A scalar value is not a collection: `INSERT INTO t VALUES ('')` into a **SET** column fails with `Casting '' to type set is not supported.` Write `{''}`.
- Collections are never converted implicitly to other types. Use **CAST** between collection types.

**Converting between collection types**

Any collection type can be cast to any other. Converting to **SET** removes duplicates, and converting to **SET** or **MULTISET** sorts the elements. Converting a **SET** or **MULTISET** to **LIST** keeps the sorted order; the original input order of a **MULTISET** is not kept.

### Example

The following table stores tags and available sizes for products of the e-commerce sample database:

```sql
CREATE TABLE product_extra (
    product_id INT PRIMARY KEY,
    tags       SET VARCHAR(20),
    sizes      LIST SHORT
);
INSERT INTO product_extra VALUES (1618, {'running', 'shoes', 'running'}, {42, 40, 41, 40});
SELECT product_id, tags, sizes FROM product_extra;
```

| product_id | tags | sizes |
|---|---|---|
| 1618 | {'running', 'shoes'} | {42, 40, 41, 40} |

The duplicate tag is removed by the **SET**; the **LIST** keeps the sizes as entered, duplicates included.

Collections are queried with the containment operators, such as **IN** and **SUBSETEQ**, described in [Containment operators](https://www.coradb.ai/manual/sql/function/containment_op.html):

```sql
SELECT product_id FROM product_extra WHERE 'shoes' IN tags;
SELECT product_id FROM product_extra WHERE tags SUBSETEQ {'running', 'shoes', 'sale'};
```

Both queries return product 1618.

### SET

A **SET** holds distinct values, sorted. **NULL** and the empty string are values like any other:

| Inserted into a **SET (CHAR(1))** column | Stored |
|---|---|
| `{'c','c','c','b','b','a'}` | `{'a', 'b', 'c'}` |
| `{NULL}` | `{NULL}` |
| `{''}` | `{' '}` (**CHAR(1)** pads the empty string to one space) |

### MULTISET

A **MULTISET** keeps duplicates and sorts the elements. `{'c','c','c','b','b','a'}` is stored as `{'a', 'b', 'b', 'c', 'c', 'c'}`; cast to **SET** it is `{'a', 'b', 'c'}`.

### LIST/SEQUENCE

A **LIST** (or **SEQUENCE**, the same type) keeps duplicates and the order in which the elements were entered. `{'c','c','c','b','b','a'}` is stored as is; cast to **SET** it is `{'a', 'b', 'c'}`, cast to **MULTISET** `{'a', 'b', 'b', 'c', 'c', 'c'}`.

The [list functions](https://www.coradb.ai/manual/sql/function/list_fn.html) work on **LIST** values, and **TYPEOF** reports a **LIST** as `sequence`.
