All pages
Powered by GitBook
1 of 6

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Arithmetic Operators

Learn about arithmetic operators in MariaDB Server SQL. This section details how to perform mathematical calculations like addition, subtraction, multiplication, and division within your queries.

Subtraction Operator (-)

Syntax

Description

Subtraction. The operator is also used as the unary minus for changing sign.

If both operands are integers, the result is calculated with BIGINT precision. If either integer is unsigned, the result is also an unsigned integer, unless the NO_UNSIGNED_SUBTRACTION is enabled, in which case the result is always signed.

For real or string operands, the operand with the highest precision determines the result precision.

Examples

Unary minus:

See Also

This page is licensed: CC BY-SA / Gnu FDL

Addition Operator (+)

Syntax

Description

Addition.

If both operands are integers, the result is calculated with precision. If either integer is unsigned, the result is also an unsigned integer.

-
Operator Precedence
SQL_MODE
Type Conversion
Addition Operator (+)
Multiplication Operator (*)
Division Operator (/)

For real or string operands, the operand with the highest precision determines the result precision.

Examples

See Also

  • Type Conversion

  • Subtraction Operator (-)

  • Multiplication Operator (*)

  • Division Operator (/)

This page is licensed: GPLv2, originally from fill_help_tables.sql

BIGINT
SELECT 96-9;
+------+
| 96-9 |
+------+
|   87 |
+------+

SELECT 15-17;
+-------+
| 15-17 |
+-------+
|    -2 |
+-------+

SELECT 3.66 + 1.333;
+--------------+
| 3.66 + 1.333 |
+--------------+
|        4.993 |
+--------------+
SELECT - (3+5);
+---------+
| - (3+5) |
+---------+
|      -8 |
+---------+
+
SELECT 3+5;
+-----+
| 3+5 |
+-----+
|   8 |
+-----+
Operator Precedence

Multiplication Operator (*)

Syntax

*

Description

Multiplication operator.

Examples

See Also

This page is licensed: GPLv2, originally from

Operator Precedence
Type Conversion
Addition Operator (+)
Subtraction Operator (-)
Division Operator (/)
fill_help_tables.sql

Division Operator (/)

Syntax

Description

Division operator. Dividing by zero will return NULL. By default, returns four digits after the decimal. This is determined by the server system variable div_precision_increment which by default is four. It can be set from 0 to 30.

Dividing by zero returns NULL. If the default ERROR_ON_DIVISION_BY_ZERO is used, a division by zero also produces a warning.

Examples

Changing for the session from the default of four to six:

See Also

This page is licensed: GPLv2, originally from

SELECT 7*6;
+-----+
| 7*6 |
+-----+
|  42 |
+-----+

SELECT 1234567890*9876543210;
+-----------------------+
| 1234567890*9876543210 |
+-----------------------+
|  -6253480962446024716 |
+-----------------------+

SELECT 18014398509481984*18014398509481984.0;
+---------------------------------------+
| 18014398509481984*18014398509481984.0 |
+---------------------------------------+
|   324518553658426726783156020576256.0 |
+---------------------------------------+

SELECT 18014398509481984*18014398509481984;
+-------------------------------------+
| 18014398509481984*18014398509481984 |
+-------------------------------------+
|                                   0 |
+-------------------------------------+
/
Multiplication Operator (*)
  • truncate()

  • Operator Precedence

  • DIV function

  • SQL_MODE
    div_precision_increment
    Type Conversion
    Module operator (%)
    Addition Operator (+)
    Subtraction Operator (-)
    fill_help_tables.sql
    SELECT 4/5;
    +--------+
    | 4/5    |
    +--------+
    | 0.8000 |
    +--------+
    
    SELECT 300/(2-2);
    +-----------+
    | 300/(2-2) |
    +-----------+
    |      NULL |
    +-----------+
    
    SELECT 300/7;
    +---------+
    | 300/7   |
    +---------+
    | 42.8571 |
    +---------+
    SET div_precision_increment = 6;
    
    SELECT 300/7;
    +-----------+
    | 300/7     |
    +-----------+
    | 42.857143 |
    +-----------+
    
    SELECT 300/7;
    +-----------+
    | 300/7     |
    +-----------+
    | 42.857143 |
    +-----------+

    Modulo Operator (%)

    Syntax

    N % M

    Description

    Modulo operator. Returns the remainder of N divided by M. See also .

    Examples

    See Also

    This page is licensed: GPLv2, originally from

    MOD
    Operator Precedence
    fill_help_tables.sql
    SELECT 1042 % 50;
    +-----------+
    | 1042 % 50 |
    +-----------+
    |        42 |
    +-----------+