Comparison Functions
Overview
Comparison functions are used to compare two expressions and return a result of the BOOLEAN type. AI DataLake supports a complete set of comparison operations defined in the SQL standard, including equality comparison, magnitude comparison, range comparison, pattern matching, and null value judgment.
For more information, see Comparison Functions.
Application Scenarios
- WHERE filtering: Uses comparison conditions to filter data rows in a query.
- JOIN: Uses equality comparison in JOIN conditions to associate different tables.
- Conditional branching: Uses the comparison result in CASE WHEN to control business logic.
- Data verification: Uses comparison operations to check the validity and consistency of data.
Notes and Constraints
- The data types of the two ends of a comparison operation must be compatible. Otherwise, an explicit CAST conversion is required.
- The result of comparing NULL with any value is NULL (not TRUE or FALSE). Use IS NULL to determine the result.
- BETWEEN is a closed interval, including the boundary values at both ends. That is, value1 BETWEEN value2 AND value3 is equivalent to value1 >= value2 AND value1 <= value3.
- In LIKE, the percent sign (%) matches any sequence of characters (including an empty sequence), and the underscore (_) matches exactly one character. To match a wildcard character itself, use the ESCAPE clause for escaping.
If the IN list contains NULL and value1 is not in the non-NULL values of the list, the result is NULL instead of FALSE.
Syntax Structure
| Syntax | Return Type | Description | Example |
|---|---|---|---|
| value1 = value2 | BOOLEAN | Equality comparison. If value1 is equal to value2, TRUE is returned. Otherwise, FALSE is returned. The types of value1 and value2 must be compatible. If either operand is NULL, NULL is returned. | SELECT 1 = 1; -- TRUE |
| value1 <> value2 | BOOLEAN | Inequality comparison. If value1 is not equal to value2, TRUE is returned. Otherwise, FALSE is returned. The types of value1 and value2 must be compatible. If either operand is NULL, NULL is returned. | SELECT 1 <> 2; -- TRUE |
| value1 != value2 | BOOLEAN | Inequality comparison. It is semantically equivalent to <>. If value1 is not equal to value2, TRUE is returned. If either operand is NULL, NULL is returned. | SELECT 1 != 2; -- TRUE |
| value1 > value2 | BOOLEAN | Greater than comparison. If value1 is greater than value2, TRUE is returned. Otherwise, FALSE is returned. The types of value1 and value2 must be compatible and sortable. If either operand is NULL, NULL is returned. | SELECT 3 > 2; -- TRUE |
| value1 >= value2 | BOOLEAN | Greater than or equal to comparison. If value1 is greater than or equal to value2, TRUE is returned. Otherwise, FALSE is returned. The types of value1 and value2 must be compatible and sortable. If either operand is NULL, NULL is returned. | SELECT 2 >= 2; -- TRUE |
| value1 < value2 | BOOLEAN | Less than comparison. If value1 is less than value2, TRUE is returned. Otherwise, FALSE is returned. The types of value1 and value2 must be compatible and sortable. If either operand is NULL, NULL is returned. | SELECT 1 < 2; -- TRUE |
| value1 <= value2 | BOOLEAN | Less than or equal to comparison. If value1 is less than or equal to value2, TRUE is returned. Otherwise, FALSE is returned. The types of value1 and value2 must be compatible and sortable. If either operand is NULL, NULL is returned. | SELECT 2 <= 3; -- TRUE |
| value IS NULL | BOOLEAN | Determines whether a value is NULL. If value is NULL, TRUE is returned. Otherwise, FALSE is returned. This function is not affected by the rule that NULL is returned when NULL is involved in a common comparison. It is the standard method for determining whether a value is NULL. | SELECT NULL IS NULL; -- TRUE |
| value IS NOT NULL | BOOLEAN | Determines whether a value is not NULL. If value is not NULL, TRUE is returned. Otherwise, FALSE is returned. This is the logical negation of IS NULL. | SELECT 1 IS NOT NULL; -- TRUE |
| value1 BETWEEN value2 AND value3 | BOOLEAN | Range judgment (closed interval). If value1 is within the closed interval [value2, value3], TRUE is returned, which is equivalent to value1 >= value2 AND value1 <= value3, including both boundary values. value2 must be less than or equal to value3. Otherwise, the result is always FALSE. If any operand is NULL, NULL is returned. | SELECT 2 BETWEEN 1 AND 3; -- TRUE |
| value1 NOT BETWEEN value2 AND value3 | BOOLEAN | Out-of-range judgment. If value1 is not within the closed interval [value2, value3], TRUE is returned, which is equivalent to value1 < value2 OR value1 > value3. This is the logical negation of BETWEEN. If any operand is NULL, NULL is returned. | SELECT 5 NOT BETWEEN 1 AND 3; -- TRUE |
| value1 LIKE value2 | BOOLEAN | Pattern matching. Returns TRUE if value1 matches pattern value2. value1 is the string to be matched, and value2 is the pattern string. The percent sign (%) matches any sequence of characters (including an empty sequence), and the underscore (_) matches exactly one character. To match a percent sign or underscore itself, use the ESCAPE clause to define an escape character. If any operand is NULL, NULL is returned. | SELECT 'hello' LIKE 'h%'; -- TRUE |
| value1 NOT LIKE value2 | BOOLEAN | Returns TRUE if value1 does not match pattern value2. The wildcard rules are the same as those for LIKE. This is the logical negation of LIKE. If any operand is NULL, NULL is returned. | SELECT 'hello' NOT LIKE 'a%'; -- TRUE |
| value1 IN (value2[, value3]*) | BOOLEAN | Determines whether a value is in a list. Returns TRUE if value1 is equal to any value in the list. The type of value1 must be compatible with the type of values in the list. If value1 is not in the list and the list contains NULL, NULL is returned instead of FALSE. If value1 is NULL, NULL is returned. | SELECT 2 IN (1, 2, 3); -- TRUE |
| value1 NOT IN (value2[, value3]*) | BOOLEAN | Determines whether a value is not in a list. If value1 is not equal to any value in the list, TRUE is returned. This is the logical negation of IN. If the list contains NULL and value1 is not in the list of non-null values, NULL is returned instead of TRUE. If value1 is NULL, NULL is returned. | SELECT 5 NOT IN (1, 2, 3); -- TRUE |
| value1 IS DISTINCT FROM value2 | BOOLEAN | Determines whether values are different (NULL-safe). If the values of value1 and value2 are different, TRUE is returned. The key difference from <> is the NULL handling: NULL IS DISTINCT FROM NULL returns FALSE, and NULL IS DISTINCT FROM a non-null value returns TRUE. NULL results are not generated. This is used in scenarios where NULLs need to be treated as comparable values. | SELECT 1 IS DISTINCT FROM 2; -- TRUE |
| value1 IS NOT DISTINCT FROM value2 | BOOLEAN | Determines whether values are the same (NULL-safe). If the values of value1 and value2 are the same, TRUE is returned. The key difference from = is the NULL handling: NULL IS NOT DISTINCT FROM NULL returns TRUE. NULL results are not generated. This is the logical negation of IS DISTINCT FROM. It is used in scenarios where NULLs need to be treated as equal values. | SELECT NULL IS NOT DISTINCT FROM NULL; -- TRUE |
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot