Technical information on this site may be out of date : no updates since 2015

SQL Greatest and Least

July 21 2009 : posted under postgresql mysql sql

Most of the time I can get what I want out of SQL using the standard aggregate functions, but today I needed to find the latest timestamp from any one of three columns.

I achieved this using the GREATEST() function, it’s not part of the SQL standard but is commonly available.

GREATEST(<em>value</em> [<span class="OPTIONAL">, ...</span>])

LEAST(<em>value</em> [<span class="OPTIONAL">, ...</span>])

The GREATEST and LEAST functions select the largest or smallest value from a list of any number of expressions.