Recipe 8.2. Summarizing with MIN( ) and MAX( )ProblemYou need to determine the smallest or largest of a set of values. SolutionUse MIN( ) to find the smallest value, MAX( ) to find the largest. DiscussionFinding smallest or largest values is somewhat akin to sorting, except that instead of producing an entire set of sorted values, you select only a single value at one end or the other of the sorted range. This kind of operation applies to questions about smallest, largest, oldest, newest, most expensive, least expensive, and so forth. One way to find such values is to use the MIN( ) and MAX( ) functions. (Another way to address these questions is to use LIMIT; see the discussions in Recipes Section 3.14 and Section 3.16.) Because MIN( ) and MAX( ) determine the extreme values in a set, they're useful for characterizing ranges:
MIN( ) and MAX( ) need not be applied directly to column values. They also work with expressions or values that are derived from column values. For example, to find the lengths of the shortest and longest state names, do this: mysql> SELECT -> MIN(CHAR_LENGTH(name)) AS shortest, -> MAX(CHAR_LENGTH(name)) AS longest -> FROM states; +----------+---------+ | shortest | longest | +----------+---------+ | 4 | 14 | +----------+---------+ |