MAX¶
Returns the maximum values
Syntax¶
-- As an aggregate
MAX( expr )
-- As a window function
MAX ( expr ) OVER (
[ PARTITION BY value_expression [, ...] ]
[ ORDER BY value_expression [ ASC | DESC ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
)
Arguments¶
Parameter |
Description |
---|---|
|
Value expression |
Returns¶
Return type is dependant on the argument.
Notes¶
NULL
values are ignored
Examples¶
For these examples, assume a table named nba
, with the following structure:
CREATE TABLE nba
(
"Name" text,
"Team" text,
"Number" tinyint,
"Position" text,
"Age" tinyint,
"Height" text,
"Weight" real,
"College" text,
"Salary" float
);
Here’s a peek at the table contents (Download nba.csv
):
Avery Bradley |
Boston Celtics |
0 |
PG |
25 |
2-Jun |
180 |
Texas |
7730337 |
---|---|---|---|---|---|---|---|---|
Jae Crowder |
Boston Celtics |
99 |
SF |
25 |
6-Jun |
235 |
Marquette |
6796117 |
John Holland |
Boston Celtics |
30 |
SG |
27 |
5-Jun |
205 |
Boston University |
|
R.J. Hunter |
Boston Celtics |
28 |
SG |
22 |
5-Jun |
185 |
Georgia State |
1148640 |
Jonas Jerebko |
Boston Celtics |
8 |
PF |
29 |
10-Jun |
231 |
5000000 |
|
Amir Johnson |
Boston Celtics |
90 |
PF |
29 |
9-Jun |
240 |
12000000 |
|
Jordan Mickey |
Boston Celtics |
55 |
PF |
21 |
8-Jun |
235 |
LSU |
1170960 |
Kelly Olynyk |
Boston Celtics |
41 |
C |
25 |
Jul-00 |
238 |
Gonzaga |
2165160 |
Terry Rozier |
Boston Celtics |
12 |
PG |
22 |
2-Jun |
190 |
Louisville |
1824360 |
Simple Minimum, Maximum on numeric columns¶
t=> SELECT MIN("Age"), MAX("Age") FROM nba;
min | max
----+----
19 | 40
Minimum and maximum on text columns¶
t=> SELECT MIN("Name"), MAX("Name") FROM nba;
min | max
-------------+--------------
Aaron Brooks | Zaza Pachulia
Combine MAX with GROUP BY¶
t=> SELECT "Team", MAX("Salary") FROM nba GROUP BY 1 ORDER BY 2 DESC LIMIT 5;
Team | max
--------------------+---------
Los Angeles Lakers | 25000000
Cleveland Cavaliers | 22970500
New York Knicks | 22875000
Houston Rockets | 22359364
Miami Heat | 22192730