# Map functions

Map functions operate on typed, ordered key/value pairs. Duplicate keys remain present; a lookup selects the first matching pair.

## Functions

| Function                 | Result                                                 |
| ------------------------ | ------------------------------------------------------ |
| `map(key, value, ...)`   | A map constructed from alternating keys and values.    |
| `map_keys(map)`          | An array of keys in pair order, including duplicates.  |
| `map_values(map)`        | An array of values in pair order.                      |
| `map_element(map, key)`  | The first matching value, or the value type's default. |
| `map_contains(map, key)` | A Bool indicating whether the key occurs.              |

Keys cannot be NULL. Typed map declarations validate every key and value. A missing key and a stored default can return the same value; use `map_contains` to distinguish them.

## Examples

### Duplicate keys

```sql
SELECT map_keys(map('a', 1, 'a', 2)),
       map_values(map('a', 1, 'a', 2)),
       map_element(map('a', 1, 'a', 2), 'a')
FORMAT csv;
```

```text
"[""a"",""a""]","[1,2]",1
```

### A stored default and a missing key

```sql
SELECT map_element(map('views', 0), 'views'),
       map_contains(map('views', 0), 'views'),
       map_element(map('views', 0), 'likes'),
       map_contains(map('views', 0), 'likes')
FORMAT csv;
```

```text
0,true,0,false
```

See the [Map type](https://coordinatordb.com/docs/reference/data-types/map) for JSON input, value defaults and nullable values.
