MIN Function
SQL · Aggregate Functions
Theory
MIN is the mirror of MAX: it returns the smallest value in a column across a set of rows. For numbers that is the lowest value, for dates the earliest date, and for text the value that sorts first alphabetically. NULL values are skipped, and MIN returns NULL only when there are no non-NULL values to compare.
With GROUP BY, MIN gives one result per group, such as the lowest salary in each department or the first order date of each customer (handy for finding when a customer joined). You can use MIN and MAX together to get a range, for example MAX(salary) - MIN(salary) per department. Text comparison depends on the database collation, so upper and lower case letters may sort differently than you expect.
Why we use it
Lowest price, earliest date, smallest stock level: MIN finds the floor of a set of values without sorting everything. Combined with MAX it gives you ranges, and with GROUP BY it gives you 'first time' facts per customer or product.
When to use it
Use MIN for the smallest number or the earliest date, for example a customer's first purchase. If you need the row behind the minimum, use ROW_NUMBER() or a join. For the minimum across columns in the same row, use LEAST() where available, not MIN.
How it works
- Rows arrive after FROM and WHERE
- GROUP BY splits rows into groups
- Start with no current minimum
- Is the next value NULL? Yes: Skip it. No: Keep it if it is smaller than the current minimum.
- Repeat for every row in the group
- Return the minimum (NULL if no values)
Syntax
MIN(column)Example
SELECT MIN(salary) FROM employees;Key points
- Works on numbers, dates and text (text = first alphabetically)
- Ignores NULLs; returns NULL only if no values exist
- Often paired with GROUP BY for per-group minimums
- MIN(order_date) per customer = their first purchase
- MIN works down rows; LEAST() compares columns within one row