View a markdown version of this page

Arithmetic operators - Amazon Athena

Arithmetic operators

Use these operators to add, subtract, multiply, and divide numbers, get remainders and absolute values, and raise numbers to powers. The + and - operators also work with dates, times, timestamps, and intervals.

% (modulo) operator

Returns the remainder after expression1 is divided by expression2. The result has the sign of expression1, so -7 % 3 returns -1, and 7 % -3 returns 1. If either operand is NULL, the result is NULL.

% also works on DECIMAL, FLOAT, and DOUBLE values, so 7.5 % 2 returns 1.5. The result has the common type of the two operands.

mod is an alias of %.

Syntax

expression1 % expression2 mod(expression1, expression2)

Arguments

The % (modulo) operator takes the following arguments:

expression1

The dividend: a value of any numeric type.

expression2

The divisor: a value of any numeric type.

Return type

The common numeric type of the operands, for example INTEGER for two INTEGER values, or DECIMAL(11,1) for a DECIMAL(2,1) and an INTEGER.

Examples

The following SQL query returns the remainder of dividing 7 by 3.

SELECT 7 % 3 AS result

The query returns the following result.

result

1

The following SQL query shows that the remainder has the sign of the dividend, and uses the mod alias.

SELECT -7 % 3 AS negative_dividend, 7 % -3 AS negative_divisor, mod(-7, 3) AS mod_function

The query returns the following result.

negative_dividend negative_divisor mod_function

-1

1

-1

The following SQL query returns the remainder of dividing the DECIMAL value 7.5 by 2. The result is a DECIMAL.

SELECT 7.5 % 2 AS result

The query returns the following result.

result

1.5

The following SQL query returns a remainder that is never negative, and uses TRY to return NULL for a zero divisor.

SELECT ((-7 % 3) + 3) % 3 AS non_negative, TRY(7 % 0) AS by_zero

The query returns the following result.

non_negative by_zero

2

NULL

Considerations

If expression2 is 0 and the operands are integers or DECIMAL values, the query fails. To return NULL instead, use TRY(x % y) or x % NULLIF(y, 0). With a DOUBLE operand, a zero divisor returns NaN.

Because the remainder has the sign of the dividend, it can be negative when the dividend is negative. To get a remainder from 0 to n - 1, for example to assign rows to n buckets, use ((x % n) + n) % n.

* (multiplication) operator

Returns the product of expression1 and expression2. The result has the common type of the two operands. For two DECIMAL values, the scale of the result is the sum of the two scales, so 1.10 * 2.5 returns 2.750. If either operand is NULL, the result is NULL.

You can also multiply an INTERVAL by a number, in either order, to get a longer or shorter INTERVAL.

multiply is an alias of *.

Syntax

expression1 * expression2 multiply(expression1, expression2)

Arguments

The * (multiplication) operator takes the following arguments:

expression1

The value to multiply, as a value of any numeric type or an INTERVAL.

expression2

The value to multiply by, as a value of any numeric type. When expression1 is an INTERVAL, it is a BIGINT or DOUBLE number, or a value that converts to one of these types.

Return type

The common numeric type of the operands, for example INTEGER for two INTEGER values, BIGINT for a BIGINT and an INTEGER, or DECIMAL(12,1) for an INTEGER and a DECIMAL(2,1). An INTERVAL times a number returns INTERVAL.

Examples

The following SQL query multiplies an integer by a DECIMAL value, which returns a DECIMAL.

SELECT 4 * 2.5 AS result

The query returns the following result.

result

10.0

The following SQL query multiplies two numbers with the multiply alias and shows that multiplying a number by NULL returns NULL.

SELECT multiply(6, 7) AS multiply_function, 6 * NULL AS null_operand

The query returns the following result.

multiply_function null_operand

42

NULL

The following SQL query multiplies two DECIMAL values, whose product has a scale of 3, and multiplies a BIGINT value so that the product does not overflow.

SELECT 1.10 * 2.5 AS decimal_product, CAST(100000 AS BIGINT) * 100000 AS bigint_product

The query returns the following result.

decimal_product bigint_product

2.750

10000000000

The following SQL query multiplies an INTERVAL by a number.

SELECT INTERVAL 90 MINUTE * 2 AS result

The query returns the following result.

result

03:00:00

Considerations

Integer multiplication fails when the product does not fit in the result type. 100000 * 100000 fails, because both literals are INTEGER values. Cast one operand to a wider type, as in CAST(100000 AS BIGINT) * 100000.

Multiplying an INTERVAL by a fraction can split a month into days, so INTERVAL 1 MONTH * 1.5 returns 1 month 15 days.

** (exponent) operator

Returns base raised to the power exponent, as a DOUBLE. Both operands are converted to DOUBLE first, so 2 ** 3 returns 8.0, not the integer 8. A negative exponent returns a fraction, so 2 ** -1 returns 0.5. If either operand is NULL, the result is NULL.

^, pow, and power are aliases of **, so 2 ^ 3, pow(2, 3), and power(2, 3) return the same result.

Syntax

base ** exponent base ^ exponent pow(base, exponent) power(base, exponent)

Arguments

The ** (exponent) operator takes the following arguments:

base

The value to raise to a power, as a DOUBLE value. Values of other numeric types are converted to DOUBLE.

exponent

The power, as a DOUBLE value. Values of other numeric types are converted to DOUBLE.

Return type

DOUBLE

Examples

The following SQL query raises 2 to the power 3.

SELECT 2 ** 3 AS result

The query returns the following result.

result

8.0

The following SQL query uses the ^ operator and the pow and power functions. An exponent of 0.5 returns the square root.

SELECT 2 ^ 10 AS caret, pow(2, 3) AS pow_function, power(9, 0.5) AS power_function

The query returns the following result.

caret pow_function power_function

1024.0

8.0

3.0

The following SQL query shows that a minus sign in front of the base is applied first, and that ** is applied from left to right.

SELECT -2 ** 2 AS sign_first, -(2 ** 2) AS negated_power, 2 ** 3 ** 2 AS left_to_right

The query returns the following result.

sign_first negated_power left_to_right

4.0

-4.0

64.0

The following SQL query shows that a negative exponent returns a fraction and that a NULL exponent returns NULL.

SELECT 2 ** -1 AS negative_exponent, 2 ** NULL AS null_exponent

The query returns the following result.

negative_exponent null_exponent

0.5

NULL

Considerations

A minus sign in front of a value is applied before ** and ^, so -2 ** 2 returns 4.0, the square of -2. To negate a power, write -(2 ** 2).

** and ^ are applied from left to right, so 2 ** 3 ** 2 returns 64.0, which is (2 ** 3) ** 2. To raise a number to a power of a power, use parentheses, as in 2 ** (3 ** 2).

A negative base with an exponent that is not a whole number returns NaN, as in (-8) ** (1.0 / 3). A result that is too large for a DOUBLE returns infinity, as in 10 ** 400. Neither case makes the query fail.

+ (addition) operator

Returns the sum of expression1 and expression2. The result has the common type of the two operands. For example, the sum of two INTEGER values is an INTEGER, and the sum of an INTEGER and a DECIMAL is a DECIMAL. If either operand is NULL, the result is NULL. With one operand, + returns the operand unchanged.

You can also use + with dates, times, timestamps, and intervals. A DATE plus an INTEGER returns the DATE that many days later. A DATE, TIME, or TIMESTAMP plus an INTERVAL returns the value moved forward by the interval, and a DATE plus a TIME returns the TIMESTAMP at that time of day. Two intervals add to an INTERVAL. With two lists, + returns one list with the elements of both lists, in order.

add is an alias of +: add(expression1, expression2) returns the same result as expression1 + expression2. With lists, add also accepts more than two arguments, so add([1], [2], [3]) returns [1, 2, 3].

Syntax

expression1 + expression2 + expression add(expression1, expression2)

Arguments

The + (addition) operator takes the following arguments:

expression1

The value to add to. It can be a value of any numeric type, such as INTEGER, BIGINT, DECIMAL, or DOUBLE. You can also pass a DATE, TIME, TIME WITH TIME ZONE, TIMESTAMP, TIMESTAMP WITH TIME ZONE, or INTERVAL value, or a list.

expression2

The value to add. With a number, any numeric value. With a DATE, an INTEGER number of days, an INTERVAL, or a TIME. With a TIME, TIMESTAMP, or TIMESTAMP WITH TIME ZONE, an INTERVAL. With a list, another list. You can write the operands in either order, as in 7 + DATE '2026-11-18'.

expression

The value to return unchanged in the one-operand form, as a value of any numeric type.

Return type

The common numeric type of the operands, for example INTEGER for two INTEGER values, or DECIMAL(12,1) for an INTEGER and a DECIMAL(2,1). A DATE plus an INTEGER returns DATE, and a DATE plus an INTERVAL or a TIME returns TIMESTAMP. A TIME, TIMESTAMP, or TIMESTAMP WITH TIME ZONE plus an INTERVAL keeps the type of the time value, and two intervals return INTERVAL. Two lists return a list type, such as INTEGER[].

Examples

The following SQL query adds two integers.

SELECT 2 + 3 AS result

The query returns the following result.

result

5

The following SQL query shows that the add alias returns the same sum as +, and that adding NULL to a number returns NULL.

SELECT add(2, 3) AS add_function, 2 + NULL AS null_operand

The query returns the following result.

add_function null_operand

5

NULL

The following SQL query adds a number of days to a DATE, which returns a DATE, and adds an INTERVAL to a DATE, which returns a TIMESTAMP.

SELECT DATE '2026-11-18' + 7 AS plus_days, DATE '2026-01-31' + INTERVAL 1 DAY AS plus_interval

The query returns the following result.

plus_days plus_interval

2026-11-25

2026-02-01 00:00:00

The following SQL query adds two lists, which returns one list with the elements of both.

SELECT [1, 2] + [3] AS result

The query returns the following result.

result

[1, 2, 3]

Considerations

Integer addition fails when the sum does not fit in the result type. 2147483647 + 1 fails, because both literals are INTEGER values. Cast one operand to a wider type, as in CAST(2147483647 AS BIGINT) + 1.

+ does not convert a string to a number. An expression such as '2' + 3 fails, so cast the string first, as in CAST('2' AS INTEGER) + 3.

To add a number of days to a DATE, the number must be an INTEGER. A BIGINT value, such as a BIGINT column or the result of count(*), fails, so cast it first, as in order_date + CAST(days AS INTEGER).

Adding an INTERVAL to a TIME wraps around midnight, so TIME '23:30:00' + INTERVAL 1 HOUR returns 00:30:00.

When you add two lists, a NULL list is skipped, so [1, 2] + NULL returns [1, 2]. The || operator returns NULL in that case.

Differences from Athena engine version 3

In Athena engine version 3, adding a decimal literal, such as 0.005, to a DECIMAL column computes the sum as a DOUBLE. In DuckDB, a literal with a decimal point is a DECIMAL, so the sum stays a DECIMAL.

- (subtraction and negation) operator

Returns expression1 minus expression2. The result has the common type of the two operands. With one operand, - returns the negated value, so -(4) returns -4. If an operand is NULL, the result is NULL.

You can also subtract with dates, times, timestamps, and intervals. A DATE minus an INTEGER returns the DATE that many days earlier, and a DATE, TIME, or TIMESTAMP minus an INTERVAL returns the value moved back by the interval. The difference of two DATE values is the number of days between them, as a BIGINT. The difference of two TIMESTAMP values is an INTERVAL in days, hours, minutes, and seconds.

subtract is an alias of -. With two arguments, subtract subtracts the second from the first, and with one argument, it negates the argument.

Syntax

expression1 - expression2 - expression subtract(expression1 [, expression2 ])

Arguments

The - (subtraction and negation) operator takes the following arguments:

expression1

The value to subtract from, as a value of any numeric type, or a DATE, TIME, TIMESTAMP, TIMESTAMP WITH TIME ZONE, or INTERVAL value.

expression2

The value to subtract. With a number, any numeric value. With a DATE, an INTEGER number of days, an INTERVAL, or another DATE. With a TIME, an INTERVAL. With a TIMESTAMP or TIMESTAMP WITH TIME ZONE, an INTERVAL or another value of the same type.

expression

The value to negate in the one-operand form, as a value of any numeric type or an INTERVAL.

Return type

The common numeric type of the operands, or the type of the operand for negation, for example INTEGER. A DATE minus an INTEGER returns DATE, and a DATE minus an INTERVAL returns TIMESTAMP. A TIME, TIMESTAMP, or TIMESTAMP WITH TIME ZONE minus an INTERVAL keeps the type of the time value. Two DATE values return BIGINT, and two TIMESTAMP values, two TIMESTAMP WITH TIME ZONE values, or two intervals return INTERVAL.

Examples

The following SQL query subtracts one integer from another.

SELECT 5 - 7 AS result

The query returns the following result.

result

-2

The following SQL query negates a number with a minus sign and subtracts 4 from 10 with the subtract alias.

SELECT -(4) AS negated, subtract(10, 4) AS subtract_function

The query returns the following result.

negated subtract_function

-4

6

The following SQL query subtracts one date from another, which returns the number of days between them, and one timestamp from another, which returns an INTERVAL.

SELECT DATE '2026-11-18' - DATE '2026-11-01' AS days_between, TIMESTAMP '2026-11-18 10:30:00' - TIMESTAMP '2026-11-17 08:00:00' AS time_between

The query returns the following result.

days_between time_between

17

1 day 02:30:00

The following SQL query subtracts a negative number. It puts a space or parentheses between the two minus signs, because -- starts a comment.

SELECT 5 - -1 AS with_space, 5 - (-1) AS with_parentheses

The query returns the following result.

with_space with_parentheses

6

6

Considerations

Two hyphens start a comment, so DuckDB ignores everything after -- on the same line. To subtract a negative number, put a space or parentheses between the two minus signs, as in 5 - -1 or 5 - (-1). Without them, 5--1 returns 5.

Integer subtraction fails when the result does not fit in the result type. For an unsigned type, a result that is less than zero fails, as in CAST(0 AS UINTEGER) - CAST(1 AS UINTEGER).

A minus sign in front of a value is applied before ** and ^, so -2 ** 2 returns 4.0. To negate a power, write -(2 ** 2).

Differences from Athena engine version 3

In Athena engine version 3, subtracting one DATE from another returns an INTERVAL DAY TO SECOND value. In DuckDB, it returns the number of days between the dates as a BIGINT, so DATE '2026-11-18' - DATE '2026-11-01' returns 17.

/ (division) operator

Returns expression1 divided by expression2. The result is a DOUBLE, even when both operands are integers or DECIMAL values, so 7 / 2 returns 3.5. When an operand is a FLOAT and the other is a FLOAT, an integer, or a DECIMAL, the result is a FLOAT. If either operand is NULL, the result is NULL.

Dividing by zero does not make the query fail. A positive number divided by zero returns infinity, a negative number divided by zero returns negative infinity, and 0 / 0 returns NaN. You can also divide an INTERVAL by a number to get a shorter INTERVAL.

To divide integers and drop the fractional part, use //. To divide DECIMAL values and get an exact DECIMAL result, use the decimal_division function.

Syntax

expression1 / expression2

Arguments

The / (division) operator takes the following arguments:

expression1

The dividend: a value of any numeric type, or an INTERVAL.

expression2

The divisor: a value of any numeric type.

Return type

DOUBLE, or FLOAT when an operand is a FLOAT and the other operand is not a DOUBLE. An INTERVAL divided by a number returns INTERVAL.

Examples

The following SQL query divides two integers. The result is a DOUBLE.

SELECT 7 / 2 AS result

The query returns the following result.

result

3.5

The following SQL query divides 1 by zero, which returns infinity, shown as inf. It also uses NULLIF to return NULL instead.

SELECT 1 / 0 AS by_zero, 1 / NULLIF(0, 0) AS with_nullif

The query returns the following result.

by_zero with_nullif

inf

NULL

The following SQL query divides a DECIMAL value by an integer, which returns a DOUBLE, and casts the quotient back to a DECIMAL with two decimal places.

SELECT CAST(1.00 AS DECIMAL(10,2)) / 3 AS double_result, CAST( CAST(1.00 AS DECIMAL(10,2)) / 3 AS DECIMAL(10,2) ) AS decimal_result

The query returns the following result.

double_result decimal_result

0.3333333333333333

0.33

The following SQL query divides an INTERVAL by a number.

SELECT INTERVAL 1 HOUR / 2 AS result

The query returns the following result.

result

00:30:00

Considerations

Because the result is a DOUBLE, it is approximate. Dividing two DECIMAL values, such as amounts of money, can return a value such as 0.3333333333333333. To keep a fixed number of decimal places, cast the result, as in CAST(x / y AS DECIMAL(12,2)), or use the decimal_division function.

A query that divides by a column that holds 0 returns infinity or NaN for those rows, not an error. To return NULL for those rows instead, write x / NULLIF(y, 0).

divide is not a function form of /. It is an alias of //, so divide(7, 2) returns 3.

Differences from Athena engine version 3

In Athena engine version 3, / between two integers returns an integer and drops the fractional part, so 5 / 2 returns 2. In DuckDB, it returns the DOUBLE value 2.5. To get the Athena engine version 3 result, use 5 // 2. For more information about differences that change results, see Differences that change results without an error.

In Athena engine version 3, dividing an integer by zero, as in 1 / 0, makes the query fail. In DuckDB, it returns infinity, so wrapping it in TRY does not return NULL. To get NULL for a zero divisor, use x / NULLIF(y, 0).

In Athena engine version 3, dividing two DECIMAL values returns a DECIMAL, so CAST(1.00 AS DECIMAL(10,2)) / CAST(3 AS DECIMAL(10,0)) returns 0.33. In DuckDB, it returns a DOUBLE. To get a DECIMAL result, cast the quotient to the DECIMAL type that you need, or use the decimal_division function.

// (integer division) operator

Returns expression1 divided by expression2 without the fractional part, when both operands are integers. The result has the integer type of the operands, so 7 // 2 returns the INTEGER value 3. Because the fractional part is dropped, the result is rounded toward zero, so -7 // 2 returns -3. If either operand is NULL, the result is NULL.

When an operand is a DOUBLE, FLOAT, or DECIMAL value, // keeps the fractional part and returns a DOUBLE or FLOAT, so 7.5 // 2 returns 3.75. To round such a quotient down to a whole number, use floor(x / y).

divide is an alias of //, not of /.

Syntax

expression1 // expression2 divide(expression1, expression2)

Arguments

The // (integer division) operator takes the following arguments:

expression1

The dividend: a value of any integer type, such as INTEGER or BIGINT. You can also pass a DOUBLE, FLOAT, or DECIMAL value, but then // keeps the fractional part.

expression2

The divisor: a value of any numeric type.

Return type

The common integer type of the operands, for example INTEGER for two INTEGER values or BIGINT for a BIGINT and an INTEGER. When an operand is a FLOAT and the other operand is not a DOUBLE, the result is FLOAT. Otherwise, when an operand is a DOUBLE or DECIMAL value, the result is DOUBLE.

Examples

The following SQL query divides two integers and drops the fractional part.

SELECT 7 // 2 AS result

The query returns the following result.

result

3

The following SQL query divides a negative integer with // and with the divide alias. The result is rounded toward zero.

SELECT -7 // 2 AS negative_dividend, divide(-7, 2) AS divide_function

The query returns the following result.

negative_dividend divide_function

-3

-3

The following SQL query uses // with a DECIMAL operand and with a DOUBLE operand. In both cases, the fractional part is kept.

SELECT 7.5 // 2 AS decimal_operand, 7.0e0 // 2 AS double_operand

The query returns the following result.

decimal_operand double_operand

3.75

3.5

The following SQL query uses TRY to return NULL for a division by zero.

SELECT TRY(7 // 0) AS result

The query returns the following result.

result

NULL

Considerations

If expression2 is 0, the query fails. To return NULL instead, wrap the division in TRY, as in TRY(x // y), or divide by NULLIF(y, 0).

// drops the fractional part only when both operands are integers. To divide a DECIMAL or DOUBLE value and drop the fractional part, use trunc(x / y), or cast the operands to an integer type first.

@ (absolute value) operator

Returns x without its sign: a negative number becomes positive, and zero and positive numbers are returned unchanged. The result has the type of x, so abs(-17.4) returns the DECIMAL(3,1) value 17.4. If x is NULL, the result is NULL.

abs is an alias of @: abs(x) returns the same result as @ x.

Syntax

@ x abs(x)

Arguments

The @ (absolute value) operator takes the following argument:

x

The number to remove the sign from, as a value of any numeric type, such as INTEGER, BIGINT, DECIMAL, or DOUBLE.

Return type

The type of x, for example INTEGER, DECIMAL(3,1), or DOUBLE.

Examples

The following SQL query returns the absolute value of a negative integer with @.

SELECT @(-5) AS result

The query returns the following result.

result

5

The following SQL query uses the abs alias with a DECIMAL value. The result keeps the DECIMAL type.

SELECT abs(-17.4) AS result

The query returns the following result.

result

17.4

The following SQL query shows that @ applies to the whole sum that follows it. Put parentheses around the @ expression, or use abs, to apply it to one value.

SELECT @ -5 + 10 AS without_parentheses, (@ -5) + 10 AS with_parentheses, abs(-5) + 10 AS abs_function

The query returns the following result.

without_parentheses with_parentheses abs_function

5

15

15

Considerations

@ is applied after + and -, so @ -5 + 10 returns 5, the absolute value of -5 + 10. Parentheses around the operand alone do not change this: @(-5) + 10 also returns 5. To get 15, write (@ -5) + 10 or abs(-5) + 10.

Write a space or a parenthesis between @ and a minus sign. @-5 fails, because @- is read as the name of one operator.

The absolute value of the smallest value of a signed integer type does not fit in that type, so abs(CAST(-2147483648 AS INTEGER)) fails. Cast the value to a wider type first, as in abs(CAST(x AS BIGINT)).

abs does not convert a string to a number. abs('-3') fails, so cast the string first, as in abs(CAST('-3' AS INTEGER)).