Knowledge Walls
John Peter
Pune, Maharashtra, India
GREATEST() and LEAST() Method in Mysql with Example
2043 Views
GREATEST vs LEAST 
GREATEST method returns greatest number from list of given numbers.
GREATEST(value 1,value 2,....,value n);

LEAST method returns least number from list of given numbers.
LEAST(value 1,value 2,....,value n);

Example
GREATEST(29,38,10,45); //Returns 45
LEAST(4,5,3,28); //Returns 3
Example
-- Returns 28
SELECT GREATEST(2,3,4,1,28);

-- Returns NULL if list contains NULL
SELECT GREATEST(3,6,8,NULL,2);

-- Returns 3
SELECT LEAST(6,3,7,4);

-- Returns NULL if list contains NULL
SELECT LEAST(6,3,NULL,2,1);
Hints 
If any one of the item value is NULL then GREATEST and LEAST methods are returning NULL.
Best Lessons of "MySQL"
Top lessons which are viewed more times.
  Copyright © 2014 Knowledge walls, All rights reserved
KnowledgeWalls
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details