8.2. Comparison Functions and Operators

    Range Operator: BETWEEN

    The statement shown above is equivalent to the following statement:

    1. SELECT 3 >= 2 AND 3 <= 6;

    To test if a value does not fall within the specified range use NOT BETWEEN:

    1. SELECT 3 NOT BETWEEN 2 AND 6;

    The statement shown above is equivalent to the following statement:

    1. SELECT 3 < 2 OR 3 > 6;

    The presence of NULL in a BETWEEN or NOT BETWEEN statement will result in the statement evaluating to NULL:

    The BETWEEN and NOT BETWEEN operators can also be used to evaluate string arguments:

    1. SELECT 'Paul' BETWEEN 'John' AND 'Ringo'; -- true

    Not that the value, min, and max parameters to BETWEEN and NOT BETWEEN must be the same type. For example, Presto will produce an error if you ask it if John is between 2.3 and 35.2.

    The IS NULL and IS NOT NULL operators test whether a value is null (undefined). Both operators work for all data types.

    Using NULL with IS NULL evaluates to true:

    1. select NULL IS NULL; -- true
    1. SELECT 3.0 IS NULL; -- false

    IS DISTINCT FROM and IS NOT DISTINCT FROM

    In SQL a NULL value signifies an unknown value, so any comparison involving a NULL will produce NULL. The IS DISTINCT FROM and IS NOT DISTINCT FROM operators treat NULL as a known value and both operators guarantee either a true or false outcome even in the presence of NULL input:

    In the example shown above, a NULL value is not considered distinct from NULL. When you are comparing values which may include NULL use these operators to guarantee either a or FALSE result.

    The following truth table demonstrate the handling of NULL in IS DISTINCT FROM and IS NOT DISTINCT FROM:

    These functions are not in the SQL standard, but are a common extension. Like most other functions in Presto, they return null if any argument is null. Note that in some other databases, such as PostgreSQL, they only return null if all arguments are null.

    The following types are supported: DOUBLE, BIGINT, VARCHAR, TIMESTAMP, TIMESTAMP WITH TIME ZONE, DATE

    greatest(value1, value2, , valueN) → [same as input]

    Returns the largest of the provided values.

    (value1, value2, , valueN) → [same as input]

    Quantified Comparison Predicates: ALL, ANY and SOME

    The ALL, ANY and SOME quantifiers can be used together with comparison operators in the following way:

      For example:

      1. SELECT 'hello' = ANY (VALUES 'hello', 'world'); -- true
      2. SELECT 21 < ALL (VALUES 19, 20, 21); -- false
      3. SELECT 42 >= SOME (SELECT 41 UNION ALL SELECT 42 UNION ALL SELECT 43); -- true

      Here are the meanings of some quantifier and comparison operator combinations:

      ANY and SOME have the same meaning and can be used interchangeably.

      The LIKE operator is used to match a specified character pattern in a string. Patterns can contain regular characters as well as wildcards. Wildcard characters can be escaped using the single character specified for the ESCAPE parameter. Matching is case sensitive.

      Syntax:

      1. expression LIKE pattern [ ESCAPE 'escape_character' ]

      if pattern or escape_character is null, the expression evaluates to null.

      Examples: