Comparisons

Comparison Operators

Comparison operators compare two values and return a boolean result (true or false). They are commonly used in WHERE clauses and join conditions.

OperatorDescription
=Equal to
<>, !=Not equal to
<Less than
<=Less than or equal to
>Greater than
>=Greater than or equal to
IS DISTINCT FROMNot equal, treating NULL values as comparable values
IS NOT DISTINCT FROMEqual, treating NULL values as comparable values

Basic Comparisons

Check if a value is equal:

SELECT * FROM cities WHERE state_code = 'CA';

Check if a value is not equal:

SELECT * FROM products WHERE category <> 'Electronics';

Check if a value is less than 100:

SELECT * FROM orders WHERE amount < 100;

Check if a value is less than or equal to the given date:

SELECT * FROM employees WHERE hire_date <= '2020-01-01';

Check if the value is greater than 50:

SELECT * FROM products WHERE price > 50;

Check if the value is greater than or equal to 90:

SELECT * FROM students WHERE score >= 90;

IS [NOT] DISTINCT FROM

These operators are similar to the = and <> operators, but they treat NULL values as comparable values.

Regular comparison with NULL returns NULL (unknown):

SELECT 1 = NULL;   -- Result: NULL
SELECT NULL = NULL;   -- Result: NULL

DISTINCT FROM treats NULLs as comparable:

SELECT 1 IS DISTINCT FROM NULL;   -- Result: true
SELECT NULL IS DISTINCT FROM NULL;   -- Result: false

NOT DISTINCT FROM also treats NULLs as comparable:

SELECT 1 IS NOT DISTINCT FROM NULL;   -- Result: false
SELECT NULL IS NOT DISTINCT FROM NULL;   -- Result: true

IS Predicates

IS predicates check if an expression is or is not a given value, returning true or false.

PredicateDescription
IS TRUECheck if an expression is true. NULL if the input expression is NULL.
IS NOT TRUECheck if an expression is not true. NULL if the input expression is NULL.
IS FALSECheck if an expression is false. NULL if the input expression is NULL.
IS NOT FALSECheck if an expression is not false. NULL if the input expression is NULL.
IS NULLReturns true if the expression is NULL, false otherwise.
IS NOT NULLReturns true if the expression is not NULL, false otherwise.

Examples

Is boolean:

SELECT true IS TRUE; -- Returns true
SELECT NULL IS TRUE; -- Returns NULL

Is NULL:

SELECT NULL IS NULL; -- Returns true
SELECT 4 IS NULL; -- Returns false

BETWEEN Predicates

BETWEEN predicates check if an expression is between or not between a lower and upper bound.

PredicateDescription
expression BETWEEN lower AND upperCheck if expression is contained by lower and upper.
expression NOT BETWEEN lower AND upperCheck if expression is not contained by lower and upper.

Examples

Check if number is in range:

SELECT 4 BETWEEN 2 AND 8; -- Returns true
SELECT 2 BETWEEN 2 AND 8; -- Returns true, expression is >= lower bound
SELECT 8 BETWEEN 2 AND 8; -- Returns true, expression is <= upper bound
SELECT 10 BETWEEN 2 AND 8; -- Returns false

Check if number is not in range:

SELECT 4 NOT BETWEEN 2 AND 8; -- Returns false
SELECT 2 NOT BETWEEN 2 AND 8; -- Returns false, expression is >= lower bound
SELECT 8 NOT BETWEEN 2 AND 8; -- Returns false, expression is <= upper bound
SELECT 10 NOT BETWEEN 2 AND 8; -- Returns true