How can I compare values with SQL?

If actual performance exceeds the guaranteed minimum, payment should be calculated on the basis of that minimum.

Yet, can I compare those two values, directly in my query?

Sure! Check out examples below:

-----------------------------Vertica--------------------------------------------------------------------
select
greatest(5.5, 8.2, -8.2) as HighestValue, -- returns the higer number, taking the sign into account
least(5.5, 8.2, -8.2) as LowestValue -- returns the lower number, taking the sign into account
-----------------------------MySQL----------------------------------------------------------------------
select
greatest(5.5, 8.2, -8.2) as HighestValue, -- returns the higer number, taking the sign into account
least(5.5, 8.2, -8.2) as LowestValue -- returns the lower number, taking the sign into account
HighestValue |LowestValue |
-------------|------------|
8.2 |-8.2 |
-----------------------------PostgreSQL--------------------------------------------------------------------
select
greatest(5.5, 8.2, -8.2) as HighestValue, -- returns the higer number, taking the sign into account
least(5.5, 8.2, -8.2) as LowestValue -- returns the lower number, taking the sign into account
highestvalue |lowestvalue |
-------------|------------|
8.2 |-8.2 |
----------------------------SQL Server---------------------------------------------------------------------
select
max(MyValue) as HighestValue, -- returns the higer number, taking the sign into account
min(MyValue) as LowestValue -- returns the lower number, taking the sign into account
from (values (5.5), (8.2), (-8.2)) as a(MyValue)
HighestValue |LowestValue |
-------------|------------|
8.2 |-8.2 |

In a nutshell, greatest() and least() make very neat SQL functions as they:

  • can compare more than 2 values
  • have the same syntax in most DBs (excepton being SQL Server where they do not have direct equvalents)

Happy SQL-ing!