Arithmetic functions

**Returned value**

Returns the bigger value of `x` and `y`. [`Float64`](/docs/sql-reference/data-types/float)

**Examples**

**Usage example**

```
SELECT max2(-1, 2)
```

```
2
```

## midpoint[​](#midpoint "Direct link to midpoint")

Introduced in: v25.11.0

Computes and returns the average value of the provided arguments.
Supports numerical and temporal types.

**Syntax**

```
midpoint(x1[, x2, ...])
```

**Arguments**

- `x1[, x2, ...]` — Accepts a single value or multiple values for averaging.

**Returned value**

Returns the average value of the provided arguments, promoted to the largest compatible type.

**Examples**

**Numeric types**

```
SELECT midpoint(1, toUInt8(3), 0.5) AS result, toTypeName(result) AS type;
-- The type returned is a Float64 as the UInt8 must be promoted to 64 bit for the comparison.
```

```
┌─result─┬─type────┐
│    1.5 │ Float64 │
└────────┴─────────┘
```

**Decimal types**

```
SELECT midpoint(toDecimal32(1.5, 2), toDecimal32(1, 1), 2) AS result, toTypeName(result) AS type;
```

```
┌─result─┬─type──────────┐
│    1.5 │ Decimal(9, 2) │
└────────┴───────────────┘
```

**Date types**

```
SELECT midpoint(toDate('2025-01-01'), toDate('2025-01-05')) AS result, toTypeName(result) AS type;
```

```
┌─────result─┬─type─┐
│ 2025-01-03 │ Date │
└────────────┴──────┘
```

**DateTime types**

```
SELECT midpoint(toDateTime('2025-01-01 00:00:00'), toDateTime('2025-01-03 12:00:00')) AS result, toTypeName(result) AS type;
```

```
┌──────────────result─┬─type─────┐
│ 2025-01-02 06:00:00 │ DateTime │
└─────────────────────┴──────────┘
```

**Time64 types**

```
SELECT midpoint(toTime64('12:00:00', 0), toTime64('14:00:00', 0)) AS result, toTypeName(result) AS type;
```

```
┌───result─┬─type──────┐
│ 13:00:00 │ Time64(0) │
└──────────┴───────────┘
```

## min2[​](#min2 "Direct link to min2")

Introduced in: v21.11.0

Returns the smaller of two numeric values `x` and `y`.

**Syntax**

```
min2(x, y)
```

**Arguments**

- `x` — First value [`(U)Int8/16/32/64`](/docs/sql-reference/data-types/int-uint) or [`Float*`](/docs/sql-reference/data-types/float) or [`Decimal`](/docs/sql-reference/data-types/decimal)
- `y` — Second value [`(U)Int8/16/32/64`](/docs/sql-reference/data-types/int-uint) or [`Float*`](/docs/sql-reference/data-types/float) or [`Decimal`](/docs/sql-reference/data-types/decimal)

**Returned value**

Returns the smaller value of `x` and `y`. [`Float64`](/docs/sql-reference/data-types/float)

**Examples**

**Usage example**

```
SELECT min2(-1, 2)
```

```
-1
```

## minus[​](#minus "Direct link to minus")

Introduced in: v1.1.0

Calculates the difference of two values `a` and `b`. The result is always signed.
Similar to plus, it is possible to subtract an integer from a date or date with time.
Additionally, subtraction between date with time is supported, resulting in the time difference between them.

**Syntax**

```
minus(x, y)
```

**Arguments**

- `x` — Minuend. - `y` — Subtrahend.

**Returned value**

x minus y

**Examples**

**Subtracting two numbers**

```
SELECT minus(10, 5)
```

```
5
```

**Subtracting an integer and a date**

```
SELECT minus(toDate('2025-01-01'),5)
```

```
2024-12-27
```

## modulo[​](#modulo "Direct link to modulo")

Introduced in: v1.1.0

Calculates the remainder of the division of two values a by b.

The result type is an integer if both inputs are integers. If one of the
inputs is a floating-point number, the result type is Float64.

The remainder is computed like in C++. Truncated division is used for
negative numbers.

An exception is thrown when dividing by zero or when dividing a minimal
negative number by minus one.

**Syntax**

```
modulo(a, b)
```

**Aliases**: `mod`

**Arguments**

- `a` — The dividend - `b` — The divisor (modulus)

**Returned value**

The remainder of a % b

**Examples**

**Usage example**

```
SELECT modulo(5, 2)
```

```
1
```

## moduloLegacy[​](#moduloLegacy "Direct link to moduloLegacy")

Introduced in: v1.1.0

Calculates the remainder of a division. This is the legacy modulo implementation that uses the C++ `%` operator, which may produce negative results for negative arguments. This function exists for backward compatibility with old table partitioning logic. Use `modulo` or `positiveModulo` for standard behavior.

**Syntax**

```
moduloLegacy(a, b)
```

**Arguments**

- `a` — The dividend. [`(U)Int*`](/docs/sql-reference/data-types/int-uint) or [`Float*`](/docs/sql-reference/data-types/float)
- `b` — The divisor. [`(U)Int*`](/docs/sql-reference/data-types/int-uint) or [`Float*`](/docs/sql-reference/data-types/float)

**Returned value**

Returns the remainder of the division. [`(U)Int*`](/docs/sql-reference/data-types/int-uint) or [`Float*`](/docs/sql-reference/data-types/float)

**Examples**

**Basic usage**

```
SELECT moduloLegacy(10, 3)
```

```
1
```

## moduloOrNull[​](#moduloOrNull "Direct link to moduloOrNull")

Introduced in: v25.5.0

Calculates the remainder when dividing `a` by `b`. Similar to function `modulo` except that `moduloOrNull` will return NULL
if the right argument is 0.

**Syntax**

```
moduloOrNull(x, y)
```

**Aliases**: `modOrNull`

**Arguments**

- `x` — The dividend. [`(U)Int*`](/docs/sql-reference/data-types/int-uint) or [`Float*`](/docs/sql-reference/data-types/float)
- `y` — The divisor (modulus). [`(U)Int*`](/docs/sql-reference/data-types/int-uint) or [`Float*`](/docs/sql-reference/data-types/float)

**Returned value**

Returns the remainder of the division of `x` by `y`, or null when the divisor is zero.

**Examples**

**moduloOrNull by zero**

```
SELECT moduloOrNull(5, 0)
```

```
\N
```

## moduloOrZero[​](#moduloOrZero "Direct link to moduloOrZero")

Introduced in: v20.3.0

Like modulo but returns zero when the divisor is zero, as opposed to an
exception with the modulo function.

**Syntax**

```
moduloOrZero(a, b)
```

**Arguments**

- `a` — The dividend. [`(U)Int*`](/docs/sql-reference/data-types/int-uint) or [`Float*`](/docs/sql-reference/data-types/float)
- `b` — The divisor (modulus). [`(U)Int*`](/docs/sql-reference/data-types/int-uint) or [`Float*`](/docs/sql-reference/data-types/float)

**Returned value**

Returns the remainder of a % b, or `0` when the divisor is `0`.

**Examples**

**Usage example**

```
SELECT moduloOrZero(5, 0)
```

```
0
```

## multiply[​](#multiply "Direct link to multiply")

Introduced in: v1.1.0

Calculates the product of two values `x` and `y`.

**Syntax**

```
multiply(x, y)
```

**Arguments**

- `x` — factor. [`(U)Int*`](/docs/sql-reference/data-types/int-uint) or [`Float*`](/docs/sql-reference/data-types/float) or [`Decimal`](/docs/sql-reference/data-types/decimal)
- `y` — factor. [`(U)Int*`](/docs/sql-reference/data-types/int-uint) or [`Float*`](/docs/sql-reference/data-types/float) or [`Decimal`](/docs/sql-reference/data-types/decimal)

**Returned value**

Returns the product of x and y

**Examples**

**Multiplying two numbers**

```
SELECT multiply(5,5)
```

```
25
```

## multiplyDecimal[​](#multiplyDecimal "Direct link to multiplyDecimal")

Introduced in: v22.12.0

Performs multiplication on two decimals. Result value will be of type [Decimal256](/docs/sql-reference/data-types/decimal).
Result scale can be explicitly specified by `result_scale` argument (const Integer in range `[0, 76]`). If not specified, the result scale is the max scale of given arguments.

Note

These functions work significantly slower than usual `multiply`.
In case you don't really need controlled precision and/or need fast computation, consider using [multiply](#multiply)

**Syntax**

```
multiplyDecimal(a, b[, result_scale])
```

**Arguments**

- `a` — First value. [`Decimal`](/docs/sql-reference/data-types/decimal)
- `b` — Second value. [`Decimal`](/docs/sql-reference/data-types/decimal)
- `result_scale` — Scale of result. [`(U)Int*`](/docs/sql-reference/data-types/int-uint)

**Returned value**

The result of multiplication with the given scale. Type: [`Decimal256`](/docs/sql-reference/data-types/decimal)

**Examples**

**Usage example**

```
SELECT multiplyDecimal(toDecimal256(-12, 0), toDecimal32(-2.1, 1), 1)
```

```
25.2
```

**Difference with regular multiplication**