x associated with the maximum value of y.
For example, SELECT max_by(subject, exam_result) FROM exam; is to return the subject that has the highest exam score.
This function is supported from v2.5.
Syntax
Parameters
x: an expression of any type.y: an expression of a type that can be ordered.
Return value
Returns a value that has the same type asx.
Usage notes
ymust be a sortable type. If you use an unsortable type ofy, such asbitmaporhll, an error is returned.- If
ycontains a null value, the row that corresponds to the null value is ignored. - If more than one value of
xhas the same maximum value ofy, this function returns the first value ofxencountered.
Examples
-
Create a table
exam. -
Insert values into this table and query data from this table.
-
Obtain the subject that has the highest score.
Two subjects
physicsandmusichave the same highest score95and the first subject encountered (physics) is returned.