MIN¶
Returns the minimum value from all non-NULL values in a group.
Syntax¶
Parameters¶
MIN(expression)Returns the smallest non-NULL value of
expressionin the group. NULL values are ignored.
Returns¶
Returns the minimum value with the same type as expression. If all values are NULL or the input set is empty, returns NULL.
Examples¶
Setup¶
For these examples, assume we have a sales table:
CREATE TABLE sales(
id BIGINT,
product STRING,
region STRING,
amount BIGINT,
PRIMARY KEY(id))
INSERT INTO sales VALUES
(1, 'Widget', 'North', 100),
(2, 'Widget', 'South', 150),
(3, 'Gadget', 'North', 200),
(4, 'Gadget', 'South', NULL),
(5, 'Widget', 'North', 120)
MIN - Minimum Value¶
Find the minimum amount in the table:
SELECT MIN(amount) AS min_amount FROM sales
|
|---|
|
Notice that the NULL value in row 4 is ignored.
MIN with GROUP BY¶
Find minimum amounts per product:
SELECT product, MIN(amount) AS min_amount
FROM sales
GROUP BY product
|
|
|---|---|
|
|
|
|
Find minimum amounts per region:
SELECT region, MIN(amount) AS min_amount
FROM sales
GROUP BY region
|
|
|---|---|
|
|
|
|
The South region minimum only considers the non-NULL value (150), ignoring the NULL from the Gadget sale.
Important Notes¶
Index Requirement: GROUP BY queries require an appropriate index to execute. See Indexes for details on creating indexes that support GROUP BY operations.