Updated on 2026-11-06 GMT+08:00

Relational Operators

All basic data types can be compared using relational operators, which return a value of type BOOLEAN.

Relational operators are all binary operators. The two data types being compared must be the same data type or types that can be implicitly converted.

Table 1 Relational operators

Operator

Return Type

Description

A = B

BOOLEAN

Returns TRUE if A equals B. Otherwise returns FALSE.

A == B

BOOLEAN

Returns TRUE if A equals B. Otherwise returns FALSE. Cannot be used for assignment operations.

A <=> B

BOOLEAN

Returns TRUE if A equals B. Otherwise returns FALSE. Returns TRUE if both A and B are NULL. Returns FALSE if one of them is NULL.

A <> B

BOOLEAN

Returns TRUE if A is not equal to B. Otherwise returns FALSE. Returns NULL if A or B is NULL. Standard SQL syntax.

A != B

BOOLEAN

Same as the <> logical operator. SQL Server syntax.

A < B

BOOLEAN

Returns TRUE if A is less than B. Otherwise returns FALSE. Returns NULL if A or B is NULL.

A <= B

BOOLEAN

Returns TRUE if A is less than or equal to B. Otherwise returns FALSE. Returns NULL if A or B is NULL.

A > B

BOOLEAN

Returns TRUE if A is greater than B. Otherwise returns FALSE. Returns NULL if A or B is NULL.

A >= B

BOOLEAN

Returns TRUE if A is greater than or equal to B. Otherwise returns FALSE. Returns NULL if A or B is NULL.

A BETWEEN B AND C

BOOLEAN

Returns TRUE if A is greater than or equal to B and less than or equal to C. Otherwise returns FALSE. Returns NULL if any of A, B, or C is NULL.

A NOT BETWEEN B AND C

BOOLEAN

Returns TRUE if A is less than B or greater than C. Otherwise returns FALSE. Returns NULL if any of A, B, or C is NULL.

A IS NULL

BOOLEAN

Returns TRUE if A is NULL. Otherwise returns FALSE.

A IS NOT NULL

BOOLEAN

Returns TRUE if A is not NULL. Otherwise returns FALSE.

A LIKE B

BOOLEAN

Returns TRUE if string A matches string B. Otherwise returns FALSE. Returns NULL if A or B is NULL.

A NOT LIKE B

BOOLEAN

Returns TRUE if string A does not match string B. Otherwise returns FALSE. Returns NULL if A or B is NULL.

A RLIKE B

BOOLEAN

Java regular expression matching. Returns TRUE if A or any of its substrings matches B. Otherwise returns FALSE. Returns NULL if A or B is NULL.

A REGEXP B

BOOLEAN

Same result as A RLIKE B.

Precautions

  • Both = and == are equality comparison operators with the same functionality. = is the standard SQL syntax, while == is a compatible alternative. Neither is used for assignment.
  • <=> is a NULL-safe equality operator. Returns TRUE when both sides are NULL.
  • <> is the standard SQL not-equal operator, while != is SQL Server syntax. Both have the same logic.
  • In ANSI mode (enabled by default in Spark 4.0), comparisons involving NULL return NULL rather than being silently handled.

Example 1: Equality Comparison

SELECT 1 = 1;       -- TRUE
SELECT 1 == 2;      -- FALSE
SELECT 1 <=> 1;     -- TRUE
SELECT NULL <=> NULL; -- TRUE

Example 2: Inequality Comparison

SELECT 1 <> 2;      -- TRUE
SELECT 1 != 1;      -- FALSE
SELECT 1 < 2;       -- TRUE
SELECT 2 >= 2;      -- TRUE

Example 3: Range Comparison and NULL Checking

SELECT 2 BETWEEN 1 AND 3;    -- TRUE
SELECT 5 NOT BETWEEN 1 AND 3; -- TRUE
SELECT NULL IS NULL;          -- TRUE
SELECT 1 IS NOT NULL;         -- TRUE

Example 4: Fuzzy Matching

SELECT 'hello' LIKE 'h%';     -- TRUE
SELECT 'hello' NOT LIKE 'a%';  -- TRUE
SELECT 'hello' RLIKE 'h.*o';   -- TRUE
SELECT 'hello' REGEXP 'h.*o';  -- TRUE