How to get the max of two values in MySQL?

Mask picture Mask · Oct 14, 2009 · Viewed 125.7k times · Source

I tried but failed:

mysql> select max(1,0);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual 
that corresponds to your MySQL server version for the right syntax to use 
near '0)' at line 1

Answer

NinethSense picture NinethSense · Oct 14, 2009

Use GREATEST()

E.g.:

SELECT GREATEST(2,1);

Note: Whenever if any single value contains null at that time this function always returns null (Thanks to user @sanghavi7)