MIN / MAX
Return the smallest or largest value in a group, including for text and dates.
Syntax
MIN(expression)
MAX(expression)What it does
MIN() returns the smallest value in a group and MAX() the largest. Both ignore NULL values, and both work on numbers, dates, and text (text compares lexicographically by the column collation).
Example
SELECT
customer_id,
MIN(order_date) AS first_order,
MAX(order_date) AS last_order,
MAX(amount) AS largest_order
FROM orders
GROUP BY customer_id;Important behavior
NULLvalues are ignored; if every value isNULL, the result isNULLMAXon avarcharorders'10'before'9'— cast to a number when the column holds numeric textMIN/MAXdo not tell you which row the value came from; useROW_NUMBER()or a correlated filter for that
Getting the row that owns the extreme value
SELECT customer_id, order_id, amount
FROM (
SELECT
customer_id,
order_id,
amount,
ROW_NUMBER() OVER (
PARTITION BY customer_id
ORDER BY amount DESC, order_id DESC
) AS rn
FROM orders
) ranked
WHERE rn = 1;Safety checklist
- confirm the column type sorts the way you expect (numeric vs text vs date)
- decide whether a group of all-
NULLvalues should yieldNULLor a fallback - if you need the full row, rank it — do not assume other columns in the
SELECTline up with theMAX