length

Counts the elements in an array.

Syntax

length(<array>)

Arguments

ArgumentTypeDescription
<array>arrayArray to measure. Cast an any value to array first.

Returns

Returns int. Counts all elements, including NULL. An empty array has length 0. Returns NULL if the input is NULL.

Examples

Measure basket size from the item-ID array attached to each order.

VALUES
    ('o-17', ['sku-1', 'sku-2', 'sku-3']),
    ('o-42', ['sku-9']), ('o-63', [])
SELECT $0 AS order_id, $1 AS item_ids
SELECT order_id, length(item_ids) AS item_count
+----------+------------+
| order_id | item_count |
+----------+------------+
| o-17     | 3          |
| o-42     | 1          |
| o-63     | 0          |
+----------+------------+