MySQL Min and Max
MIN() and MAX() return the smallest and largest values found in a column, letting you quickly answer questions about extremes in your data.
The MIN() Function
MIN() returns the smallest value in a selected numeric, string, or date column. When applied to a string column, MIN() returns the value that would sort first alphabetically; when applied to a date column, it returns the earliest date.
Find the cheapest product price
SELECT MIN(Price) AS LowestPrice
FROM Products;The MAX() Function
MAX() works exactly like MIN() but in the opposite direction, returning the largest value found in the specified column — useful for finding the highest price, the most recent date, or the largest order quantity.
Find the most expensive product price
SELECT MAX(Price) AS HighestPrice
FROM Products;Giving the Result a Readable Alias
Because MIN() and MAX() return a computed column rather than a named table column, it is good practice to use AS to alias the result, so that the output is self-explanatory rather than showing a generic column name like 'MIN(Price)'.
- MIN() and MAX() ignore NULL values automatically
- Both functions can operate on numbers, strings, and dates
- Aliasing the result with AS makes reports far easier to read
- MIN()/MAX() can be combined with GROUP BY to find extremes per group
Combining MIN() and MAX() With GROUP BY
Just like other aggregate functions, MIN() and MAX() become significantly more useful when paired with GROUP BY, letting you find, for example, the cheapest product offered by each individual supplier rather than across the whole catalog.
Find the cheapest product from each supplier
SELECT SupplierID, MIN(Price) AS CheapestFromSupplier
FROM Products
GROUP BY SupplierID;