Updated on 2024-12-19 GMT+08:00

Simple Expressions

Logical Expressions

Logical Operators lists the operators and calculation rules of logical expressions.

Comparative Expressions

Comparison Operators lists the common comparative operators.

In addition to comparative operators, you can also use the following sentence structure:

  • BETWEEN

    The operator BETWEEN...AND selects a data range between two values. These values can be numeric, text, or date.

    The expression a BETWEEN x AND y is equivalent to the expression a >= x AND a <= y.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    SELECT 2 BETWEEN 1 AND 3 AS RESULT;
     result 
    ----------
     t
    (1 row)
    
    SELECT 2 >= 1 AND 2 <= 3 AS RESULT;
     result 
    ----------
     t
    (1 row)
    

    a NOT BETWEEN x AND y is equivalent to a < x OR a > y.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    SELECT 2 NOT BETWEEN 1 AND 3 AS RESULT;
     result 
    ----------
     f
    (1 row)
    
    SELECT 2 < 1 OR 2 > 3 AS RESULT;
     result 
    ----------
     f
    (1 row)
    
  • To check whether a value is null, use:
    1
    2
    expression IS NULL
    expression IS NOT NULL
    
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    SELECT 2+2 IS NULL AS RESULT;
     result 
    ----------
     f
    (1 row)
    
    SELECT 2+2 IS NOT NULL AS RESULT;
     result 
    ----------
     t
    (1 row)
    

    or an equivalent (non-standard) sentence structure:

    1
    2
    expression   ISNULL
    expression  NOTNULL
    

    Do not write expression=NULL or expression<>(!=)NULL, because NULL represents an unknown value, and these expressions cannot determine whether two unknown values are equal.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    SELECT 2+2 ISNULL AS RESULT;
     result 
    ----------
     f
    (1 row)
    
    SELECT 2+2 NOTNULL AS RESULT;
     result 
    ----------
     t
    (1 row)
    
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    SELECT 2+2 IS DISTINCT FROM NULL AS RESULT;
     result 
    ----------
     t
    (1 row)
    
    SELECT 2+2 IS NOT DISTINCT FROM NULL AS RESULT;
     result  
    ----------
     f
    (1 row)