MAX_BY

All functions > AGGREGATE > MAX_BY

Returns the maximum value in the group by the given expression.

Syntax

MAX_BY(expr: V, expr_min_max: K) → V
sql

Type Parameters:

  • V: Any type

  • K: Any comparable type

Arguments

ParameterTypeRequiredDescription
exprVYesThe value expression to return
expr_min_maxKYesThe expression to maximize
num_valuesANYNoOptional number of values to return as array

Returns

The value of expr where expr_min_max is maximum

Notes

  • Returns the value of expr at the row where expr_min_max is largest
  • Useful for finding associated values at extremes (e.g., name of highest scorer)
  • NULL values in expr_min_max are excluded
  • Returns NULL if all values are NULL or group is empty
  • With num_values parameter, returns array of top N associated values
  • Can be used with WHERE clause to filter before aggregation
  • Can be used with GROUP BY clause for grouped aggregation
Last update at: 2025/10/13 10:23:46