Skip to main content

MIN

Functions › Aggregate

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

Syntax​

MIN( [ 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 'B' sorts before 'a' and '10' before '9'.
  • To compare values within one row, use LEAST instead.
  • Also a window function with OVER (...). See OVER.
  • On large tables MIN runs in the data store.

Examples​

The first and last order of each customer:

SELECT o.customer, MIN(o.placed_at) AS first_order, MAX(o.placed_at) AS last_order, MIN(o.amount) AS smallest
FROM Order o
GROUP BY o.customer
customerfirst_orderlast_ordersmallest
C-12026-01-042026-10-1240
C-22026-03-182026-03-18125

Not supported​

RefusedWrite instead
MIN(MIN(x)) and other nested aggregatesAggregate in a derived table, then aggregate again outside
-- Refused by the engine
SELECT MIN(MAX(o.amount)) AS m FROM Order o

See also​

MAX · COUNT · LEAST · GROUP BY · OVER