min()
function returns the minimum value in a set of values.
Syntax: min(expression)
Returns: A property type or a list depending on the values returned by the expression.
Arguments
Name | Description |
expression | An expression returning a set containing any combination of property types and lists thereof. |
Example
MATCH (n:Person)
RETURN min(n.age)
The lowest of all the values in the property age for all nodes of the type ‘Person’ is returned. So for examples if we have 5 nodes of the type ‘Person’ and their values for the property age are- 23, 65, 32, 37, 18. The result returned by the query will be 18.
Considerations
- All null values are excluded from the calculations.
- In a mixed set, any string value is always considered to be lower than any numeric value, and any list is always considered to be lower than any string.
- Lists are compared in dictionary order, i.e. list elements are compared pairwise in ascending order from the start of the list to the end.
min(null)
returnsnull
.
Example
UNWIND ['-17', 0, 'a', NULL , 4, -10, 'B', '1', '99'] AS val
RETURN min(val)
Returns the following Result
Min(val) |
"-17" |
Example
UNWIND [[1, 2, 10, 100],[1, 4]] AS val
RETURN min(val)
Returns the following Result
Min(val) |
[1, 2, 10, 100] |
Even though the second list contains less elements than the first list, the first list is returned because the lists are compared element wise. The first element is same in both the lists but the second element in the first list(2) is lesser than the second element in the second list(4). Hence the first list is returned.
Example
UNWIND [[1, 'a', '100', 99],['b', 4]] AS val
RETURN min(val)
Returns the following Result
Min(val) |
['b', 4] |
‘b’ is considered a lower value than 1, hence second list is returned as the result after comparing the lists element wise.