I need to get the biggest value from two fields:
SELECT MAX(field1), MAX(field2)
Now, how can I get biggest value from these two?
Use of GREATEST/LEAST with MIN/MAX
GREATEST/LEAST: used with the columns, when you want to find the max or min value from the various columns.
MIN/MAX: used with the rows, when you want to find the max or min value from the various rows:
Example table:
SELECT GREATEST(col_a, col_b, col_c) FROM temp;
SELECT MIN(GREATEST(col_a, col_b, col_c)) FROM temp; # 3 as output
SELECT MAX(GREATEST(col_a, col_b, col_c)) FROM temp; # 9 as output
SELECT LEAST(col_a, col_b, col_c) FROM temp;
SELECT MIN(LEAST(col_a, col_b, col_c)) FROM temp; # 1 as output
SELECT MAX(LEAST(col_a, col_b, col_c)) FROM temp; # 7 as output