MySQL: minimum and maximum values in a row

How can you select the minimum value in a row of data? This is quite tricky question for MS SQL but not for MySQL.
There are functions LEAST() and GREATEST() to get minimum and maximum value in a row.

For example a SQL query like below returns minimum of those three dates.

SELECT LEAST(DateCreated, DateChanged, DateUploaded)
FROM UserPhoto;

The same is for maximum:

SELECT GREATEST(DateCreated, DateChanged, DateUploaded)
FROM UserPhoto;

Draw attention that LEAST() (or GREATEST()) returns NULL if at least one input parameter is NULL. Thus one of possible workarounds is using of function IFNULL().for each input parameter.

No Comment

No comments yet

Leave a reply