SQL: MIN() reference
19 March 2023 (Updated 19 March 2023)
MIN()
is an aggregate function that lets you find the minimum value in a set.
Syntax
MIN(expression)
Example & execution plan
select min(salary) from salaries;
SQL will execute the above query in the following steps:
- Read through the
salaries
table and extract the value from thesalary
column for each row. - For each value in the
salary
column, compare it to a running minimum value. If the current value is lower than the running minimum value, update the running minimum value to the current value. - After running through all the rows, return the running minimum value as the result of
MIN()
.
Other notes
The data type determines the comparison algorithm
If you pass a set of INT
values to MIN()
, it will return the smallest number. If you pass it a set of DATE
values, it will return the earliest date. And so on,.
Sources
Tagged:
MySQL
Thanks for your comment 🙏. Once it's approved, it will appear here.
Leave a comment