Comparison Operators
Comparison operators are used to compare two values and return a boolean result (TRUE, FALSE, or NULL). They are commonly used in WHERE clauses, CASE expressions, and join conditions.
Supported Operators
Operator | Description | Example |
|---|---|---|
| Equal to | |
| Not equal to | |
| Less than | |
| Greater than | |
| Less than or equal to | |
| Greater than or equal to | |
| Not equal, treating NULL as a value (Databricks only) | |
| Equal, treating NULL as a value (Databricks only) | |
Syntax
expression1 { = | <> | != | < | > | <= | >= } expression2
expression1 IS [NOT] DISTINCT FROM expression2
Returns
Returns
TRUEif the comparison condition is satisfied.Returns
FALSEif the comparison condition is not satisfied.Returns
NULLif either operand isNULL(for standard operators).
NULL Handling
Standard comparison operators return
NULLwhen either operand isNULL.IS DISTINCT FROMandIS NOT DISTINCT FROMtreatNULLas a comparable value.
Examples
Basic Comparisons
-- Example 1: Equality > SELECT 1 = 1; true -- Example 2: IS NULL check > SELECT 1 IS NULL; false > SELECT 1 IS NOT NULL; true > SELECT NULL IS NULL; true
Timestamp Comparisons
-- Example 3: Timestamp equality
> SELECT TIMESTAMP '2005-09-27 12:00:00' = TIMESTAMP '2005-09-27 12:00:00';
true
-- Example 4: Timestamp less than
> SELECT TIMESTAMP '1957-10-04 19:28:00' < TIMESTAMP '1961-04-12 06:07:00';
true
-- Example 5: Timestamp with timezone
> SELECT TIMESTAMP WITH TIME ZONE '2013-07-22 16:24:00 EST'
< TIMESTAMP WITH TIME ZONE '2011-04-29 10:48:00 EST';
false
IS DISTINCT FROM (Databricks Only)
-- Example 6: DISTINCT handles NULL differently > SELECT 1 IS DISTINCT FROM 1; false > SELECT 1 IS DISTINCT FROM 0; true > SELECT 1 IS DISTINCT FROM NULL; true > SELECT NULL IS DISTINCT FROM NULL; false
String Comparisons (Case-Sensitive)
-- Example 7: String comparisons are case-sensitive > SELECT 'ab' = 'Ab'; false > SELECT 'ab' > 'Ab'; true > SELECT 'CaSe_SenSITive' = 'case_sensitive'; false
Engine Differences
IS DISTINCT FROM
Databricks: Fully supported
Vertica: Not supported (will throw error: “DistinctExpr not supported”)
NaN Comparisons
Databricks:
NaN = NaNreturnstrueVertica:
NaN = NaNreturnsfalse
See Also
IS Operator - Testing for NULL, TRUE, FALSE
BETWEEN Operator - Range comparisons
IN Operator - Set membership