max()
function returns the maximum value in a set of values.
Syntax: max(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 max(n.age)
The highest 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 65.
Considerations
- All null values are excluded from the calculations.
- In a mixed set, any numeric value is always considered to be higher than any string value, and any string value is always considered to be higher than any list.
- 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.
max(null)
returnsnull
.
Example
UNWIND ['-17', 0, 'a', NULL , 4, -10, 'B', '1', '99'] AS val
RETURN max(val)
Returns the following Result
Max(val) |
4 |
Example
UNWIND [[1, 2, 10, 100],[1, 4]] AS val
RETURN max(val)
Returns the following Result
Max(val) |
[1, 4] |
Even though the first list contains more elements than the second list, the second 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 second list(4) is greater than the second element in the first list(2). Hence the second list is returned.
Example
UNWIND [[1, 'a', '100', 99],['b', 4]] AS val
RETURN max(val)
Returns the following Result
Max(val) |
[1, 'a', '100', 99] |
1 is considered a higher value than ‘b’, hence first list is returned as the result after comparing the lists element wise.