docs-v2/content/shared/sql-reference/operators/arithmetic.md

2.9 KiB

Arithmetic operators take two numeric values (either literals or variables) and perform a calculation that returns a single numeric value.

Operator Description
+ Addition {{< icon "link" >}}
- Subtraction {{< icon "link" >}}
* Multiplication {{< icon "link" >}}
/ Division {{< icon "link" >}}
% Modulo {{< icon "link" >}}

+

The + operator adds two operands together and returns the sum.

{{< flex >}} {{% flex-content "two-thirds operator-example" %}}

SELECT 1 + 2

{{% /flex-content %}} {{% flex-content "third operator-example" %}}

int64(1) + int64(2)
3

{{% /flex-content %}} {{< /flex >}}

-

The - operator subtracts the right operand from the left operand and returns the difference.

{{< flex >}} {{% flex-content "two-thirds operator-example" %}}

SELECT 4 - 2

{{% /flex-content %}} {{% flex-content "third operator-example" %}}

int64(4) - int64(2)
2

{{% /flex-content %}} {{< /flex >}}

*

The * operator multiplies two operands together and returns the product.

{{< flex >}} {{% flex-content "two-thirds operator-example" %}}

SELECT 2 * 3

{{% /flex-content %}} {{% flex-content "third operator-example" %}}

int64(2) * int64(3)
6

{{% /flex-content %}} {{< /flex >}}

/

The / operator divides the left operand by the right operand and returns the quotient.

{{< flex >}} {{% flex-content "two-thirds operator-example" %}}

SELECT 6 / 3

{{% /flex-content %}} {{% flex-content "third operator-example" %}}

int64(6) / int64(3)
2

{{% /flex-content %}} {{< /flex >}}

%

The % (modulo) operator divides the left operand by the right operand and returns the remainder. If the left operand is not divisible by the right operand, it returns the left operand.

{{< flex >}} {{% flex-content "two-thirds operator-example" %}}

SELECT 8 % 3

{{% /flex-content %}} {{% flex-content "third operator-example" %}}

Int64(8) % Int64(3)
2

{{% /flex-content %}} {{< /flex >}}

{{< flex >}} {{% flex-content "two-thirds operator-example" %}}

SELECT 3 % 8

{{% /flex-content %}} {{% flex-content "third operator-example" %}}

Int64(3) % Int64(8)
3

{{% /flex-content %}} {{< /flex >}}