Skip to content

Work with Map values ​

A Map stores ordered key/value pairs with declared key and value types. This quickstart stores text labels for one account.

Create and insert a map ​

sql
CREATE TABLE labels(id INTEGER PRIMARY KEY, tags Map(String, String));
INSERT INTO labels VALUES (1, map('team', 'sales', 'region', 'east'));

map takes alternating keys and values. A typed Map validates every pair. Keys cannot be NULL; duplicate keys are retained.

Read values and test presence ​

sql
SELECT map_element(tags, 'team') AS team,
       map_contains(tags, 'missing') AS has_missing,
       map_element(tags, 'missing') AS missing_value
FROM labels
FORMAT csv;
text
"sales",false,""

A missing key returns the value type's default, an empty string here. map_contains distinguishes a missing key from a stored empty value. If a key is repeated, map_element returns its first matching value.

Replace a map ​

sql
UPDATE labels
SET tags = map('team', 'support', 'region', 'west')
WHERE id = 1;
sql
SELECT map_element(tags, 'team'), map_keys(tags)
FROM labels
FORMAT csv;
text
"support","[""team"",""region""]"

See the Map reference and map functions for additional operations.