Comparison operators and functions in DuckDB
Use comparison operators to test whether two values are equal, or whether one value sorts before or after another. Use the greatest and least functions to find the largest or the smallest of several values. Each operator returns a BOOLEAN value. You can compare numbers, strings, Boolean values, dates, times, timestamps, intervals, lists, structs, and maps. Both values must have the same type, or types that DuckDB can convert to a common type, such as INTEGER and DOUBLE.
If either value is NULL, the operators =, <>, !=, <, <=, >, and >= return NULL, so a WHERE clause leaves the row out. IS DISTINCT FROM and IS NOT DISTINCT FROM handle NULL like any other value: two NULL values are the same, and the result is never NULL. The greatest and least functions ignore NULL arguments.
When one value is a string literal, DuckDB converts the literal to the type of the other value. For example, when order_date is a DATE column, order_date > '2026-10-01' compares two dates. For <, <=, >, and >=, a VARCHAR column or expression is not converted. Comparing it with a number or a date fails unless you cast it first, as in CAST(. You can't chain comparisons such as s AS INTEGER) > 100. Combine them with a < b < cAND, or use BETWEEN for a range test.
The following table lists the comparison operators and functions that this topic describes.
| Function or operator | Description |
|---|---|
Tests whether the first value is less than the second. |
|
Tests whether the first value is less than or equal to the second. |
|
Tests whether two values are not equal. |
|
Tests whether two values are equal. |
|
Tests whether the first value is greater than the second. |
|
Tests whether the first value is greater than or equal to the second. |
|
Returns the largest of its arguments, ignoring |
|
Tests whether two values are different ( |
|
Returns the smallest of its arguments, ignoring |
< (less than) operator
Returns true if expression1 sorts before expression2, and false if it does not. If either value is NULL, the result is NULL.
Numbers compare by value, and dates, times, and timestamps compare in time order. For Boolean values, false sorts before true. Strings compare character by character, by Unicode code point, so the comparison is case-sensitive. Every uppercase ASCII letter sorts before every lowercase letter, and a string sorts before any longer string that starts with it.
Lists compare element by element, starting with the first element, and the first pair of elements that differ decides the result. When one list matches the start of a longer list, the shorter list is less. Structs compare field by field in the same way.
Syntax
expression1 < expression2
Arguments
The < (less than) operator takes the following arguments:
expression1-
The first value to compare, of any type that has an order, for example
INTEGER,VARCHAR,DATE,TIMESTAMP, a list, or aSTRUCT. expression2-
The second value to compare. It must have the same type as
expression1, or a type that DuckDB can convert to a common type with it.
Return type
BOOLEAN
Examples
The following SQL query tests whether one number is less than another.
SELECT 1 < 2 AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shows that an uppercase letter sorts before a lowercase letter, and that a string sorts before a longer string that starts with it.
SELECT 'B' < 'a' AS uppercase_first, 'ab' < 'abc' AS shorter_first
The query returns the following result.
| uppercase_first | shorter_first |
|---|---|
|
|
The following SQL query compares lists. The first elements that differ decide the result, and a list that matches the start of a longer list is less than the longer list.
SELECT [1, 2] < [1, 3] AS second_element_decides, [1, 2] < [1, 2, 0] AS prefix_first, [2] < [1, 9] AS first_element_decides
The query returns the following result.
| second_element_decides | prefix_first | first_element_decides |
|---|---|---|
|
|
|
The following SQL query compares 10 with 9 in three ways: as two strings, as two numbers after a cast, and as a number with a string literal. As text, '10' sorts before '9', so the first result is true. After the cast to INTEGER, 10 is not less than 9, so the second result is false. In the third comparison, DuckDB converts the string literal '9' to a number, so that result is also false.
SELECT s < '9' AS as_strings, CAST(s AS INTEGER) < 9 AS as_numbers, 10 < '9' AS number_and_literal FROM (VALUES ('10')) AS v(s)
The query returns the following result.
| as_strings | as_numbers | number_and_literal |
|---|---|---|
|
|
|
Considerations
DuckDB converts a string literal to the type of the other value, so 10 < '9' compares two numbers and returns false. It does not convert a VARCHAR column or expression in an ordering comparison. Comparing a VARCHAR value with a number or a date by using <, <=, >, or >= fails. Cast the VARCHAR value first, as in CAST(. Two s AS INTEGER) < 9VARCHAR values compare as text, so '10' < '9' returns true.
You can't chain comparisons, as in 1 < 2 < 3. Combine them with AND, as in , or use a < b AND b < cBETWEEN for a range test.
To compare strings without regard to case, compare lower( with a)lower(.b)
<= (less than or equal) operator
Returns true if expression1 is less than or equal to expression2, and false if it is greater. If either value is NULL, the result is NULL.
<= orders values the same way as <. Numbers compare by value, dates and timestamps compare in time order, strings compare character by character and are case-sensitive, lists compare element by element, and structs compare field by field.
Syntax
expression1 <= expression2
Arguments
The <= (less than or equal) operator takes the following arguments:
expression1-
The first value to compare, of any type that has an order, for example
INTEGER,VARCHAR,DATE,TIMESTAMP, a list, or aSTRUCT. expression2-
The second value to compare. It must have the same type as
expression1, or a type that DuckDB can convert to a common type with it.
Return type
BOOLEAN
Examples
The following SQL query tests whether one number is less than or equal to another.
SELECT 1 <= 2 AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shows that <= returns true for two equal numbers, and false when the first date is later than the second.
SELECT 2 <= 2 AS equal_values, DATE '2026-10-02' <= DATE '2026-10-01' AS later_date
The query returns the following result.
| equal_values | later_date |
|---|---|
|
|
The following SQL query compares a timestamp at noon with dates. Because DuckDB converts each date to midnight, noon on 2026-10-01 is not less than or equal to DATE '2026-10-01', but it is less than DATE '2026-10-02'.
SELECT TIMESTAMP '2026-10-01 12:00:00' <= DATE '2026-10-01' AS same_day, TIMESTAMP '2026-10-01 12:00:00' < DATE '2026-10-02' AS before_next_day
The query returns the following result.
| same_day | before_next_day |
|---|---|
|
|
Considerations
When you compare a TIMESTAMP with a DATE, DuckDB converts the date to midnight at the start of that day. As a result, does not match timestamps after midnight on ts <= DATE '2026-10-01'2026-10-01. To include the whole day, compare with the start of the next day: .ts < DATE '2026-10-02'
<> and != (not equal) operators
Returns true if expression1 and expression2 are not equal, and false if they are equal. <> and != are two spellings of the same operator. If either value is NULL, the result is NULL.
The operator follows the same rules as =. It works on numbers, strings, dates, times, timestamps, intervals, Boolean values, lists, structs, and maps, and string comparison is case-sensitive. To get true when one value is NULL and the other is not, use IS DISTINCT FROM.
Syntax
expression1 <> expression2
expression1 != expression2
Arguments
The <> and != (not equal) operators take the following arguments:
expression1-
The first value to compare, of any type, for example
INTEGER,VARCHAR,DATE, a list, or aSTRUCT. expression2-
The second value to compare. It must have the same type as
expression1, or a type that DuckDB can convert to a common type with it.
Return type
BOOLEAN
Examples
The following SQL query compares two different numbers with !=.
SELECT 1 != 2 AS result
The query returns the following result.
| result |
|---|
|
The following SQL query uses <>. Strings that differ only in case are not equal, and numbers of different types are compared by value.
SELECT 'a' <> 'A' AS different_case, 1 <> 1.0 AS integer_and_decimal
The query returns the following result.
| different_case | integer_and_decimal |
|---|---|
|
|
The following SQL query shows that a <> filter leaves out the row where the value is NULL, because the comparison returns NULL for that row, not true. Only the row with 1 is returned.
SELECT x FROM (VALUES (1), (2), (NULL)) AS v(x) WHERE x <> 2 ORDER BY x
The query returns the following result.
| x |
|---|
|
The following SQL query adds 0.1 and 0.2 as DECIMAL values and as DOUBLE values. The DECIMAL sum equals 0.3, but the DOUBLE sum does not.
SELECT 0.1 + 0.2 <> 0.3 AS decimal_sum, CAST( 0.1 AS DOUBLE ) + CAST(0.2 AS DOUBLE) <> CAST(0.3 AS DOUBLE) AS double_sum
The query returns the following result.
| decimal_sum | double_sum |
|---|---|
|
|
Considerations
A WHERE clause keeps only the rows for which the condition is true. Because is x <> 2NULL when x is NULL, the filter WHERE also leaves out the rows where x <> 2x is NULL. To keep those rows, write WHERE .x IS DISTINCT FROM 2
DOUBLE and FLOAT values are approximate, so two results that look equal can differ in the last bits. To test whether two such values differ, check whether the difference is larger than a tolerance, as in abs(.a - b) > 1e-9
= (equal) operator
Returns true if expression1 and expression2 are equal, and false if they are not. = and == are two spellings of the same operator. If either value is NULL, the result is NULL, even when both values are NULL. To treat two NULL values as equal, use IS NOT DISTINCT FROM.
You can compare numbers, strings, dates, times, timestamps, intervals, Boolean values, lists, structs, and maps. Numbers of different types are compared by value, so 1 = 1.0 returns true. String comparison is case-sensitive, and trailing spaces count. Two lists are equal when they have the same length and equal elements in the same order, and two structs are equal when their fields are equal.
Syntax
expression1 = expression2
expression1 == expression2
Arguments
The = (equal) operator takes the following arguments:
expression1-
The first value to compare, of any type, for example
INTEGER,VARCHAR,DATE, a list, or aSTRUCT. expression2-
The second value to compare. It must have the same type as
expression1, or a type that DuckDB can convert to a common type with it.
Return type
BOOLEAN
Examples
The following SQL query compares two equal numbers.
SELECT 1 = 1 AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shows that a comparison with NULL returns NULL, even when both values are NULL.
SELECT 1 = NULL AS compared_with_null, NULL = NULL AS both_null
The query returns the following result.
| compared_with_null | both_null |
|---|---|
|
|
The following SQL query shows that string comparison is case-sensitive and counts trailing spaces, and that NULL elements inside two lists compare as equal.
SELECT 'abc' = 'ABC' AS different_case, 'abc ' = 'abc' AS trailing_space, [1, NULL] = [1, NULL] AS list_with_null
The query returns the following result.
| different_case | trailing_space | list_with_null |
|---|---|---|
|
|
|
The following SQL query compares a number and a date with string literals. DuckDB converts each literal to the type of the other value before it compares them.
SELECT 1 = '1' AS number_and_string, DATE '2026-10-01' = '2026-10-01' AS date_and_string
The query returns the following result.
| number_and_string | date_and_string |
|---|---|
|
|
Considerations
Inside a list or a struct, NULL elements and fields compare as equal to each other, so [1, NULL] = [1, NULL] returns true. The result is NULL only when one of the two values that you compare is itself NULL, as in [1, 2] = NULL.
When one value is a number or a date and the other is a string, DuckDB converts the string to the type of the other value. For example, 1 = '1' and DATE '2026-10-01' = '2026-10-01' return true. If the string is not a valid value of that type, as in 1 = 'abc', the query fails. This also applies to a VARCHAR column that you compare with a number: one row that holds a string that is not a number makes the query fail. To compare without the error, use TRY_CAST, as in TRY_CAST(, which returns s AS INTEGER) = 100NULL for strings that are not numbers.
A string that has a decimal part is rounded when it is converted to an integer, so 2 = '1.5' returns true. The same is true for a VARCHAR column s that holds '1.5'. Both and s = 2TRY_CAST( return s AS INTEGER) = 2true. A filter on an integer value can therefore match strings that are not whole numbers.
DOUBLE and FLOAT values are approximate, so CAST(0.1 AS DOUBLE) + CAST(0.2 AS DOUBLE) = CAST(0.3 AS DOUBLE) returns false. To compare them, test whether the difference is within a tolerance, as in abs(. Number literals with a decimal point, such as a - b) < 1e-90.1, are DECIMAL values, so 0.1 + 0.2 = 0.3 returns true.
NaN (not a number) equals NaN, so CAST('nan' AS DOUBLE) = CAST('nan' AS DOUBLE) returns true.
Intervals are compared as if every month had 30 days, so INTERVAL 1 MONTH = INTERVAL 30 DAY returns true.
Differences from Athena engine version 3
In Athena engine version 3, comparing a number with a string literal, as in 1 = '1', fails with an error. In DuckDB, the string literal is converted to a number, so the comparison runs and returns true.
In Athena engine version 3, comparing two arrays that are equal except for NULL elements, as in ARRAY[1, NULL] = ARRAY[1, NULL], returns NULL. In DuckDB, NULL elements inside lists and structs compare as equal, so [1, NULL] = [1, NULL] returns true.
In Athena engine version 3, NaN is not equal to any value, including NaN, so nan() = nan() returns false. In DuckDB, NaN equals NaN, so CAST('nan' AS DOUBLE) = CAST('nan' AS DOUBLE) returns true.
> (greater than) operator
Returns true if expression1 sorts after expression2, and false if it does not. If either value is NULL, the result is NULL.
> orders values the same way as <. Numbers compare by value, dates and timestamps compare in time order, strings compare character by character and are case-sensitive, lists compare element by element, and structs compare field by field. For DOUBLE and FLOAT values, NaN is greater than every other value, including infinity.
Syntax
expression1 > expression2
Arguments
The > (greater than) operator takes the following arguments:
expression1-
The first value to compare, of any type that has an order, for example
INTEGER,VARCHAR,DATE,TIMESTAMP, a list, or aSTRUCT. expression2-
The second value to compare. It must have the same type as
expression1, or a type that DuckDB can convert to a common type with it.
Return type
BOOLEAN
Examples
The following SQL query tests whether one number is greater than another.
SELECT 2 > 1 AS result
The query returns the following result.
| result |
|---|
|
The following SQL query filters DATE values with a string literal. Because DuckDB converts the literal to a DATE before it compares the values, the filter keeps only the two later dates.
SELECT d AS order_date FROM (VALUES (DATE '2026-10-01'), (DATE '2026-10-02'), (DATE '2026-10-03') ) AS v(d) WHERE d > '2026-10-01' ORDER BY d
The query returns the following results.
| order_date |
|---|
|
|
The following SQL query shows that NaN is greater than infinity and that NaN equals NaN.
SELECT CAST( 'nan' AS DOUBLE ) > CAST('infinity' AS DOUBLE) AS nan_is_greater, CAST( 'nan' AS DOUBLE ) = CAST('nan' AS DOUBLE) AS nan_equals_nan
The query returns the following result.
| nan_is_greater | nan_equals_nan |
|---|---|
|
|
The following SQL query counts the values that are greater than 1e300, first with the NaN value included and then with it left out. Of the three values, the NaN value and 2e300 are greater than 1e300, so the first count is 2 and the second is 1.
SELECT count(*) FILTER (WHERE x > 1e300) AS greater, count(*) FILTER ( WHERE x > 1e300 AND NOT isnan(x) ) AS greater_without_nan FROM (VALUES (CAST(1 AS DOUBLE)), (CAST('nan' AS DOUBLE)), (CAST(2e300 AS DOUBLE)) ) AS v(x)
The query returns the following result.
| greater | greater_without_nan |
|---|---|
|
|
Considerations
Because NaN is greater than every other value, a filter such as WHERE also returns the rows where x > 1e300x is NaN. To leave them out, add AND NOT isnan(.x)
A string literal that you compare with a DATE is converted to a DATE, so compares dates. If the string is not a valid date, the query fails.order_date > '2026-10-01'
Differences from Athena engine version 3
In Athena engine version 3, comparing a DATE value with a string literal, as in , fails with an error. In DuckDB, the string literal is converted to a order_date > '2026-10-01'DATE, so the comparison runs. To write a filter that runs on both engines, use a date literal, as in DATE '2026-10-01'.
In Athena engine version 3, NaN is not greater than any value, so nan() > infinity() returns false and a filter such as does not match NaN values. In DuckDB, NaN is greater than every other value, including infinity, so the same filter also returns the rows where x > 1e300x is NaN. To get the Athena engine version 3 result, add AND NOT isnan(.x)
>= (greater than or equal) operator
Returns true if expression1 is greater than or equal to expression2, and false if it is less. If either value is NULL, the result is NULL.
>= orders values the same way as <. Numbers compare by value, dates and timestamps compare in time order, strings compare character by character and are case-sensitive, lists compare element by element, and structs compare field by field. For structs, the first field that differs decides the result.
Syntax
expression1 >= expression2
Arguments
The >= (greater than or equal) operator takes the following arguments:
expression1-
The first value to compare, of any type that has an order, for example
INTEGER,VARCHAR,DATE,TIMESTAMP, a list, or aSTRUCT. expression2-
The second value to compare. It must have the same type as
expression1, or a type that DuckDB can convert to a common type with it.
Return type
BOOLEAN
Examples
The following SQL query tests whether one number is greater than or equal to another.
SELECT 2 >= 1 AS result
The query returns the following result.
| result |
|---|
|
The following SQL query compares structs. In the first comparison, the first field decides the result. In the second, the first fields are equal, so the second field decides.
SELECT { 'a': 2, 'b': 0 } >= {'a': 1, 'b': 9} AS first_field_decides, { 'a': 1, 'b': 2 } >= {'a': 1, 'b': 3} AS second_field_decides
The query returns the following result.
| first_field_decides | second_field_decides |
|---|---|
|
|
The following SQL query compares intervals. A month counts as 30 days, so one month equals 30 days and is less than 31 days.
SELECT INTERVAL 1 MONTH >= INTERVAL 30 DAY AS month_vs_30_days, INTERVAL 1 MONTH = INTERVAL 30 DAY AS month_equals_30_days, INTERVAL 1 MONTH >= INTERVAL 31 DAY AS month_vs_31_days
The query returns the following result.
| month_vs_30_days | month_equals_30_days | month_vs_31_days |
|---|---|---|
|
|
|
Considerations
Intervals are compared as if every month had 30 days, so INTERVAL 1 MONTH >= INTERVAL 30 DAY returns true. However, INTERVAL 1 MONTH >= INTERVAL 31 DAY returns false, even though some months have 31 days.
greatest function
Returns the largest of the values that you pass. The values can be numbers, strings, dates, timestamps, or values of any other type that you can compare, as long as they share a common type.
greatest ignores NULL arguments. It returns NULL only when every argument is NULL.
greatest compares strings character by character, by Unicode code point, and does not apply collations. The comparison is case-sensitive: every uppercase ASCII letter sorts before every lowercase ASCII letter, so 'a' is greater than 'B'.
Syntax
greatest(value [, ...])
Arguments
The greatest function takes the following argument:
value-
One or more values to compare. All values must have the same type, or types that have a common type, such as
INTEGERandDECIMAL.
Return type
The common type of the arguments. For example, INTEGER for integer literals and VARCHAR for strings.
Examples
The following SQL query returns the larger of two numbers.
SELECT greatest(42, 84) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query compares strings. Because uppercase letters sort before lowercase letters, the result is cde, not EFG.
SELECT greatest('abc', 'bcd', 'cde', 'EFG') AS result
The query returns the following result.
| result |
|---|
|
The following SQL query passes a NULL argument, which greatest ignores.
SELECT greatest(1, NULL, 3) AS result
The query returns the following result.
| result |
|---|
|
Differences from Athena engine version 3
In Athena engine version 3, greatest returns NULL when any argument is NULL. In DuckDB, greatest ignores NULL arguments, so greatest(1, NULL, 3) returns 3. To get the Athena engine version 3 result, wrap the call in a CASE expression that returns NULL when any argument is NULL.
IS DISTINCT FROM and IS NOT DISTINCT FROM operators
IS DISTINCT FROM returns true if expression1 and expression2 are different, and false if they are the same. It handles NULL like any other value: two NULL values are the same, and NULL is different from every value that is not NULL. The result is never NULL.
IS NOT DISTINCT FROM returns the opposite result: true if the two values are equal or both NULL, and false otherwise. Both operators work on the same types as =, including numbers, strings, dates, lists, and structs.
Syntax
expression1 IS [ NOT ] DISTINCT FROM expression2
Arguments
The IS DISTINCT FROM and IS NOT DISTINCT FROM operators take the following arguments:
expression1-
The first value to compare, of any type, for example
INTEGER,VARCHAR,DATE, a list, or aSTRUCT. It can beNULL. expression2-
The second value to compare. It must have the same type as
expression1, or a type that DuckDB can convert to a common type with it. It can beNULL.
Return type
BOOLEAN
Examples
The following SQL query uses IS DISTINCT FROM with a NULL value, with two NULL values, and with two equal numbers. Only the comparison of 1 with NULL returns true.
SELECT 1 IS DISTINCT FROM NULL AS one_and_null, NULL IS DISTINCT FROM NULL AS both_null, 1 IS DISTINCT FROM 1 AS same_value
The query returns the following result.
| one_and_null | both_null | same_value |
|---|---|---|
|
|
|
The following SQL query uses IS NOT DISTINCT FROM with the same inputs.
SELECT 1 IS NOT DISTINCT FROM NULL AS one_and_null, NULL IS NOT DISTINCT FROM NULL AS both_null, 1 IS NOT DISTINCT FROM 1 AS same_value
The query returns the following result.
| one_and_null | both_null | same_value |
|---|---|---|
|
|
|
The following SQL query compares <> with IS DISTINCT FROM on a value that can be NULL. For the NULL row, <> returns NULL, and IS DISTINCT FROM returns true.
SELECT x, x <> 2 AS not_equal, x IS DISTINCT FROM 2 AS is_distinct FROM (VALUES (1), (2), (NULL)) AS v(x) ORDER BY x NULLS LAST
The query returns the following results.
| x | not_equal | is_distinct |
|---|---|---|
|
|
|
|
|
|
|
|
|
The following SQL query joins two inputs on a key that can be NULL. With IS NOT DISTINCT FROM, the two NULL keys match, so both rows are returned. With =, only the row with key 1 would match.
SELECT a.k AS k, b.label AS label FROM (VALUES (1), (NULL)) AS a(k) JOIN (VALUES (1, 'one'), (NULL, 'missing') ) AS b(k, label) ON a.k IS NOT DISTINCT FROM b.k ORDER BY label
The query returns the following results.
| k | label |
|---|---|
|
|
|
|
Considerations
For two values that are not NULL, IS DISTINCT FROM returns the same result as <>, and IS NOT DISTINCT FROM returns the same result as =.
To match NULL keys to each other in a join, use IS NOT DISTINCT FROM in the join condition. With =, a row whose key is NULL never matches.
least function
Returns the smallest of the values that you pass. The values can be numbers, strings, dates, timestamps, or values of any other type that you can compare, as long as they share a common type.
least ignores NULL arguments. It returns NULL only when every argument is NULL.
least compares strings character by character, by Unicode code point, and does not apply collations. The comparison is case-sensitive: every uppercase ASCII letter sorts before every lowercase ASCII letter, so 'B' is less than 'a'.
Syntax
least(value [, ...])
Arguments
The least function takes the following argument:
value-
One or more values to compare. All values must have the same type, or types that have a common type, such as
INTEGERandDECIMAL.
Return type
The common type of the arguments. For example, INTEGER for integer literals and VARCHAR for strings.
Examples
The following SQL query returns the smaller of two numbers.
SELECT least(42, 84) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query compares strings. Because uppercase letters sort before lowercase letters, the result is EFG.
SELECT least('abc', 'bcd', 'cde', 'EFG') AS result
The query returns the following result.
| result |
|---|
|
The following SQL query passes a NULL argument, which least ignores.
SELECT least(1, NULL, 3) AS result
The query returns the following result.
| result |
|---|
|
Differences from Athena engine version 3
In Athena engine version 3, least returns NULL when any argument is NULL. In DuckDB, least ignores NULL arguments, so least(1, NULL, 3) returns 1. To get the Athena engine version 3 result, wrap the call in a CASE expression that returns NULL when any argument is NULL.