<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h3 class="title">
      <a name="arithmetic-functions">
      </a>
      14.6.1 Arithmetic Operators
     </h3>
    </div>
   </div>
  </div>
  <a class="indexterm" name="idm46045206456608">
  </a>
  <div class="table">
   <a name="idm46045206455120">
   </a>
   <p class="title">
    <b>
     Table 14.9 Arithmetic Operators
    </b>
   </p>
   <div class="table-contents">
    <table frame="box" rules="all" summary="A reference that lists arithmetic operators.">
     <colgroup>
      <col style="width: 28%"/>
      <col style="width: 71%"/>
     </colgroup>
     <thead>
      <tr>
       <th>
        Name
       </th>
       <th>
        Description
       </th>
      </tr>
     </thead>
     <tbody>
      <tr>
       <td>
        <a class="link" href="arithmetic-functions.html#operator_mod">
         <code class="literal">
          %
         </code>
         ,
         <code class="literal">
          MOD
         </code>
        </a>
       </td>
       <td>
        Modulo operator
       </td>
      </tr>
      <tr>
       <td>
        <a class="link" href="arithmetic-functions.html#operator_times">
         <code class="literal">
          *
         </code>
        </a>
       </td>
       <td>
        Multiplication operator
       </td>
      </tr>
      <tr>
       <td>
        <a class="link" href="arithmetic-functions.html#operator_plus">
         <code class="literal">
          +
         </code>
        </a>
       </td>
       <td>
        Addition operator
       </td>
      </tr>
      <tr>
       <td>
        <a class="link" href="arithmetic-functions.html#operator_minus">
         <code class="literal">
          -
         </code>
        </a>
       </td>
       <td>
        Minus operator
       </td>
      </tr>
      <tr>
       <td>
        <a class="link" href="arithmetic-functions.html#operator_unary-minus">
         <code class="literal">
          -
         </code>
        </a>
       </td>
       <td>
        Change the sign of the argument
       </td>
      </tr>
      <tr>
       <td>
        <a class="link" href="arithmetic-functions.html#operator_divide">
         <code class="literal">
          /
         </code>
        </a>
       </td>
       <td>
        Division operator
       </td>
      </tr>
      <tr>
       <td>
        <a class="link" href="arithmetic-functions.html#operator_div">
         <code class="literal">
          DIV
         </code>
        </a>
       </td>
       <td>
        Integer division
       </td>
      </tr>
     </tbody>
    </table>
   </div>
  </div>
  <br class="table-break"/>
  <p>
   The usual arithmetic operators are available. The result is
        determined according to the following rules:
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      In the case of
      <a class="link" href="arithmetic-functions.html#operator_minus">
       <code class="literal">
        -
       </code>
      </a>
      ,
      <a class="link" href="arithmetic-functions.html#operator_plus">
       <code class="literal">
        +
       </code>
      </a>
      , and
      <a class="link" href="arithmetic-functions.html#operator_times">
       <code class="literal">
        *
       </code>
      </a>
      , the result
            is calculated with
      <a class="link" href="integer-types.html" title="13.1.2 Integer Types (Exact Value) - INTEGER, INT, SMALLINT, TINYINT, MEDIUMINT, BIGINT">
       <code class="literal">
        BIGINT
       </code>
      </a>
      (64-bit) precision if both operands are integers.
     </p>
    </li>
    <li class="listitem">
     <p>
      If both operands are integers and any of them are unsigned,
            the result is an unsigned integer. For subtraction, if the
      <a class="link" href="sql-mode.html#sqlmode_no_unsigned_subtraction">
       <code class="literal">
        NO_UNSIGNED_SUBTRACTION
       </code>
      </a>
      SQL mode is enabled, the result is signed even if any
            operand is unsigned.
     </p>
    </li>
    <li class="listitem">
     <p>
      If any of the operands of a
      <a class="link" href="arithmetic-functions.html#operator_plus">
       <code class="literal">
        +
       </code>
      </a>
      ,
      <a class="link" href="arithmetic-functions.html#operator_minus">
       <code class="literal">
        -
       </code>
      </a>
      ,
      <a class="link" href="arithmetic-functions.html#operator_divide">
       <code class="literal">
        /
       </code>
      </a>
      ,
      <a class="link" href="arithmetic-functions.html#operator_times">
       <code class="literal">
        *
       </code>
      </a>
      ,
      <a class="link" href="arithmetic-functions.html#operator_mod">
       <code class="literal">
        %
       </code>
      </a>
      is a real or
            string value, the precision of the result is the precision
            of the operand with the maximum precision.
     </p>
    </li>
    <li class="listitem">
     <p>
      In division performed with
      <a class="link" href="arithmetic-functions.html#operator_divide">
       <code class="literal">
        /
       </code>
      </a>
      , the scale
            of the result when using two exact-value operands is the
            scale of the first operand plus the value of the
      <a class="link" href="server-system-variables.html#sysvar_div_precision_increment">
       <code class="literal">
        div_precision_increment
       </code>
      </a>
      system variable (which is 4 by default). For example, the
            result of the expression
      <code class="literal">
       5.05 / 0.014
      </code>
      has
            a scale of six decimal places
            (
      <code class="literal">
       360.714286
      </code>
      ).
     </p>
    </li>
   </ul>
  </div>
  <p>
   These rules are applied for each operation, such that nested
        calculations imply the precision of each component. Hence,
   <code class="literal">
    (14620 / 9432456) / (24250 / 9432456)
   </code>
   ,
        resolves first to
   <code class="literal">
    (0.0014) / (0.0026)
   </code>
   , with
        the final result having 8 decimal places
        (
   <code class="literal">
    0.60288653
   </code>
   ).
  </p>
  <p>
   Because of these rules and the way they are applied, care should
        be taken to ensure that components and subcomponents of a
        calculation use the appropriate level of precision. See
   <a class="xref" href="cast-functions.html" title="14.10 Cast Functions and Operators">
    Section 14.10, “Cast Functions and Operators”
   </a>
   .
  </p>
  <p>
   For information about handling of overflow in numeric expression
        evaluation, see
   <a class="xref" href="out-of-range-and-overflow.html" title="13.1.7 Out-of-Range and Overflow Handling">
    Section 13.1.7, “Out-of-Range and Overflow Handling”
   </a>
   .
  </p>
  <p>
   Arithmetic operators apply to numbers. For other types of
        values, alternative operations may be available. For example, to
        add date values, use
   <a class="link" href="date-and-time-functions.html#function_date-add">
    <code class="literal">
     DATE_ADD()
    </code>
   </a>
   ;
        see
   <a class="xref" href="date-and-time-functions.html" title="14.7 Date and Time Functions">
    Section 14.7, “Date and Time Functions”
   </a>
   .
  </p>
  <a class="indexterm" name="idm46045206398384">
  </a>
  <a class="indexterm" name="idm46045206396896">
  </a>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <a name="operator_plus">
     </a>
     <p>
      <a class="link" href="arithmetic-functions.html#operator_plus">
       <code class="literal">
        +
       </code>
      </a>
     </p>
     <a class="indexterm" name="idm46045206391392">
     </a>
     <a class="indexterm" name="idm46045206390320">
     </a>
     <p>
      Addition:
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa47315316"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token number">3</span><span class="token operator">+</span><span class="token number">5</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token number">8</span></code></pre>
     </div>
    </li>
    <li class="listitem">
     <a name="operator_minus">
     </a>
     <p>
      <a class="link" href="arithmetic-functions.html#operator_minus">
       <code class="literal">
        -
       </code>
      </a>
     </p>
     <a class="indexterm" name="idm46045206380656">
     </a>
     <a class="indexterm" name="idm46045206379584">
     </a>
     <p>
      Subtraction:
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa1268715"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token number">3</span><span class="token operator">-</span><span class="token number">5</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token operator">-</span><span class="token number">2</span></code></pre>
     </div>
    </li>
    <li class="listitem">
     <a name="operator_unary-minus">
     </a>
     <p>
      <a class="link" href="arithmetic-functions.html#operator_unary-minus">
       <code class="literal">
        -
       </code>
      </a>
     </p>
     <a class="indexterm" name="idm46045206369920">
     </a>
     <a class="indexterm" name="idm46045206368848">
     </a>
     <a class="indexterm" name="idm46045206367360">
     </a>
     <p>
      Unary minus. This operator changes the sign of the operand.
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa15067427"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token operator">-</span> <span class="token number">2</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token operator">-</span><span class="token number">2</span></code></pre>
     </div>
     <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
      <div class="admon-title">
       Note
      </div>
      <p>
       If this operator is used with a
       <a class="link" href="integer-types.html" title="13.1.2 Integer Types (Exact Value) - INTEGER, INT, SMALLINT, TINYINT, MEDIUMINT, BIGINT">
        <code class="literal">
         BIGINT
        </code>
       </a>
       , the return value is
              also a
       <a class="link" href="integer-types.html" title="13.1.2 Integer Types (Exact Value) - INTEGER, INT, SMALLINT, TINYINT, MEDIUMINT, BIGINT">
        <code class="literal">
         BIGINT
        </code>
       </a>
       . This means
              that you should avoid using
       <code class="literal">
        -
       </code>
       on
              integers that may have the value of
              −2
       <sup>
        63
       </sup>
       .
      </p>
     </div>
    </li>
    <li class="listitem">
     <a name="operator_times">
     </a>
     <p>
      <a class="link" href="arithmetic-functions.html#operator_times">
       <code class="literal">
        *
       </code>
      </a>
     </p>
     <a class="indexterm" name="idm46045206353072">
     </a>
     <a class="indexterm" name="idm46045206352000">
     </a>
     <p>
      Multiplication:
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa52637800"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token number">3</span><span class="token operator">*</span><span class="token number">5</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token number">15</span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token number">18014398509481984</span><span class="token operator">*</span><span class="token number">18014398509481984.0</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token number">324518553658426726783156020576256.0</span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token number">18014398509481984</span><span class="token operator">*</span><span class="token number">18014398509481984</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token keyword">out</span><span class="token operator">-</span><span class="token keyword">of</span><span class="token operator">-</span><span class="token keyword">range</span> <span class="token keyword">error</span></code></pre>
     </div>
     <p>
      The last expression produces an error because the result of
            the integer multiplication exceeds the 64-bit range of
      <a class="link" href="integer-types.html" title="13.1.2 Integer Types (Exact Value) - INTEGER, INT, SMALLINT, TINYINT, MEDIUMINT, BIGINT">
       <code class="literal">
        BIGINT
       </code>
      </a>
      calculations. (See
      <a class="xref" href="numeric-types.html" title="13.1 Numeric Data Types">
       Section 13.1, “Numeric Data Types”
      </a>
      .)
     </p>
    </li>
    <li class="listitem">
     <a name="operator_divide">
     </a>
     <p>
      <a class="link" href="arithmetic-functions.html#operator_divide">
       <code class="literal">
        /
       </code>
      </a>
     </p>
     <a class="indexterm" name="idm46045206338144">
     </a>
     <a class="indexterm" name="idm46045206337072">
     </a>
     <p>
      Division:
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa96769063"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token number">3</span><span class="token operator">/</span><span class="token number">5</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token number">0.60</span></code></pre>
     </div>
     <p>
      Division by zero produces a
      <code class="literal">
       NULL
      </code>
      result:
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa42401793"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token number">102</span><span class="token operator">/</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token boolean">NULL</span></code></pre>
     </div>
     <p>
      A division is calculated with
      <a class="link" href="integer-types.html" title="13.1.2 Integer Types (Exact Value) - INTEGER, INT, SMALLINT, TINYINT, MEDIUMINT, BIGINT">
       <code class="literal">
        BIGINT
       </code>
      </a>
      arithmetic only if
            performed in a context where its result is converted to an
            integer.
     </p>
    </li>
    <li class="listitem">
     <a name="operator_div">
     </a>
     <p>
      <a class="link" href="arithmetic-functions.html#operator_div">
       <code class="literal">
        DIV
       </code>
      </a>
     </p>
     <a class="indexterm" name="idm46045206322832">
     </a>
     <p>
      Integer division. Discards from the division result any
            fractional part to the right of the decimal point.
     </p>
     <p>
      If either operand has a noninteger type, the operands are
            converted to
      <a class="link" href="fixed-point-types.html" title="13.1.3 Fixed-Point Types (Exact Value) - DECIMAL, NUMERIC">
       <code class="literal">
        DECIMAL
       </code>
      </a>
      and
            divided using
      <a class="link" href="fixed-point-types.html" title="13.1.3 Fixed-Point Types (Exact Value) - DECIMAL, NUMERIC">
       <code class="literal">
        DECIMAL
       </code>
      </a>
      arithmetic before converting the result to
      <a class="link" href="integer-types.html" title="13.1.2 Integer Types (Exact Value) - INTEGER, INT, SMALLINT, TINYINT, MEDIUMINT, BIGINT">
       <code class="literal">
        BIGINT
       </code>
      </a>
      . If the result exceeds
      <code class="literal">
       BIGINT
      </code>
      range, an error occurs.
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa62040199"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token number">5</span> <span class="token operator">DIV</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">5</span> <span class="token operator">DIV</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">5</span> <span class="token operator">DIV</span> <span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">5</span> <span class="token operator">DIV</span> <span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span></code></pre>
     </div>
    </li>
    <li class="listitem">
     <a name="operator_mod">
     </a>
     <p>
      <a class="link" href="arithmetic-functions.html#operator_mod">
       <code class="literal">
        <em class="replaceable">
         <code>
          N
         </code>
        </em>
        %
        <em class="replaceable">
         <code>
          M
         </code>
        </em>
       </code>
      </a>
      ,
      <a class="link" href="arithmetic-functions.html#operator_mod">
       <code class="literal">
        <em class="replaceable">
         <code>
          N
         </code>
        </em>
        MOD
        <em class="replaceable">
         <code>
          M
         </code>
        </em>
       </code>
      </a>
     </p>
     <a class="indexterm" name="idm46045206305728">
     </a>
     <p>
      Modulo operation. Returns the remainder of
      <em class="replaceable">
       <code>
        N
       </code>
      </em>
      divided by
      <em class="replaceable">
       <code>
        M
       </code>
      </em>
      . For more information, see the
            description for the
      <a class="link" href="mathematical-functions.html#function_mod">
       <code class="literal">
        MOD()
       </code>
      </a>
      function in
      <a class="xref" href="mathematical-functions.html" title="14.6.2 Mathematical Functions">
       Section 14.6.2, “Mathematical Functions”
      </a>
      .
     </p>
    </li>
   </ul>
  </div>
 </div>
 <br/>
</div>
