SQLite Tutorial SQLite Advanced SQLite Database SQLite References

SQLite MAX() Function



The SQLite multi-argument MAX() function returns the argument with the maximum value, or return NULL if any argument is NULL. The multi-argument MAX() function searches its arguments from left to right for an argument that defines a collating function and uses that collating function for all string comparisons. If none of the arguments to MAX() define a collating function, then the BINARY collating function is used.

Note: When single argument is used, MAX() function operates as an aggregate function.

Syntax

MAX(expr1, expr2, ... expr_n)

Parameters

expr1, expr2, ... expr_n Required. Specify the list of expressions to be evaluated.

Return Value

Returns the greatest value in a list of expressions.

Example 1:

The example below shows the usage of MAX() function.

SELECT MAX(20, 30, 60, 10);
Result: 60

SELECT MAX('20', '30', '60', '10');
Result: '60'

SELECT MAX('D', 'G', 'X', 'A');
Result: 'X'

SELECT MAX('Alpha', 'Beta', 'Delta', 'Gamma');
Result: 'Gamma'

SELECT MAX('Alpha1', 'Alpha2', 'Alpha3', 'Alpha4');
Result: 'Alpha4'

SELECT MAX(20, 30, 60, 10, NULL);
Result: NULL

Example 2:

Consider a database table called Sample with the following records:

Dataxyz
Data 110011
Data 2201512
Data 3303013
Data 4404514
Data 5506015

To get the greatest value, when values of column x, column y and column z are compared, the following query can be used:

SELECT *, MAX(x, y, z) AS MAX_Value FROM Sample;

This will produce the result as shown below:

DataxyzMAX_Value
Data 11001111
Data 220151220
Data 330301330
Data 440451445
Data 550601560

❮ SQLite Functions