Skip to main content

MAX

Functions › Aggregate

Returns the largest non-NULL value of an expression over each group. Works on numbers, text and dates.

Syntax​

MAX( [ ALL ] <expression> )

Arguments​

ArgumentDescription
<expression>A column or an expression of any comparable type.

Returns​

The same type as <expression>. NULL when the group has no non-NULL value.

Usage notes​

  • NULLs are skipped. An empty text value is treated as NULL.
  • Text compares character by character, so 'a' sorts after 'B' and '9' after '10'.
  • To compare values within one row, use GREATEST instead.
  • MAX(CASE WHEN <condition> THEN 1 ELSE 0 END) = 1 tests whether any row of the group meets a condition.
  • Also a window function with OVER (...). See OVER.
  • On large tables MAX runs in the data store.

Examples​

The latest update per ticket status, and whether any ticket in it is high priority:

SELECT t.status,
MAX(t.updated_at) AS last_update,
MAX(CASE WHEN t.priority = 'high' THEN 1 ELSE 0 END) = 1 AS any_high
FROM Ticket t
GROUP BY t.status
statuslast_updateany_high
open2026-11-07true
resolved2026-11-06false

The largest order of each customer next to every order:

SELECT o.key, o.customer, o.amount,
MAX(o.amount) OVER (PARTITION BY o.customer) AS customer_max
FROM Order o
keycustomeramountcustomer_max
O-10C-1100250
O-14C-1250250
O-11C-28080

Not supported​

RefusedWrite instead
bool_or(cond)MAX(CASE WHEN cond THEN 1 ELSE 0 END) = 1
-- Refused by the engine
SELECT t.status, BOOL_OR(t.priority = 'high') AS any_high FROM Ticket t GROUP BY t.status

See also​

MIN · COUNT · GREATEST · GROUP BY · OVER