SQLite MIN() and MAX() Functions
The MIN() Function
MIN() returns the smallest value of the selected column.
This example finds the lowest price in the Products table:
The MAX() Function
MAX() returns the largest value. This example finds the highest price:
Name the Result Column
The result column gets a name like "MIN(Price)". Use AS to give it a better name:
Get the Whole Record
SQLite has a useful special rule: when a query uses only one MIN() or MAX(), the other columns come from the row with that value. This example shows the name of the most expensive product:
Note: This rule is special for SQLite. Other databases give an error for this query.
MIN() and MAX() for Each Group
With GROUP BY, you get the lowest price in each category:
Example
SELECT CategoryID, MIN(Price) AS SmallestPrice
FROM Products
GROUP BY CategoryID;
Try it Yourself »
MAX() With More Than One Value
With two or more arguments, MAX() returns the largest of the arguments, and MIN() the smallest: