# `max`, `maxs`, `mmax`

## `max`

_Maximum_
```syntax
max x    max[x]
```

Where `x` is a non-symbol sortable list, returns the maximum of its items.
The maximum of an atom is itself.
From KDB-X 5.0.20261015, `max` supports GUIDs.

`max` ignores nulls. If all items of `x` are null, or `x` is empty, the result is negative infinity. For types without an infinity, such as GUIDs, the result is null.
```q
q)max 2 5 7 1 3
7
q)max "genie"
"n"
q)max 0N 5 0N 1 3                  / nulls are ignored
5
q)max 0N 0N                        / negative infinity if all null
-0W
q)t:([]sym:`a`b`a;price:10 20 30)
q)select max price by sym from t   / use in a select statement
sym| price
---| -----
a  | 30
b  | 20
```

`max` is an aggregate function. It is equivalent to `|/`.
```txt
domain: B G X H I J E F C S P M D Z N U V T
range:  b g x h i j e f c . p m d z n u v t
```

`max` is a [multithreaded primitive](mt-primitives.md).

## `maxs`

_Maximums_
```syntax
maxs x    maxs[x]
```

Where `x` is a non-symbol sortable list, returns the running maximums of its prefixes.

Nulls are ignored, except that initial nulls are returned as negative infinity.
```q
q)maxs 2 5 7 1 3
2 5 7 7 7
q)maxs "genie"
"ggnnn"
q)maxs 0N 5 0N 1 3         / initial nulls return negative infinity
-0W 5 5 5 5
```

`maxs` is a uniform function. It is equivalent to `|\`.
```txt
domain: B G X H I J E F C S P M D Z N U V T
range:  B G X H I J E F C . P M D Z N U V T
```

## `mmax`

_Moving maximums_
```syntax
x mmax y    mmax[x;y]
```

Where

- `x` is a positive integer atom
- `y` is a non-symbol sortable list

returns the `x`-item moving maximums of `y`, with nulls after the first replaced by the preceding maximum. The first `x` items of the result are the maximums of the items so far, and thereafter the result is the moving maximum.
```q
q)3 mmax 2 7 1 3 5 2 8
2 7 7 7 5 5 8
q)/ initial null returns negative infinity; remaining nulls replaced by preceding max
q)3 mmax 0N -3 -2 0N 1 0
-0W -3 -2 -2 1 1
```

`mmax` is a uniform function.

Domain and range, with the type of `x` in rows and the type of `y` in columns:
```txt
    b g x h i j e f c s p m d z n u v t
----------------------------------------
b | b g x h i j e f c s p m d z n u v t
g | . . . . . . . . . . . . . . . . . .
x | b g x h i j e f c . p m d z n u v t
h | b g x h i j e f c . p m d z n u v t
i | b g x h i j e f c . p m d z n u v t
j | b g x h i j e f c . p m d z n u v t
e | . . . . . . . . . . . . . . . . . .
f | . . . . . . . . . . . . . . . . . .
c | . . . . . . . . . . . . . . . . . .
s | . . . . . . . . . . . . . . . . . .
p | . . . . . . . . . . . . . . . . . .
m | . . . . . . . . . . . . . . . . . .
d | . . . . . . . . . . . . . . . . . .
z | . . . . . . . . . . . . . . . . . .
n | . . . . . . . . . . . . . . . . . .
u | . . . . . . . . . . . . . . . . . .
v | . . . . . . . . . . . . . . . . . .
t | . . . . . . . . . . . . . . . . . .
```

Range: `bcdefghijmnpstuvxz`

## Implicit iteration

`max`, `maxs`, and `mmax` apply to [dictionaries and tables](math.md#dictionaries-and-tables).
```q
q)max`a`b!(10 21 3;4 5 6)
10 21 6
q)max flip`a`b!(10 21 3;4 5 6)
a| 21
b| 6

q)maxs`a`b!(10 21 3;4 5 6)
a| 10 21 3
b| 10 21 6
q)maxs flip`a`b!(10 21 3;4 5 6)
a  b
----
10 4
21 5
21 6

q)2 mmax flip`a`b!(10 21 3;4 5 6)
a  b
----
10 4
21 5
21 6
q)2 mmax`a`b!(10 21 3;4 5 6)
a| 10 21 3
b| 10 21 6

q)2 mmax ([k:`abc`def`ghi]a:10 21 3;b:4 5 6)
k  | a  b
---| ----
abc| 10 4
def| 21 5
ghi| 21 6
```

## Aggregating nulls

`avg`, `min`, `max`, and `sum` are special: they ignore nulls, to be consistent with SQL-92.
But for nested `x`, these functions combine the items item by item, so nulls are not ignored. Because null is less than any other value, `max` skips a null, but `min` returns it.
```q
q)max (1 2;0N 4)
1 4
q)min (1 2;0N 4)
0N 2
```

----

[Greater Than](greater-than.md)


[Comparison](comparison.md),
[Mathematics](math.md)


[Sliding windows](../examples/programming-idioms.md#how-do-i-apply-a-function-to-a-sequence-sliding-window)
