SQL MIN and MAX Functions

The SQL MIN and MAX functions provide a means to return the minimum and maximum values of a particular column in a table. Both are formatted similarly, with the column name specified in brackets after the function name. Note that the ‘where’ clause can be omitted if the result required covers all the data in the table.

SELECT MIN(column_name)
FROM table_name
WHERE condition;
SELECT MAX(column_name)
FROM table_name
WHERE condition;

Using the ‘person’ table from previous examples, these functions can be used to find the minimum and maximum date of birth.

id firstname lastname title dob
1 Fred Bloggs Mr 1980-05-05
2 Simon Smith Mr 1960-04-01
3 Freida Bloggs Mrs 1970-10-12
4 Fiona Jones Miss 1985-05-19
SELECT MIN(dob)
FROM person;
SELECT MAX(dob)
FROM person;

In order to give the returned values a meaningful label or heading, column aliases can be used, for example, ‘MinDoB’ and ‘MaxDoB’.

SELECT MIN(dob) AS "MinDoB"
FROM person;
SELECT MAX(dob) AS "MaxDoB" 
FROM person;

If column aliases are not used, the label for each would be ‘MIN(dob)’ and ‘MAX(dob) respectively.

It should be noted, that if it is required to find both the minimum and maximum values of a particular column, then the above two queries can be combined.

SELECT MIN(dob) AS "MinDoB", 
       MAX(dob) AS "MaxDoB"
FROM person;