<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h3 class="title">
      <a name="aggregate-functions">
      </a>
      14.19.1 Aggregate Function Descriptions
     </h3>
    </div>
   </div>
  </div>
  <a class="indexterm" name="idm46045193394656">
  </a>
  <a class="indexterm" name="idm46045193393616">
  </a>
  <a class="indexterm" name="idm46045193392128">
  </a>
  <a class="indexterm" name="idm46045193391056">
  </a>
  <a class="indexterm" name="idm46045193389568">
  </a>
  <p>
   This section describes aggregate functions that operate on sets
        of values. They are often used with a
   <code class="literal">
    GROUP
        BY
   </code>
   clause to group values into subsets.
  </p>
  <div class="table">
   <a name="idm46045193386800">
   </a>
   <p class="title">
    <b>
     Table 14.29 Aggregate Functions
    </b>
   </p>
   <div class="table-contents">
    <table frame="box" rules="all" summary="A reference that lists aggregate functions.">
     <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="aggregate-functions.html#function_avg">
         <code class="literal">
          AVG()
         </code>
        </a>
       </td>
       <td>
        Return the average value of the argument
       </td>
      </tr>
      <tr>
       <td>
        <a class="link" href="aggregate-functions.html#function_bit-and">
         <code class="literal">
          BIT_AND()
         </code>
        </a>
       </td>
       <td>
        Return bitwise AND
       </td>
      </tr>
      <tr>
       <td>
        <a class="link" href="aggregate-functions.html#function_bit-or">
         <code class="literal">
          BIT_OR()
         </code>
        </a>
       </td>
       <td>
        Return bitwise OR
       </td>
      </tr>
      <tr>
       <td>
        <a class="link" href="aggregate-functions.html#function_bit-xor">
         <code class="literal">
          BIT_XOR()
         </code>
        </a>
       </td>
       <td>
        Return bitwise XOR
       </td>
      </tr>
      <tr>
       <td>
        <a class="link" href="aggregate-functions.html#function_count">
         <code class="literal">
          COUNT()
         </code>
        </a>
       </td>
       <td>
        Return a count of the number of rows returned
       </td>
      </tr>
      <tr>
       <td>
        <a class="link" href="aggregate-functions.html#function_count-distinct">
         <code class="literal">
          COUNT(DISTINCT)
         </code>
        </a>
       </td>
       <td>
        Return the count of a number of different values
       </td>
      </tr>
      <tr>
       <td>
        <a class="link" href="aggregate-functions.html#function_group-concat">
         <code class="literal">
          GROUP_CONCAT()
         </code>
        </a>
       </td>
       <td>
        Return a concatenated string
       </td>
      </tr>
      <tr>
       <td>
        <a class="link" href="aggregate-functions.html#function_json-arrayagg">
         <code class="literal">
          JSON_ARRAYAGG()
         </code>
        </a>
       </td>
       <td>
        Return result set as a single JSON array
       </td>
      </tr>
      <tr>
       <td>
        <a class="link" href="aggregate-functions.html#function_json-objectagg">
         <code class="literal">
          JSON_OBJECTAGG()
         </code>
        </a>
       </td>
       <td>
        Return result set as a single JSON object
       </td>
      </tr>
      <tr>
       <td>
        <a class="link" href="aggregate-functions.html#function_max">
         <code class="literal">
          MAX()
         </code>
        </a>
       </td>
       <td>
        Return the maximum value
       </td>
      </tr>
      <tr>
       <td>
        <a class="link" href="aggregate-functions.html#function_min">
         <code class="literal">
          MIN()
         </code>
        </a>
       </td>
       <td>
        Return the minimum value
       </td>
      </tr>
      <tr>
       <td>
        <a class="link" href="aggregate-functions.html#function_std">
         <code class="literal">
          STD()
         </code>
        </a>
       </td>
       <td>
        Return the population standard deviation
       </td>
      </tr>
      <tr>
       <td>
        <a class="link" href="aggregate-functions.html#function_stddev">
         <code class="literal">
          STDDEV()
         </code>
        </a>
       </td>
       <td>
        Return the population standard deviation
       </td>
      </tr>
      <tr>
       <td>
        <a class="link" href="aggregate-functions.html#function_stddev-pop">
         <code class="literal">
          STDDEV_POP()
         </code>
        </a>
       </td>
       <td>
        Return the population standard deviation
       </td>
      </tr>
      <tr>
       <td>
        <a class="link" href="aggregate-functions.html#function_stddev-samp">
         <code class="literal">
          STDDEV_SAMP()
         </code>
        </a>
       </td>
       <td>
        Return the sample standard deviation
       </td>
      </tr>
      <tr>
       <td>
        <a class="link" href="aggregate-functions.html#function_sum">
         <code class="literal">
          SUM()
         </code>
        </a>
       </td>
       <td>
        Return the sum
       </td>
      </tr>
      <tr>
       <td>
        <a class="link" href="aggregate-functions.html#function_var-pop">
         <code class="literal">
          VAR_POP()
         </code>
        </a>
       </td>
       <td>
        Return the population standard variance
       </td>
      </tr>
      <tr>
       <td>
        <a class="link" href="aggregate-functions.html#function_var-samp">
         <code class="literal">
          VAR_SAMP()
         </code>
        </a>
       </td>
       <td>
        Return the sample variance
       </td>
      </tr>
      <tr>
       <td>
        <a class="link" href="aggregate-functions.html#function_variance">
         <code class="literal">
          VARIANCE()
         </code>
        </a>
       </td>
       <td>
        Return the population standard variance
       </td>
      </tr>
     </tbody>
    </table>
   </div>
   <div class="table-contents">
    <table cellpadding="0" cellspacing="0" style="position: fixed; top: 0px; display: none; left: 401px; width: 490px;">
     <thead>
      <tr>
       <th style="width: 138.391px;">
        Name
       </th>
       <th style="width: 350.938px;">
        Description
       </th>
      </tr>
     </thead>
    </table>
   </div>
  </div>
  <br class="table-break"/>
  <p>
   Unless otherwise stated, aggregate functions ignore
   <code class="literal">
    NULL
   </code>
   values.
  </p>
  <p>
   If you use an aggregate function in a statement containing no
   <code class="literal">
    GROUP BY
   </code>
   clause, it is equivalent to grouping
        on all rows. For more information, see
   <a class="xref" href="group-by-handling.html" title="14.19.3 MySQL Handling of GROUP BY">
    Section 14.19.3, “MySQL Handling of GROUP BY”
   </a>
   .
  </p>
  <p>
   Most aggregate functions can be used as window functions. Those
        that can be used this way are signified in their syntax
        description by
   <code class="literal">
    [
    <em class="replaceable">
     <code>
      over_clause
     </code>
    </em>
    ]
   </code>
   ,
        representing an optional
   <code class="literal">
    OVER
   </code>
   clause.
   <em class="replaceable">
    <code>
     over_clause
    </code>
   </em>
   is described in
   <a class="xref" href="window-functions-usage.html" title="14.20.2 Window Function Concepts and Syntax">
    Section 14.20.2, “Window Function Concepts and Syntax”
   </a>
   , which also includes
        other information about window function usage.
  </p>
  <p>
   For numeric arguments, the variance and standard deviation
        functions return a
   <a class="link" href="floating-point-types.html" title="13.1.4 Floating-Point Types (Approximate Value) - FLOAT, DOUBLE">
    <code class="literal">
     DOUBLE
    </code>
   </a>
   value.
        The
   <a class="link" href="aggregate-functions.html#function_sum">
    <code class="literal">
     SUM()
    </code>
   </a>
   and
   <a class="link" href="aggregate-functions.html#function_avg">
    <code class="literal">
     AVG()
    </code>
   </a>
   functions return a
   <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>
   value for exact-value
        arguments (integer or
   <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 a
   <a class="link" href="floating-point-types.html" title="13.1.4 Floating-Point Types (Approximate Value) - FLOAT, DOUBLE">
    <code class="literal">
     DOUBLE
    </code>
   </a>
   value for
        approximate-value arguments
        (
   <a class="link" href="floating-point-types.html" title="13.1.4 Floating-Point Types (Approximate Value) - FLOAT, DOUBLE">
    <code class="literal">
     FLOAT
    </code>
   </a>
   or
   <a class="link" href="floating-point-types.html" title="13.1.4 Floating-Point Types (Approximate Value) - FLOAT, DOUBLE">
    <code class="literal">
     DOUBLE
    </code>
   </a>
   ).
  </p>
  <p>
   The
   <a class="link" href="aggregate-functions.html#function_sum">
    <code class="literal">
     SUM()
    </code>
   </a>
   and
   <a class="link" href="aggregate-functions.html#function_avg">
    <code class="literal">
     AVG()
    </code>
   </a>
   aggregate functions do not
        work with temporal values. (They convert the values to numbers,
        losing everything after the first nonnumeric character.) To work
        around this problem, convert to numeric units, perform the
        aggregate operation, and convert back to a temporal value.
        Examples:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa1035995"><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 keyword">SELECT</span> <span class="token function">SEC_TO_TIME</span><span class="token punctuation">(</span><span class="token function">SUM</span><span class="token punctuation">(</span><span class="token function">TIME_TO_SEC</span><span class="token punctuation">(</span><em class="replaceable">time_col</em><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">FROM</span> <em class="replaceable">tbl_name</em><span class="token punctuation">;</span>
<span class="token keyword">SELECT</span> <span class="token function">FROM_DAYS</span><span class="token punctuation">(</span><span class="token function">SUM</span><span class="token punctuation">(</span><span class="token function">TO_DAYS</span><span class="token punctuation">(</span><em class="replaceable">date_col</em><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">FROM</span> <em class="replaceable">tbl_name</em><span class="token punctuation">;</span></code></pre>
  </div>
  <p>
   Functions such as
   <a class="link" href="aggregate-functions.html#function_sum">
    <code class="literal">
     SUM()
    </code>
   </a>
   or
   <a class="link" href="aggregate-functions.html#function_avg">
    <code class="literal">
     AVG()
    </code>
   </a>
   that expect a numeric
        argument cast the argument to a number if necessary. For
   <a class="link" href="set.html" title="13.3.6 The SET Type">
    <code class="literal">
     SET
    </code>
   </a>
   or
   <a class="link" href="enum.html" title="13.3.5 The ENUM Type">
    <code class="literal">
     ENUM
    </code>
   </a>
   values, the cast operation
        causes the underlying numeric value to be used.
  </p>
  <p>
   The
   <a class="link" href="aggregate-functions.html#function_bit-and">
    <code class="literal">
     BIT_AND()
    </code>
   </a>
   ,
   <a class="link" href="aggregate-functions.html#function_bit-or">
    <code class="literal">
     BIT_OR()
    </code>
   </a>
   , and
   <a class="link" href="aggregate-functions.html#function_bit-xor">
    <code class="literal">
     BIT_XOR()
    </code>
   </a>
   aggregate functions
        perform bit operations.
  </p>
  <p>
   MySQL bit functions and operators permit binary string type
        arguments (
   <a class="link" href="binary-varbinary.html" title="13.3.3 The BINARY and VARBINARY Types">
    <code class="literal">
     BINARY
    </code>
   </a>
   ,
   <a class="link" href="binary-varbinary.html" title="13.3.3 The BINARY and VARBINARY Types">
    <code class="literal">
     VARBINARY
    </code>
   </a>
   , and the
   <a class="link" href="blob.html" title="13.3.4 The BLOB and TEXT Types">
    <code class="literal">
     BLOB
    </code>
   </a>
   types) and return a value of
        like type, which enables them to take arguments and produce
        return values larger than 64 bits. For discussion about argument
        evaluation and result types for bit operations, see the
        introductory discussion in
   <a class="xref" href="bit-functions.html" title="14.12 Bit Functions and Operators">
    Section 14.12, “Bit Functions and Operators”
   </a>
   .
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <a name="function_avg">
     </a>
     <p>
      <a class="link" href="aggregate-functions.html#function_avg">
       <code class="literal">
        AVG([DISTINCT]
        <em class="replaceable">
         <code>
          expr
         </code>
        </em>
        )
            [
        <em class="replaceable">
         <code>
          over_clause
         </code>
        </em>
        ]
       </code>
      </a>
     </p>
     <a class="indexterm" name="idm46045193279872">
     </a>
     <a class="indexterm" name="idm46045193278800">
     </a>
     <a class="indexterm" name="idm46045193277728">
     </a>
     <p>
      Returns the average value of
      <code class="literal">
       <em class="replaceable">
        <code>
         expr
        </code>
       </em>
      </code>
      . The
      <code class="literal">
       DISTINCT
      </code>
      option can be used to return the
            average of the distinct values of
      <em class="replaceable">
       <code>
        expr
       </code>
      </em>
      .
     </p>
     <p>
      If there are no matching rows,
      <a class="link" href="aggregate-functions.html#function_avg">
       <code class="literal">
        AVG()
       </code>
      </a>
      returns
      <code class="literal">
       NULL
      </code>
      . The function also returns
      <code class="literal">
       NULL
      </code>
      if
      <em class="replaceable">
       <code>
        expr
       </code>
      </em>
      is
      <code class="literal">
       NULL
      </code>
      .
     </p>
     <p>
      This function executes as a window function if
      <em class="replaceable">
       <code>
        over_clause
       </code>
      </em>
      is present.
      <em class="replaceable">
       <code>
        over_clause
       </code>
      </em>
      is as described in
      <a class="xref" href="window-functions-usage.html" title="14.20.2 Window Function Concepts and Syntax">
       Section 14.20.2, “Window Function Concepts and Syntax”
      </a>
      ; it cannot be used
            with
      <code class="literal">
       DISTINCT
      </code>
      .
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa86728563"><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> student_name<span class="token punctuation">,</span> <span class="token function">AVG</span><span class="token punctuation">(</span>test_score<span class="token punctuation">)</span>
       <span class="token keyword">FROM</span> student
       <span class="token keyword">GROUP</span> <span class="token keyword">BY</span> student_name<span class="token punctuation">;</span></code></pre>
     </div>
    </li>
    <li class="listitem">
     <a name="function_bit-and">
     </a>
     <p>
      <a class="link" href="aggregate-functions.html#function_bit-and">
       <code class="literal">
        BIT_AND(
        <em class="replaceable">
         <code>
          expr
         </code>
        </em>
        )
            [
        <em class="replaceable">
         <code>
          over_clause
         </code>
        </em>
        ]
       </code>
      </a>
     </p>
     <a class="indexterm" name="idm46045193256128">
     </a>
     <p>
      Returns the bitwise
      <code class="literal">
       AND
      </code>
      of all bits in
      <em class="replaceable">
       <code>
        expr
       </code>
      </em>
      .
     </p>
     <p>
      The result type depends on whether the function argument
            values are evaluated as binary strings or numbers:
     </p>
     <div class="itemizedlist">
      <ul class="itemizedlist" style="list-style-type: circle; ">
       <li class="listitem">
        <p>
         Binary-string evaluation occurs when the argument values
                have a binary string type, and the argument is not a
                hexadecimal literal, bit literal, or
         <code class="literal">
          NULL
         </code>
         literal. Numeric evaluation
                occurs otherwise, with argument value conversion to
                unsigned 64-bit integers as necessary.
        </p>
       </li>
       <li class="listitem">
        <p>
         Binary-string evaluation produces a binary string of the
                same length as the argument values. If argument values
                have unequal lengths, an
         <a class="ulink" href="/doc/mysql-errors/8.4/en/server-error-reference.html#error_er_invalid_bitwise_operands_size" target="_top">
          <code class="literal">
           ER_INVALID_BITWISE_OPERANDS_SIZE
          </code>
         </a>
         error occurs. If the argument size exceeds 511 bytes, an
         <a class="ulink" href="/doc/mysql-errors/8.4/en/server-error-reference.html#error_er_invalid_bitwise_aggregate_operands_size" target="_top">
          <code class="literal">
           ER_INVALID_BITWISE_AGGREGATE_OPERANDS_SIZE
          </code>
         </a>
         error occurs. Numeric evaluation produces an unsigned
                64-bit integer.
        </p>
       </li>
      </ul>
     </div>
     <p>
      If there are no matching rows,
      <a class="link" href="aggregate-functions.html#function_bit-and">
       <code class="literal">
        BIT_AND()
       </code>
      </a>
      returns a neutral
            value (all bits set to 1) having the same length as the
            argument values.
     </p>
     <p>
      <code class="literal">
       NULL
      </code>
      values do not affect the result
            unless all values are
      <code class="literal">
       NULL
      </code>
      . In that case,
            the result is a neutral value having the same length as the
            argument values.
     </p>
     <p>
      For more information discussion about argument evaluation
            and result types, see the introductory discussion in
      <a class="xref" href="bit-functions.html" title="14.12 Bit Functions and Operators">
       Section 14.12, “Bit Functions and Operators”
      </a>
      .
     </p>
     <p>
      If
      <a class="link" href="aggregate-functions.html#function_bit-and">
       <code class="literal">
        BIT_AND()
       </code>
      </a>
      is invoked from
            within the
      <a class="link" href="mysql.html" title="6.5.1 mysql — The MySQL Command-Line Client">
       <span class="command">
        <strong>
         mysql
        </strong>
       </span>
      </a>
      client, binary string
            results display using hexadecimal notation, depending on the
            value of the
      <a class="link" href="mysql-command-options.html#option_mysql_binary-as-hex">
       <code class="option">
        --binary-as-hex
       </code>
      </a>
      .
            For more information about that option, see
      <a class="xref" href="mysql.html" title="6.5.1 mysql — The MySQL Command-Line Client">
       Section 6.5.1, “mysql — The MySQL Command-Line Client”
      </a>
      .
     </p>
     <p>
      This function executes as a window function if
      <em class="replaceable">
       <code>
        over_clause
       </code>
      </em>
      is present.
      <em class="replaceable">
       <code>
        over_clause
       </code>
      </em>
      is as described in
      <a class="xref" href="window-functions-usage.html" title="14.20.2 Window Function Concepts and Syntax">
       Section 14.20.2, “Window Function Concepts and Syntax”
      </a>
      .
     </p>
    </li>
    <li class="listitem">
     <a name="function_bit-or">
     </a>
     <p>
      <a class="link" href="aggregate-functions.html#function_bit-or">
       <code class="literal">
        BIT_OR(
        <em class="replaceable">
         <code>
          expr
         </code>
        </em>
        )
            [
        <em class="replaceable">
         <code>
          over_clause
         </code>
        </em>
        ]
       </code>
      </a>
     </p>
     <a class="indexterm" name="idm46045193228320">
     </a>
     <p>
      Returns the bitwise
      <code class="literal">
       OR
      </code>
      of all bits in
      <em class="replaceable">
       <code>
        expr
       </code>
      </em>
      .
     </p>
     <p>
      The result type depends on whether the function argument
            values are evaluated as binary strings or numbers:
     </p>
     <div class="itemizedlist">
      <ul class="itemizedlist" style="list-style-type: circle; ">
       <li class="listitem">
        <p>
         Binary-string evaluation occurs when the argument values
                have a binary string type, and the argument is not a
                hexadecimal literal, bit literal, or
         <code class="literal">
          NULL
         </code>
         literal. Numeric evaluation
                occurs otherwise, with argument value conversion to
                unsigned 64-bit integers as necessary.
        </p>
       </li>
       <li class="listitem">
        <p>
         Binary-string evaluation produces a binary string of the
                same length as the argument values. If argument values
                have unequal lengths, an
         <a class="ulink" href="/doc/mysql-errors/8.4/en/server-error-reference.html#error_er_invalid_bitwise_operands_size" target="_top">
          <code class="literal">
           ER_INVALID_BITWISE_OPERANDS_SIZE
          </code>
         </a>
         error occurs. If the argument size exceeds 511 bytes, an
         <a class="ulink" href="/doc/mysql-errors/8.4/en/server-error-reference.html#error_er_invalid_bitwise_aggregate_operands_size" target="_top">
          <code class="literal">
           ER_INVALID_BITWISE_AGGREGATE_OPERANDS_SIZE
          </code>
         </a>
         error occurs. Numeric evaluation produces an unsigned
                64-bit integer.
        </p>
       </li>
      </ul>
     </div>
     <p>
      If there are no matching rows,
      <a class="link" href="aggregate-functions.html#function_bit-or">
       <code class="literal">
        BIT_OR()
       </code>
      </a>
      returns a neutral
            value (all bits set to 0) having the same length as the
            argument values.
     </p>
     <p>
      <code class="literal">
       NULL
      </code>
      values do not affect the result
            unless all values are
      <code class="literal">
       NULL
      </code>
      . In that case,
            the result is a neutral value having the same length as the
            argument values.
     </p>
     <p>
      For more information discussion about argument evaluation
            and result types, see the introductory discussion in
      <a class="xref" href="bit-functions.html" title="14.12 Bit Functions and Operators">
       Section 14.12, “Bit Functions and Operators”
      </a>
      .
     </p>
     <p>
      If
      <a class="link" href="aggregate-functions.html#function_bit-or">
       <code class="literal">
        BIT_OR()
       </code>
      </a>
      is invoked from
            within the
      <a class="link" href="mysql.html" title="6.5.1 mysql — The MySQL Command-Line Client">
       <span class="command">
        <strong>
         mysql
        </strong>
       </span>
      </a>
      client, binary string
            results display using hexadecimal notation, depending on the
            value of the
      <a class="link" href="mysql-command-options.html#option_mysql_binary-as-hex">
       <code class="option">
        --binary-as-hex
       </code>
      </a>
      .
            For more information about that option, see
      <a class="xref" href="mysql.html" title="6.5.1 mysql — The MySQL Command-Line Client">
       Section 6.5.1, “mysql — The MySQL Command-Line Client”
      </a>
      .
     </p>
     <p>
      This function executes as a window function if
      <em class="replaceable">
       <code>
        over_clause
       </code>
      </em>
      is present.
      <em class="replaceable">
       <code>
        over_clause
       </code>
      </em>
      is as described in
      <a class="xref" href="window-functions-usage.html" title="14.20.2 Window Function Concepts and Syntax">
       Section 14.20.2, “Window Function Concepts and Syntax”
      </a>
      .
     </p>
    </li>
    <li class="listitem">
     <a name="function_bit-xor">
     </a>
     <p>
      <a class="link" href="aggregate-functions.html#function_bit-xor">
       <code class="literal">
        BIT_XOR(
        <em class="replaceable">
         <code>
          expr
         </code>
        </em>
        )
            [
        <em class="replaceable">
         <code>
          over_clause
         </code>
        </em>
        ]
       </code>
      </a>
     </p>
     <a class="indexterm" name="idm46045193200512">
     </a>
     <p>
      Returns the bitwise
      <a class="link" href="logical-operators.html#operator_xor">
       <code class="literal">
        XOR
       </code>
      </a>
      of all
            bits in
      <em class="replaceable">
       <code>
        expr
       </code>
      </em>
      .
     </p>
     <p>
      The result type depends on whether the function argument
            values are evaluated as binary strings or numbers:
     </p>
     <div class="itemizedlist">
      <ul class="itemizedlist" style="list-style-type: circle; ">
       <li class="listitem">
        <p>
         Binary-string evaluation occurs when the argument values
                have a binary string type, and the argument is not a
                hexadecimal literal, bit literal, or
         <code class="literal">
          NULL
         </code>
         literal. Numeric evaluation
                occurs otherwise, with argument value conversion to
                unsigned 64-bit integers as necessary.
        </p>
       </li>
       <li class="listitem">
        <p>
         Binary-string evaluation produces a binary string of the
                same length as the argument values. If argument values
                have unequal lengths, an
         <a class="ulink" href="/doc/mysql-errors/8.4/en/server-error-reference.html#error_er_invalid_bitwise_operands_size" target="_top">
          <code class="literal">
           ER_INVALID_BITWISE_OPERANDS_SIZE
          </code>
         </a>
         error occurs. If the argument size exceeds 511 bytes, an
         <a class="ulink" href="/doc/mysql-errors/8.4/en/server-error-reference.html#error_er_invalid_bitwise_aggregate_operands_size" target="_top">
          <code class="literal">
           ER_INVALID_BITWISE_AGGREGATE_OPERANDS_SIZE
          </code>
         </a>
         error occurs. Numeric evaluation produces an unsigned
                64-bit integer.
        </p>
       </li>
      </ul>
     </div>
     <p>
      If there are no matching rows,
      <a class="link" href="aggregate-functions.html#function_bit-xor">
       <code class="literal">
        BIT_XOR()
       </code>
      </a>
      returns a neutral
            value (all bits set to 0) having the same length as the
            argument values.
     </p>
     <p>
      <code class="literal">
       NULL
      </code>
      values do not affect the result
            unless all values are
      <code class="literal">
       NULL
      </code>
      . In that case,
            the result is a neutral value having the same length as the
            argument values.
     </p>
     <p>
      For more information discussion about argument evaluation
            and result types, see the introductory discussion in
      <a class="xref" href="bit-functions.html" title="14.12 Bit Functions and Operators">
       Section 14.12, “Bit Functions and Operators”
      </a>
      .
     </p>
     <p>
      If
      <a class="link" href="aggregate-functions.html#function_bit-xor">
       <code class="literal">
        BIT_XOR()
       </code>
      </a>
      is invoked from
            within the
      <a class="link" href="mysql.html" title="6.5.1 mysql — The MySQL Command-Line Client">
       <span class="command">
        <strong>
         mysql
        </strong>
       </span>
      </a>
      client, binary string
            results display using hexadecimal notation, depending on the
            value of the
      <a class="link" href="mysql-command-options.html#option_mysql_binary-as-hex">
       <code class="option">
        --binary-as-hex
       </code>
      </a>
      .
            For more information about that option, see
      <a class="xref" href="mysql.html" title="6.5.1 mysql — The MySQL Command-Line Client">
       Section 6.5.1, “mysql — The MySQL Command-Line Client”
      </a>
      .
     </p>
     <p>
      This function executes as a window function if
      <em class="replaceable">
       <code>
        over_clause
       </code>
      </em>
      is present.
      <em class="replaceable">
       <code>
        over_clause
       </code>
      </em>
      is as described in
      <a class="xref" href="window-functions-usage.html" title="14.20.2 Window Function Concepts and Syntax">
       Section 14.20.2, “Window Function Concepts and Syntax”
      </a>
      .
     </p>
    </li>
    <li class="listitem">
     <a name="function_count">
     </a>
     <p>
      <a class="link" href="aggregate-functions.html#function_count">
       <code class="literal">
        COUNT(
        <em class="replaceable">
         <code>
          expr
         </code>
        </em>
        )
            [
        <em class="replaceable">
         <code>
          over_clause
         </code>
        </em>
        ]
       </code>
      </a>
     </p>
     <a class="indexterm" name="idm46045193172192">
     </a>
     <p>
      Returns a count of the number of non-
      <code class="literal">
       NULL
      </code>
      values of
      <em class="replaceable">
       <code>
        expr
       </code>
      </em>
      in the rows
            retrieved by a
      <a class="link" href="select.html" title="15.2.13 SELECT Statement">
       <code class="literal">
        SELECT
       </code>
      </a>
      statement. The result is 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>
      value.
     </p>
     <p>
      If there are no matching rows,
      <a class="link" href="aggregate-functions.html#function_count">
       <code class="literal">
        COUNT()
       </code>
      </a>
      returns
      <code class="literal">
       0
      </code>
      .
      <code class="literal">
       COUNT(NULL)
      </code>
      returns
            0.
     </p>
     <p>
      This function executes as a window function if
      <em class="replaceable">
       <code>
        over_clause
       </code>
      </em>
      is present.
      <em class="replaceable">
       <code>
        over_clause
       </code>
      </em>
      is as described in
      <a class="xref" href="window-functions-usage.html" title="14.20.2 Window Function Concepts and Syntax">
       Section 14.20.2, “Window Function Concepts and Syntax”
      </a>
      .
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa44365415"><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> student<span class="token punctuation">.</span>student_name<span class="token punctuation">,</span><span class="token function">COUNT</span><span class="token punctuation">(</span><span class="token operator">*</span><span class="token punctuation">)</span>
       <span class="token keyword">FROM</span> student<span class="token punctuation">,</span>course
       <span class="token keyword">WHERE</span> student<span class="token punctuation">.</span>student_id<span class="token operator">=</span>course<span class="token punctuation">.</span>student_id
       <span class="token keyword">GROUP</span> <span class="token keyword">BY</span> student_name<span class="token punctuation">;</span></code></pre>
     </div>
     <p>
      <a class="link" href="aggregate-functions.html#function_count">
       <code class="literal">
        COUNT(*)
       </code>
      </a>
      is somewhat
            different in that it returns a count of the number of rows
            retrieved, whether or not they contain
      <code class="literal">
       NULL
      </code>
      values.
     </p>
     <p>
      For transactional storage engines such as
      <code class="literal">
       InnoDB
      </code>
      , storing an exact row count is
            problematic. Multiple transactions may be occurring at the
            same time, each of which may affect the count.
     </p>
     <p>
      <code class="literal">
       InnoDB
      </code>
      does not keep an internal count of
            rows in a table because concurrent transactions might
      <span class="quote">
       “
       <span class="quote">
        see
       </span>
       ”
      </span>
      different numbers of rows at the same
            time. Consequently,
      <code class="literal">
       SELECT COUNT(*)
      </code>
      statements only count rows visible to the current
            transaction.
     </p>
     <p>
      <code class="literal">
       SELECT COUNT(*) FROM
       <em class="replaceable">
        <code>
         tbl_name
        </code>
       </em>
      </code>
      query
            performance for
      <code class="literal">
       InnoDB
      </code>
      tables is
            optimized for single-threaded workloads if there are no
            extra clauses such as
      <code class="literal">
       WHERE
      </code>
      or
      <code class="literal">
       GROUP BY
      </code>
      .
     </p>
     <p>
      <code class="literal">
       InnoDB
      </code>
      processes
      <code class="literal">
       SELECT
            COUNT(*)
      </code>
      statements by traversing the smallest
            available secondary index unless an index or optimizer hint
            directs the optimizer to use a different index. If a
            secondary index is not present,
      <code class="literal">
       InnoDB
      </code>
      processes
      <code class="literal">
       SELECT COUNT(*)
      </code>
      statements by
            scanning the clustered index.
     </p>
     <p>
      Processing of
      <code class="literal">
       SELECT COUNT(*)
      </code>
      statements
            takes some time if index records are not entirely in the
            buffer pool. For a faster count, create a counter table and
            let your application update it according to the inserts and
            deletes it does. However, this method may not scale well in
            situations where thousands of concurrent transactions are
            initiating updates to the same counter table. If an
            approximate row count is sufficient, use
      <a class="link" href="show-table-status.html" title="15.7.7.38 SHOW TABLE STATUS Statement">
       <code class="literal">
        SHOW TABLE STATUS
       </code>
      </a>
      .
     </p>
     <p>
      <code class="literal">
       InnoDB
      </code>
      handles
      <code class="literal">
       SELECT
            COUNT(*)
      </code>
      and
      <code class="literal">
       SELECT COUNT(1)
      </code>
      operations in the same way. There is no performance
            difference.
     </p>
     <p>
      For
      <code class="literal">
       MyISAM
      </code>
      tables,
      <a class="link" href="aggregate-functions.html#function_count">
       <code class="literal">
        COUNT(*)
       </code>
      </a>
      is optimized to
            return very quickly if the
      <a class="link" href="select.html" title="15.2.13 SELECT Statement">
       <code class="literal">
        SELECT
       </code>
      </a>
      retrieves from one
            table, no other columns are retrieved, and there is no
      <code class="literal">
       WHERE
      </code>
      clause. For example:
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting one-line language-sql"><div class="docs-select-all right" id="sa79757004"><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 function">COUNT</span><span class="token punctuation">(</span><span class="token operator">*</span><span class="token punctuation">)</span> <span class="token keyword">FROM</span> student<span class="token punctuation">;</span></code></pre>
     </div>
     <p>
      This optimization only applies to
      <code class="literal">
       MyISAM
      </code>
      tables, because an exact row count is stored for this
            storage engine and can be accessed very quickly.
      <code class="literal">
       COUNT(1)
      </code>
      is only subject to the same
            optimization if the first column is defined as
      <code class="literal">
       NOT
            NULL
      </code>
      .
     </p>
    </li>
    <li class="listitem">
     <a name="function_count-distinct">
     </a>
     <p>
      <a class="link" href="aggregate-functions.html#function_count">
       <code class="literal">
        COUNT(DISTINCT
        <em class="replaceable">
         <code>
          expr
         </code>
        </em>
        ,[
        <em class="replaceable">
         <code>
          expr
         </code>
        </em>
        ...])
       </code>
      </a>
     </p>
     <a class="indexterm" name="idm46045193121376">
     </a>
     <a class="indexterm" name="idm46045193120304">
     </a>
     <p>
      Returns a count of the number of rows with different
            non-
      <code class="literal">
       NULL
      </code>
      <em class="replaceable">
       <code>
        expr
       </code>
      </em>
      values.
     </p>
     <p>
      If there are no matching rows,
      <a class="link" href="aggregate-functions.html#function_count">
       <code class="literal">
        COUNT(DISTINCT)
       </code>
      </a>
      returns
      <code class="literal">
       0
      </code>
      .
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting one-line language-sql"><div class="docs-select-all right" id="sa84077697"><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 function">COUNT</span><span class="token punctuation">(</span><span class="token keyword">DISTINCT</span> results<span class="token punctuation">)</span> <span class="token keyword">FROM</span> student<span class="token punctuation">;</span></code></pre>
     </div>
     <p>
      In MySQL, you can obtain the number of distinct expression
            combinations that do not contain
      <code class="literal">
       NULL
      </code>
      by
            giving a list of expressions. In standard SQL, you would
            have to do a concatenation of all expressions inside
      <a class="link" href="aggregate-functions.html#function_count">
       <code class="literal">
        COUNT(DISTINCT ...)
       </code>
      </a>
      .
     </p>
    </li>
    <li class="listitem">
     <a name="function_group-concat">
     </a>
     <p>
      <a class="link" href="aggregate-functions.html#function_group-concat">
       <code class="literal">
        GROUP_CONCAT(
        <em class="replaceable">
         <code>
          expr
         </code>
        </em>
        )
       </code>
      </a>
     </p>
     <a class="indexterm" name="idm46045193103552">
     </a>
     <p>
      This function returns a string result with the concatenated
            non-
      <code class="literal">
       NULL
      </code>
      values from a group. It returns
      <code class="literal">
       NULL
      </code>
      if there are no
            non-
      <code class="literal">
       NULL
      </code>
      values. The full syntax is as
            follows:
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa43477493"><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 function">GROUP_CONCAT</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token keyword">DISTINCT</span><span class="token punctuation">]</span> <em class="replaceable">expr</em> <span class="token punctuation">[</span><span class="token punctuation">,</span><em class="replaceable">expr</em> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">]</span>
             <span class="token punctuation">[</span><span class="token keyword">ORDER</span> <span class="token keyword">BY</span> {<em class="replaceable">unsigned_integer</em> <span class="token operator">|</span> <em class="replaceable">col_name</em> <span class="token operator">|</span> <em class="replaceable">expr</em>}
                 <span class="token punctuation">[</span><span class="token keyword">ASC</span> <span class="token operator">|</span> <span class="token keyword">DESC</span><span class="token punctuation">]</span> <span class="token punctuation">[</span><span class="token punctuation">,</span><em class="replaceable">col_name</em> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">]</span>
             <span class="token punctuation">[</span><span class="token keyword">SEPARATOR</span> <em class="replaceable">str_val</em><span class="token punctuation">]</span><span class="token punctuation">)</span></code></pre>
     </div>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa51754168"><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> student_name<span class="token punctuation">,</span>
         <span class="token function">GROUP_CONCAT</span><span class="token punctuation">(</span>test_score<span class="token punctuation">)</span>
       <span class="token keyword">FROM</span> student
       <span class="token keyword">GROUP</span> <span class="token keyword">BY</span> student_name<span class="token punctuation">;</span></code></pre>
     </div>
     <p>
      Or:
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa9067881"><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> student_name<span class="token punctuation">,</span>
         <span class="token function">GROUP_CONCAT</span><span class="token punctuation">(</span><span class="token keyword">DISTINCT</span> test_score
                      <span class="token keyword">ORDER</span> <span class="token keyword">BY</span> test_score <span class="token keyword">DESC</span> <span class="token keyword">SEPARATOR</span> <span class="token string">' '</span><span class="token punctuation">)</span>
       <span class="token keyword">FROM</span> student
       <span class="token keyword">GROUP</span> <span class="token keyword">BY</span> student_name<span class="token punctuation">;</span></code></pre>
     </div>
     <p>
      In MySQL, you can get the concatenated values of expression
            combinations. To eliminate duplicate values, use the
      <code class="literal">
       DISTINCT
      </code>
      clause. To sort values in the
            result, use the
      <code class="literal">
       ORDER BY
      </code>
      clause. To sort
            in reverse order, add the
      <code class="literal">
       DESC
      </code>
      (descending) keyword to the name of the column you are
            sorting by in the
      <code class="literal">
       ORDER BY
      </code>
      clause. The
            default is ascending order; this may be specified explicitly
            using the
      <code class="literal">
       ASC
      </code>
      keyword. The default
            separator between values in a group is comma
            (
      <code class="literal">
       ,
      </code>
      ). To specify a separator explicitly,
            use
      <code class="literal">
       SEPARATOR
      </code>
      followed by the string
            literal value that should be inserted between group values.
            To eliminate the separator altogether, specify
      <code class="literal">
       SEPARATOR ''
      </code>
      .
     </p>
     <p>
      The result is truncated to the maximum length that is given
            by the
      <a class="link" href="server-system-variables.html#sysvar_group_concat_max_len">
       <code class="literal">
        group_concat_max_len
       </code>
      </a>
      system variable, which has a default value of 1024. The
            value can be set higher, although the effective maximum
            length of the return value is constrained by the value of
      <a class="link" href="server-system-variables.html#sysvar_max_allowed_packet">
       <code class="literal">
        max_allowed_packet
       </code>
      </a>
      . The
            syntax to change the value of
      <a class="link" href="server-system-variables.html#sysvar_group_concat_max_len">
       <code class="literal">
        group_concat_max_len
       </code>
      </a>
      at
            runtime is as follows, where
      <em class="replaceable">
       <code>
        val
       </code>
      </em>
      is an unsigned integer:
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting one-line language-sql"><div class="docs-select-all right" id="sa29151605"><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 keyword">SET</span> <span class="token punctuation">[</span><span class="token keyword">GLOBAL</span> <span class="token operator">|</span> <span class="token keyword">SESSION</span><span class="token punctuation">]</span> group_concat_max_len <span class="token operator">=</span> <em class="replaceable">val</em><span class="token punctuation">;</span></code></pre>
     </div>
     <p>
      The return value is a nonbinary or binary string, depending
            on whether the arguments are nonbinary or binary strings.
            The result type is
      <a class="link" href="blob.html" title="13.3.4 The BLOB and TEXT Types">
       <code class="literal">
        TEXT
       </code>
      </a>
      or
      <a class="link" href="blob.html" title="13.3.4 The BLOB and TEXT Types">
       <code class="literal">
        BLOB
       </code>
      </a>
      unless
      <a class="link" href="server-system-variables.html#sysvar_group_concat_max_len">
       <code class="literal">
        group_concat_max_len
       </code>
      </a>
      is
            less than or equal to 512, in which case the result type is
      <a class="link" href="char.html" title="13.3.2 The CHAR and VARCHAR Types">
       <code class="literal">
        VARCHAR
       </code>
      </a>
      or
      <a class="link" href="binary-varbinary.html" title="13.3.3 The BINARY and VARBINARY Types">
       <code class="literal">
        VARBINARY
       </code>
      </a>
      .
     </p>
     <p>
      If
      <a class="link" href="aggregate-functions.html#function_group-concat">
       <code class="literal">
        GROUP_CONCAT()
       </code>
      </a>
      is invoked
            from within the
      <a class="link" href="mysql.html" title="6.5.1 mysql — The MySQL Command-Line Client">
       <span class="command">
        <strong>
         mysql
        </strong>
       </span>
      </a>
      client, binary
            string results display using hexadecimal notation, depending
            on the value of the
      <a class="link" href="mysql-command-options.html#option_mysql_binary-as-hex">
       <code class="option">
        --binary-as-hex
       </code>
      </a>
      . For more
            information about that option, see
      <a class="xref" href="mysql.html" title="6.5.1 mysql — The MySQL Command-Line Client">
       Section 6.5.1, “mysql — The MySQL Command-Line Client”
      </a>
      .
     </p>
     <p>
      See also
      <a class="link" href="string-functions.html#function_concat">
       <code class="literal">
        CONCAT()
       </code>
      </a>
      and
      <a class="link" href="string-functions.html#function_concat-ws">
       <code class="literal">
        CONCAT_WS()
       </code>
      </a>
      :
      <a class="xref" href="string-functions.html" title="14.8 String Functions and Operators">
       Section 14.8, “String Functions and Operators”
      </a>
      .
     </p>
    </li>
    <li class="listitem">
     <a name="function_json-arrayagg">
     </a>
     <p>
      <a class="link" href="aggregate-functions.html#function_json-arrayagg">
       <code class="literal">
        JSON_ARRAYAGG(
        <em class="replaceable">
         <code>
          col_or_expr
         </code>
        </em>
        )
            [
        <em class="replaceable">
         <code>
          over_clause
         </code>
        </em>
        ]
       </code>
      </a>
     </p>
     <a class="indexterm" name="idm46045193051600">
     </a>
     <p>
      Aggregates a result set as a single
      <a class="link" href="json.html" title="13.5 The JSON Data Type">
       <code class="literal">
        JSON
       </code>
      </a>
      array whose elements
            consist of the rows. The order of elements in this array is
            undefined. The function acts on a column or an expression
            that evaluates to a single value. Returns
      <code class="literal">
       NULL
      </code>
      if the result contains no rows, or
            in the event of an error. If
      <em class="replaceable">
       <code>
        col_or_expr
       </code>
      </em>
      is
      <code class="literal">
       NULL
      </code>
      , the function returns an array of
            JSON
      <code class="literal">
       [null]
      </code>
      elements.
     </p>
     <p>
      This function executes as a window function if
      <em class="replaceable">
       <code>
        over_clause
       </code>
      </em>
      is present.
      <em class="replaceable">
       <code>
        over_clause
       </code>
      </em>
      is as described in
      <a class="xref" href="window-functions-usage.html" title="14.20.2 Window Function Concepts and Syntax">
       Section 14.20.2, “Window Function Concepts and Syntax”
      </a>
      .
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa26967611"><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> o_id<span class="token punctuation">,</span> <span class="token keyword">attribute</span><span class="token punctuation">,</span> <span class="token keyword">value</span> <span class="token keyword">FROM</span> t3<span class="token punctuation">;</span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> o_id <span class="token punctuation">|</span> attribute <span class="token punctuation">|</span> value <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span>    2 <span class="token punctuation">|</span> color     <span class="token punctuation">|</span> red   <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span>    2 <span class="token punctuation">|</span> fabric    <span class="token punctuation">|</span> silk  <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span>    3 <span class="token punctuation">|</span> color     <span class="token punctuation">|</span> green <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span>    3 <span class="token punctuation">|</span> shape     <span class="token punctuation">|</span> square<span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output">4 rows in set (0.00 sec)</span>

<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> o_id<span class="token punctuation">,</span> <span class="token function">JSON_ARRAYAGG</span><span class="token punctuation">(</span><span class="token keyword">attribute</span><span class="token punctuation">)</span> <span class="token keyword">AS</span> attributes
<span class="token prompt">    -&gt;</span> <span class="token keyword">FROM</span> t3 <span class="token keyword">GROUP</span> <span class="token keyword">BY</span> o_id<span class="token punctuation">;</span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> o_id <span class="token punctuation">|</span> attributes          <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span>    2 <span class="token punctuation">|</span> ["color", "fabric"] <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span>    3 <span class="token punctuation">|</span> ["color", "shape"]  <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output">2 rows in set (0.00 sec)</span></code></pre>
     </div>
    </li>
    <li class="listitem">
     <a name="function_json-objectagg">
     </a>
     <p>
      <a class="link" href="aggregate-functions.html#function_json-objectagg">
       <code class="literal">
        JSON_OBJECTAGG(
        <em class="replaceable">
         <code>
          key
         </code>
        </em>
        ,
        <em class="replaceable">
         <code>
          value
         </code>
        </em>
        )
            [
        <em class="replaceable">
         <code>
          over_clause
         </code>
        </em>
        ]
       </code>
      </a>
     </p>
     <a class="indexterm" name="idm46045193031632">
     </a>
     <p>
      Takes two column names or expressions as arguments, the
            first of these being used as a key and the second as a
            value, and returns a JSON object containing key-value pairs.
            Returns
      <code class="literal">
       NULL
      </code>
      if the result contains no
            rows, or in the event of an error. An error occurs if any
            key name is
      <code class="literal">
       NULL
      </code>
      or the number of
            arguments is not equal to 2.
     </p>
     <p>
      This function executes as a window function if
      <em class="replaceable">
       <code>
        over_clause
       </code>
      </em>
      is present.
      <em class="replaceable">
       <code>
        over_clause
       </code>
      </em>
      is as described in
      <a class="xref" href="window-functions-usage.html" title="14.20.2 Window Function Concepts and Syntax">
       Section 14.20.2, “Window Function Concepts and Syntax”
      </a>
      .
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa72317180"><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> o_id<span class="token punctuation">,</span> <span class="token keyword">attribute</span><span class="token punctuation">,</span> <span class="token keyword">value</span> <span class="token keyword">FROM</span> t3<span class="token punctuation">;</span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> o_id <span class="token punctuation">|</span> attribute <span class="token punctuation">|</span> value <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span>    2 <span class="token punctuation">|</span> color     <span class="token punctuation">|</span> red   <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span>    2 <span class="token punctuation">|</span> fabric    <span class="token punctuation">|</span> silk  <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span>    3 <span class="token punctuation">|</span> color     <span class="token punctuation">|</span> green <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span>    3 <span class="token punctuation">|</span> shape     <span class="token punctuation">|</span> square<span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output">4 rows in set (0.00 sec)</span>

<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> o_id<span class="token punctuation">,</span> <span class="token function">JSON_OBJECTAGG</span><span class="token punctuation">(</span><span class="token keyword">attribute</span><span class="token punctuation">,</span> <span class="token keyword">value</span><span class="token punctuation">)</span>
<span class="token prompt">    -&gt;</span> <span class="token keyword">FROM</span> t3 <span class="token keyword">GROUP</span> <span class="token keyword">BY</span> o_id<span class="token punctuation">;</span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> o_id <span class="token punctuation">|</span> JSON_OBJECTAGG(attribute, value)      <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span>    2 <span class="token punctuation">|</span> {"color": "red", "fabric": "silk"}    <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span>    3 <span class="token punctuation">|</span> {"color": "green", "shape": "square"} <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output">2 rows in set (0.00 sec)</span></code></pre>
     </div>
     <p>
      <b>
       Duplicate key handling.
      </b>
      When the result of this function is normalized, values
              having duplicate keys are discarded. In keeping with the
              MySQL
      <a class="link" href="json.html" title="13.5 The JSON Data Type">
       <code class="literal">
        JSON
       </code>
      </a>
      data type
              specification that does not permit duplicate keys, only
              the last value encountered is used with that key in the
              returned object (
      <span class="quote">
       “
       <span class="quote">
        last duplicate key wins
       </span>
       ”
      </span>
      ).
              This means that the result of using this function on
              columns from a
      <code class="literal">
       SELECT
      </code>
      can depend on the
              order in which the rows are returned, which is not
              guaranteed.
     </p>
     <p>
      When used as a window function, if there are duplicate keys
            within a frame, only the last value for the key is present
            in the result. The value for the key from the last row in
            the frame is deterministic if the
      <code class="literal">
       ORDER
            BY
      </code>
      specification guarantees that the values have a
            specific order. If not, the resulting value of the key is
            nondeterministic.
     </p>
     <p>
      Consider the following:
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa86411192"><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">CREATE</span> <span class="token keyword">TABLE</span> t<span class="token punctuation">(</span>c <span class="token datatype">VARCHAR</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">,</span> i <span class="token datatype">INT</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token output">Query OK, 0 rows affected (0.33 sec)</span>

<span class="token prompt">mysql&gt;</span> <span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> t <span class="token keyword">VALUES</span> <span class="token punctuation">(</span><span class="token string">'key'</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token string">'key'</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token string">'key'</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token output">Query OK, 3 rows affected (0.10 sec)</span>
<span class="token output">Records: 3  Duplicates: 0  Warnings: 0</span>

<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> c<span class="token punctuation">,</span> i <span class="token keyword">FROM</span> t<span class="token punctuation">;</span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> c    <span class="token punctuation">|</span> i    <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> key  <span class="token punctuation">|</span>    3 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> key  <span class="token punctuation">|</span>    4 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> key  <span class="token punctuation">|</span>    5 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output">3 rows in set (0.00 sec)</span>

<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token function">JSON_OBJECTAGG</span><span class="token punctuation">(</span>c<span class="token punctuation">,</span> i<span class="token punctuation">)</span> <span class="token keyword">FROM</span> t<span class="token punctuation">;</span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> JSON_OBJECTAGG(c, i) <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> {"key": 5}           <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output">1 row in set (0.00 sec)</span>

<span class="token prompt">mysql&gt;</span> <span class="token keyword">DELETE</span> <span class="token keyword">FROM</span> t<span class="token punctuation">;</span>
<span class="token output">Query OK, 3 rows affected (0.08 sec)</span>

<span class="token prompt">mysql&gt;</span> <span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> t <span class="token keyword">VALUES</span> <span class="token punctuation">(</span><span class="token string">'key'</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token string">'key'</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token string">'key'</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token output">Query OK, 3 rows affected (0.06 sec)</span>
<span class="token output">Records: 3  Duplicates: 0  Warnings: 0</span>

<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> c<span class="token punctuation">,</span> i <span class="token keyword">FROM</span> t<span class="token punctuation">;</span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> c    <span class="token punctuation">|</span> i    <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> key  <span class="token punctuation">|</span>    3 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> key  <span class="token punctuation">|</span>    5 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> key  <span class="token punctuation">|</span>    4 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output">3 rows in set (0.00 sec)</span>

<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token function">JSON_OBJECTAGG</span><span class="token punctuation">(</span>c<span class="token punctuation">,</span> i<span class="token punctuation">)</span> <span class="token keyword">FROM</span> t<span class="token punctuation">;</span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> JSON_OBJECTAGG(c, i) <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> {"key": 4}           <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output">1 row in set (0.00 sec)</span></code></pre>
     </div>
     <p>
      The key chosen from the last query is nondeterministic. If
            the query does not use
      <code class="literal">
       GROUP BY
      </code>
      (which
            usually imposes its own ordering regardless) and you prefer
            a particular key ordering, you can invoke
      <code class="literal">
       JSON_OBJECTAGG()
      </code>
      as a window function by
            including an
      <code class="literal">
       OVER
      </code>
      clause with an
      <code class="literal">
       ORDER BY
      </code>
      specification to impose a
            particular order on frame rows. The following examples show
            what happens with and without
      <code class="literal">
       ORDER BY
      </code>
      for a few different frame specifications.
     </p>
     <p>
      Without
      <code class="literal">
       ORDER BY
      </code>
      , the frame is the entire
            partition:
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa60342813"><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 function">JSON_OBJECTAGG</span><span class="token punctuation">(</span>c<span class="token punctuation">,</span> i<span class="token punctuation">)</span>
       <span class="token keyword">OVER</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">AS</span> json_object <span class="token keyword">FROM</span> t<span class="token punctuation">;</span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> json_object <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> {"key": 4}  <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> {"key": 4}  <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> {"key": 4}  <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span></code></pre>
     </div>
     <p>
      With
      <code class="literal">
       ORDER BY
      </code>
      , where the frame is the
            default of
      <code class="literal">
       RANGE BETWEEN UNBOUNDED PRECEDING AND
            CURRENT ROW
      </code>
      (in both ascending and descending
            order):
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa29719008"><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 function">JSON_OBJECTAGG</span><span class="token punctuation">(</span>c<span class="token punctuation">,</span> i<span class="token punctuation">)</span>
       <span class="token keyword">OVER</span> <span class="token punctuation">(</span><span class="token keyword">ORDER</span> <span class="token keyword">BY</span> i<span class="token punctuation">)</span> <span class="token keyword">AS</span> json_object <span class="token keyword">FROM</span> t<span class="token punctuation">;</span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> json_object <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> {"key": 3}  <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> {"key": 4}  <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> {"key": 5}  <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token function">JSON_OBJECTAGG</span><span class="token punctuation">(</span>c<span class="token punctuation">,</span> i<span class="token punctuation">)</span>
       <span class="token keyword">OVER</span> <span class="token punctuation">(</span><span class="token keyword">ORDER</span> <span class="token keyword">BY</span> i <span class="token keyword">DESC</span><span class="token punctuation">)</span> <span class="token keyword">AS</span> json_object <span class="token keyword">FROM</span> t<span class="token punctuation">;</span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> json_object <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> {"key": 5}  <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> {"key": 4}  <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> {"key": 3}  <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span></code></pre>
     </div>
     <p>
      With
      <code class="literal">
       ORDER BY
      </code>
      and an explicit frame of
            the entire partition:
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa46423479"><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 function">JSON_OBJECTAGG</span><span class="token punctuation">(</span>c<span class="token punctuation">,</span> i<span class="token punctuation">)</span>
       <span class="token keyword">OVER</span> <span class="token punctuation">(</span><span class="token keyword">ORDER</span> <span class="token keyword">BY</span> i
            <span class="token keyword">ROWS</span> <span class="token operator">BETWEEN</span> <span class="token keyword">UNBOUNDED</span> <span class="token keyword">PRECEDING</span> <span class="token operator">AND</span> <span class="token keyword">UNBOUNDED</span> <span class="token keyword">FOLLOWING</span><span class="token punctuation">)</span>
        <span class="token keyword">AS</span> json_object
       <span class="token keyword">FROM</span> t<span class="token punctuation">;</span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> json_object <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> {"key": 5}  <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> {"key": 5}  <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> {"key": 5}  <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span></code></pre>
     </div>
     <p>
      To return a particular key value (such as the smallest or
            largest), include a
      <code class="literal">
       LIMIT
      </code>
      clause in the
            appropriate query. For example:
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa4906526"><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 function">JSON_OBJECTAGG</span><span class="token punctuation">(</span>c<span class="token punctuation">,</span> i<span class="token punctuation">)</span>
       <span class="token keyword">OVER</span> <span class="token punctuation">(</span><span class="token keyword">ORDER</span> <span class="token keyword">BY</span> i<span class="token punctuation">)</span> <span class="token keyword">AS</span> json_object <span class="token keyword">FROM</span> t <span class="token keyword">LIMIT</span> <span class="token number">1</span><span class="token punctuation">;</span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> json_object <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> {"key": 3}  <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token function">JSON_OBJECTAGG</span><span class="token punctuation">(</span>c<span class="token punctuation">,</span> i<span class="token punctuation">)</span>
       <span class="token keyword">OVER</span> <span class="token punctuation">(</span><span class="token keyword">ORDER</span> <span class="token keyword">BY</span> i <span class="token keyword">DESC</span><span class="token punctuation">)</span> <span class="token keyword">AS</span> json_object <span class="token keyword">FROM</span> t <span class="token keyword">LIMIT</span> <span class="token number">1</span><span class="token punctuation">;</span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> json_object <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> {"key": 5}  <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span></code></pre>
     </div>
     <p>
      See
      <a class="xref" href="json.html#json-normalization" title="Normalization, Merging, and Autowrapping of JSON Values">
       Normalization, Merging, and Autowrapping of JSON Values
      </a>
      , for additional
            information and examples.
     </p>
    </li>
    <li class="listitem">
     <a name="function_max">
     </a>
     <p>
      <a class="link" href="aggregate-functions.html#function_max">
       <code class="literal">
        MAX([DISTINCT]
        <em class="replaceable">
         <code>
          expr
         </code>
        </em>
        )
            [
        <em class="replaceable">
         <code>
          over_clause
         </code>
        </em>
        ]
       </code>
      </a>
     </p>
     <a class="indexterm" name="idm46045192973552">
     </a>
     <a class="indexterm" name="idm46045192972480">
     </a>
     <a class="indexterm" name="idm46045192971408">
     </a>
     <p>
      Returns the maximum value of
      <em class="replaceable">
       <code>
        expr
       </code>
      </em>
      .
      <a class="link" href="aggregate-functions.html#function_max">
       <code class="literal">
        MAX()
       </code>
      </a>
      may take a string
            argument; in such cases, it returns the maximum string
            value. See
      <a class="xref" href="mysql-indexes.html" title="10.3.1 How MySQL Uses Indexes">
       Section 10.3.1, “How MySQL Uses Indexes”
      </a>
      . The
      <code class="literal">
       DISTINCT
      </code>
      keyword can be used to find the
            maximum of the distinct values of
      <em class="replaceable">
       <code>
        expr
       </code>
      </em>
      , however, this produces the
            same result as omitting
      <code class="literal">
       DISTINCT
      </code>
      .
     </p>
     <p>
      If there are no matching rows, or if
      <em class="replaceable">
       <code>
        expr
       </code>
      </em>
      is
      <code class="literal">
       NULL
      </code>
      ,
      <a class="link" href="aggregate-functions.html#function_max">
       <code class="literal">
        MAX()
       </code>
      </a>
      returns
      <code class="literal">
       NULL
      </code>
      .
     </p>
     <p>
      This function executes as a window function if
      <em class="replaceable">
       <code>
        over_clause
       </code>
      </em>
      is present.
      <em class="replaceable">
       <code>
        over_clause
       </code>
      </em>
      is as described in
      <a class="xref" href="window-functions-usage.html" title="14.20.2 Window Function Concepts and Syntax">
       Section 14.20.2, “Window Function Concepts and Syntax”
      </a>
      ; it cannot be used
            with
      <code class="literal">
       DISTINCT
      </code>
      .
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa60546397"><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> student_name<span class="token punctuation">,</span> <span class="token function">MIN</span><span class="token punctuation">(</span>test_score<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">MAX</span><span class="token punctuation">(</span>test_score<span class="token punctuation">)</span>
       <span class="token keyword">FROM</span> student
       <span class="token keyword">GROUP</span> <span class="token keyword">BY</span> student_name<span class="token punctuation">;</span></code></pre>
     </div>
     <p>
      For
      <a class="link" href="aggregate-functions.html#function_max">
       <code class="literal">
        MAX()
       </code>
      </a>
      , MySQL currently
            compares
      <a class="link" href="enum.html" title="13.3.5 The ENUM Type">
       <code class="literal">
        ENUM
       </code>
      </a>
      and
      <a class="link" href="set.html" title="13.3.6 The SET Type">
       <code class="literal">
        SET
       </code>
      </a>
      columns by their string
            value rather than by the string's relative position in the
            set. This differs from how
      <code class="literal">
       ORDER BY
      </code>
      compares them.
     </p>
    </li>
    <li class="listitem">
     <a name="function_min">
     </a>
     <p>
      <a class="link" href="aggregate-functions.html#function_min">
       <code class="literal">
        MIN([DISTINCT]
        <em class="replaceable">
         <code>
          expr
         </code>
        </em>
        )
            [
        <em class="replaceable">
         <code>
          over_clause
         </code>
        </em>
        ]
       </code>
      </a>
     </p>
     <a class="indexterm" name="idm46045192943280">
     </a>
     <a class="indexterm" name="idm46045192942208">
     </a>
     <a class="indexterm" name="idm46045192941136">
     </a>
     <p>
      Returns the minimum value of
      <em class="replaceable">
       <code>
        expr
       </code>
      </em>
      .
      <a class="link" href="aggregate-functions.html#function_min">
       <code class="literal">
        MIN()
       </code>
      </a>
      may take a string
            argument; in such cases, it returns the minimum string
            value. See
      <a class="xref" href="mysql-indexes.html" title="10.3.1 How MySQL Uses Indexes">
       Section 10.3.1, “How MySQL Uses Indexes”
      </a>
      . The
      <code class="literal">
       DISTINCT
      </code>
      keyword can be used to find the
            minimum of the distinct values of
      <em class="replaceable">
       <code>
        expr
       </code>
      </em>
      , however, this produces the
            same result as omitting
      <code class="literal">
       DISTINCT
      </code>
      .
     </p>
     <p>
      If there are no matching rows, or if
      <em class="replaceable">
       <code>
        expr
       </code>
      </em>
      is
      <code class="literal">
       NULL
      </code>
      ,
      <a class="link" href="aggregate-functions.html#function_min">
       <code class="literal">
        MIN()
       </code>
      </a>
      returns
      <code class="literal">
       NULL
      </code>
      .
     </p>
     <p>
      This function executes as a window function if
      <em class="replaceable">
       <code>
        over_clause
       </code>
      </em>
      is present.
      <em class="replaceable">
       <code>
        over_clause
       </code>
      </em>
      is as described in
      <a class="xref" href="window-functions-usage.html" title="14.20.2 Window Function Concepts and Syntax">
       Section 14.20.2, “Window Function Concepts and Syntax”
      </a>
      ; it cannot be used
            with
      <code class="literal">
       DISTINCT
      </code>
      .
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa60326184"><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> student_name<span class="token punctuation">,</span> <span class="token function">MIN</span><span class="token punctuation">(</span>test_score<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">MAX</span><span class="token punctuation">(</span>test_score<span class="token punctuation">)</span>
       <span class="token keyword">FROM</span> student
       <span class="token keyword">GROUP</span> <span class="token keyword">BY</span> student_name<span class="token punctuation">;</span></code></pre>
     </div>
     <p>
      For
      <a class="link" href="aggregate-functions.html#function_min">
       <code class="literal">
        MIN()
       </code>
      </a>
      , MySQL currently
            compares
      <a class="link" href="enum.html" title="13.3.5 The ENUM Type">
       <code class="literal">
        ENUM
       </code>
      </a>
      and
      <a class="link" href="set.html" title="13.3.6 The SET Type">
       <code class="literal">
        SET
       </code>
      </a>
      columns by their string
            value rather than by the string's relative position in the
            set. This differs from how
      <code class="literal">
       ORDER BY
      </code>
      compares them.
     </p>
    </li>
    <li class="listitem">
     <a name="function_std">
     </a>
     <p>
      <a class="link" href="aggregate-functions.html#function_std">
       <code class="literal">
        STD(
        <em class="replaceable">
         <code>
          expr
         </code>
        </em>
        )
            [
        <em class="replaceable">
         <code>
          over_clause
         </code>
        </em>
        ]
       </code>
      </a>
     </p>
     <a class="indexterm" name="idm46045192913088">
     </a>
     <p>
      Returns the population standard deviation of
      <em class="replaceable">
       <code>
        expr
       </code>
      </em>
      .
      <a class="link" href="aggregate-functions.html#function_std">
       <code class="literal">
        STD()
       </code>
      </a>
      is a synonym for the
            standard SQL function
      <a class="link" href="aggregate-functions.html#function_stddev-pop">
       <code class="literal">
        STDDEV_POP()
       </code>
      </a>
      , provided as a
            MySQL extension.
     </p>
     <p>
      If there are no matching rows, or if
      <em class="replaceable">
       <code>
        expr
       </code>
      </em>
      is
      <code class="literal">
       NULL
      </code>
      ,
      <a class="link" href="aggregate-functions.html#function_std">
       <code class="literal">
        STD()
       </code>
      </a>
      returns
      <code class="literal">
       NULL
      </code>
      .
     </p>
     <p>
      This function executes as a window function if
      <em class="replaceable">
       <code>
        over_clause
       </code>
      </em>
      is present.
      <em class="replaceable">
       <code>
        over_clause
       </code>
      </em>
      is as described in
      <a class="xref" href="window-functions-usage.html" title="14.20.2 Window Function Concepts and Syntax">
       Section 14.20.2, “Window Function Concepts and Syntax”
      </a>
      .
     </p>
    </li>
    <li class="listitem">
     <a name="function_stddev">
     </a>
     <p>
      <a class="link" href="aggregate-functions.html#function_stddev">
       <code class="literal">
        STDDEV(
        <em class="replaceable">
         <code>
          expr
         </code>
        </em>
        )
            [
        <em class="replaceable">
         <code>
          over_clause
         </code>
        </em>
        ]
       </code>
      </a>
     </p>
     <a class="indexterm" name="idm46045192896016">
     </a>
     <a class="indexterm" name="idm46045192894944">
     </a>
     <a class="indexterm" name="idm46045192893872">
     </a>
     <p>
      Returns the population standard deviation of
      <em class="replaceable">
       <code>
        expr
       </code>
      </em>
      .
      <a class="link" href="aggregate-functions.html#function_stddev">
       <code class="literal">
        STDDEV()
       </code>
      </a>
      is a synonym for the
            standard SQL function
      <a class="link" href="aggregate-functions.html#function_stddev-pop">
       <code class="literal">
        STDDEV_POP()
       </code>
      </a>
      , provided for
            compatibility with Oracle.
     </p>
     <p>
      If there are no matching rows, or if
      <em class="replaceable">
       <code>
        expr
       </code>
      </em>
      is
      <code class="literal">
       NULL
      </code>
      ,
      <a class="link" href="aggregate-functions.html#function_stddev">
       <code class="literal">
        STDDEV()
       </code>
      </a>
      returns
      <code class="literal">
       NULL
      </code>
      .
     </p>
     <p>
      This function executes as a window function if
      <em class="replaceable">
       <code>
        over_clause
       </code>
      </em>
      is present.
      <em class="replaceable">
       <code>
        over_clause
       </code>
      </em>
      is as described in
      <a class="xref" href="window-functions-usage.html" title="14.20.2 Window Function Concepts and Syntax">
       Section 14.20.2, “Window Function Concepts and Syntax”
      </a>
      .
     </p>
    </li>
    <li class="listitem">
     <a name="function_stddev-pop">
     </a>
     <p>
      <a class="link" href="aggregate-functions.html#function_stddev-pop">
       <code class="literal">
        STDDEV_POP(
        <em class="replaceable">
         <code>
          expr
         </code>
        </em>
        )
            [
        <em class="replaceable">
         <code>
          over_clause
         </code>
        </em>
        ]
       </code>
      </a>
     </p>
     <a class="indexterm" name="idm46045192876368">
     </a>
     <p>
      Returns the population standard deviation of
      <em class="replaceable">
       <code>
        expr
       </code>
      </em>
      (the square root of
      <a class="link" href="aggregate-functions.html#function_var-pop">
       <code class="literal">
        VAR_POP()
       </code>
      </a>
      ). You can also use
      <a class="link" href="aggregate-functions.html#function_std">
       <code class="literal">
        STD()
       </code>
      </a>
      or
      <a class="link" href="aggregate-functions.html#function_stddev">
       <code class="literal">
        STDDEV()
       </code>
      </a>
      , which are
            equivalent but not standard SQL.
     </p>
     <p>
      If there are no matching rows, or if
      <em class="replaceable">
       <code>
        expr
       </code>
      </em>
      is
      <code class="literal">
       NULL
      </code>
      ,
      <a class="link" href="aggregate-functions.html#function_stddev-pop">
       <code class="literal">
        STDDEV_POP()
       </code>
      </a>
      returns
      <code class="literal">
       NULL
      </code>
      .
     </p>
     <p>
      This function executes as a window function if
      <em class="replaceable">
       <code>
        over_clause
       </code>
      </em>
      is present.
      <em class="replaceable">
       <code>
        over_clause
       </code>
      </em>
      is as described in
      <a class="xref" href="window-functions-usage.html" title="14.20.2 Window Function Concepts and Syntax">
       Section 14.20.2, “Window Function Concepts and Syntax”
      </a>
      .
     </p>
    </li>
    <li class="listitem">
     <a name="function_stddev-samp">
     </a>
     <p>
      <a class="link" href="aggregate-functions.html#function_stddev-samp">
       <code class="literal">
        STDDEV_SAMP(
        <em class="replaceable">
         <code>
          expr
         </code>
        </em>
        )
            [
        <em class="replaceable">
         <code>
          over_clause
         </code>
        </em>
        ]
       </code>
      </a>
     </p>
     <a class="indexterm" name="idm46045192858064">
     </a>
     <p>
      Returns the sample standard deviation of
      <em class="replaceable">
       <code>
        expr
       </code>
      </em>
      (the square root of
      <a class="link" href="aggregate-functions.html#function_var-samp">
       <code class="literal">
        VAR_SAMP()
       </code>
      </a>
      .
     </p>
     <p>
      If there are no matching rows, or if
      <em class="replaceable">
       <code>
        expr
       </code>
      </em>
      is
      <code class="literal">
       NULL
      </code>
      ,
      <a class="link" href="aggregate-functions.html#function_stddev-samp">
       <code class="literal">
        STDDEV_SAMP()
       </code>
      </a>
      returns
      <code class="literal">
       NULL
      </code>
      .
     </p>
     <p>
      This function executes as a window function if
      <em class="replaceable">
       <code>
        over_clause
       </code>
      </em>
      is present.
      <em class="replaceable">
       <code>
        over_clause
       </code>
      </em>
      is as described in
      <a class="xref" href="window-functions-usage.html" title="14.20.2 Window Function Concepts and Syntax">
       Section 14.20.2, “Window Function Concepts and Syntax”
      </a>
      .
     </p>
    </li>
    <li class="listitem">
     <a name="function_sum">
     </a>
     <p>
      <a class="link" href="aggregate-functions.html#function_sum">
       <code class="literal">
        SUM([DISTINCT]
        <em class="replaceable">
         <code>
          expr
         </code>
        </em>
        )
            [
        <em class="replaceable">
         <code>
          over_clause
         </code>
        </em>
        ]
       </code>
      </a>
     </p>
     <a class="indexterm" name="idm46045192841600">
     </a>
     <a class="indexterm" name="idm46045192840528">
     </a>
     <a class="indexterm" name="idm46045192839456">
     </a>
     <p>
      Returns the sum of
      <em class="replaceable">
       <code>
        expr
       </code>
      </em>
      . If the
            return set has no rows,
      <a class="link" href="aggregate-functions.html#function_sum">
       <code class="literal">
        SUM()
       </code>
      </a>
      returns
      <code class="literal">
       NULL
      </code>
      . The
      <code class="literal">
       DISTINCT
      </code>
      keyword can be used to sum only
            the distinct values of
      <em class="replaceable">
       <code>
        expr
       </code>
      </em>
      .
     </p>
     <p>
      If there are no matching rows, or if
      <em class="replaceable">
       <code>
        expr
       </code>
      </em>
      is
      <code class="literal">
       NULL
      </code>
      ,
      <a class="link" href="aggregate-functions.html#function_sum">
       <code class="literal">
        SUM()
       </code>
      </a>
      returns
      <code class="literal">
       NULL
      </code>
      .
     </p>
     <p>
      This function executes as a window function if
      <em class="replaceable">
       <code>
        over_clause
       </code>
      </em>
      is present.
      <em class="replaceable">
       <code>
        over_clause
       </code>
      </em>
      is as described in
      <a class="xref" href="window-functions-usage.html" title="14.20.2 Window Function Concepts and Syntax">
       Section 14.20.2, “Window Function Concepts and Syntax”
      </a>
      ; it cannot be used
            with
      <code class="literal">
       DISTINCT
      </code>
      .
     </p>
    </li>
    <li class="listitem">
     <a name="function_var-pop">
     </a>
     <p>
      <a class="link" href="aggregate-functions.html#function_var-pop">
       <code class="literal">
        VAR_POP(
        <em class="replaceable">
         <code>
          expr
         </code>
        </em>
        )
            [
        <em class="replaceable">
         <code>
          over_clause
         </code>
        </em>
        ]
       </code>
      </a>
     </p>
     <a class="indexterm" name="idm46045192820608">
     </a>
     <p>
      Returns the population standard variance of
      <em class="replaceable">
       <code>
        expr
       </code>
      </em>
      . It considers rows as the
            whole population, not as a sample, so it has the number of
            rows as the denominator. You can also use
      <a class="link" href="aggregate-functions.html#function_variance">
       <code class="literal">
        VARIANCE()
       </code>
      </a>
      , which is
            equivalent but is not standard SQL.
     </p>
     <p>
      If there are no matching rows, or if
      <em class="replaceable">
       <code>
        expr
       </code>
      </em>
      is
      <code class="literal">
       NULL
      </code>
      ,
      <a class="link" href="aggregate-functions.html#function_var-pop">
       <code class="literal">
        VAR_POP()
       </code>
      </a>
      returns
      <code class="literal">
       NULL
      </code>
      .
     </p>
     <p>
      This function executes as a window function if
      <em class="replaceable">
       <code>
        over_clause
       </code>
      </em>
      is present.
      <em class="replaceable">
       <code>
        over_clause
       </code>
      </em>
      is as described in
      <a class="xref" href="window-functions-usage.html" title="14.20.2 Window Function Concepts and Syntax">
       Section 14.20.2, “Window Function Concepts and Syntax”
      </a>
      .
     </p>
    </li>
    <li class="listitem">
     <a name="function_var-samp">
     </a>
     <p>
      <a class="link" href="aggregate-functions.html#function_var-samp">
       <code class="literal">
        VAR_SAMP(
        <em class="replaceable">
         <code>
          expr
         </code>
        </em>
        )
            [
        <em class="replaceable">
         <code>
          over_clause
         </code>
        </em>
        ]
       </code>
      </a>
     </p>
     <a class="indexterm" name="idm46045192804656">
     </a>
     <p>
      Returns the sample variance of
      <em class="replaceable">
       <code>
        expr
       </code>
      </em>
      . That is, the denominator is
            the number of rows minus one.
     </p>
     <p>
      If there are no matching rows, or if
      <em class="replaceable">
       <code>
        expr
       </code>
      </em>
      is
      <code class="literal">
       NULL
      </code>
      ,
      <a class="link" href="aggregate-functions.html#function_var-samp">
       <code class="literal">
        VAR_SAMP()
       </code>
      </a>
      returns
      <code class="literal">
       NULL
      </code>
      .
     </p>
     <p>
      This function executes as a window function if
      <em class="replaceable">
       <code>
        over_clause
       </code>
      </em>
      is present.
      <em class="replaceable">
       <code>
        over_clause
       </code>
      </em>
      is as described in
      <a class="xref" href="window-functions-usage.html" title="14.20.2 Window Function Concepts and Syntax">
       Section 14.20.2, “Window Function Concepts and Syntax”
      </a>
      .
     </p>
    </li>
    <li class="listitem">
     <a name="function_variance">
     </a>
     <p>
      <a class="link" href="aggregate-functions.html#function_variance">
       <code class="literal">
        VARIANCE(
        <em class="replaceable">
         <code>
          expr
         </code>
        </em>
        )
            [
        <em class="replaceable">
         <code>
          over_clause
         </code>
        </em>
        ]
       </code>
      </a>
     </p>
     <a class="indexterm" name="idm46045192790128">
     </a>
     <p>
      Returns the population standard variance of
      <em class="replaceable">
       <code>
        expr
       </code>
      </em>
      .
      <a class="link" href="aggregate-functions.html#function_variance">
       <code class="literal">
        VARIANCE()
       </code>
      </a>
      is a synonym for
            the standard SQL function
      <a class="link" href="aggregate-functions.html#function_var-pop">
       <code class="literal">
        VAR_POP()
       </code>
      </a>
      , provided as a
            MySQL extension.
     </p>
     <p>
      If there are no matching rows, or if
      <em class="replaceable">
       <code>
        expr
       </code>
      </em>
      is
      <code class="literal">
       NULL
      </code>
      ,
      <a class="link" href="aggregate-functions.html#function_variance">
       <code class="literal">
        VARIANCE()
       </code>
      </a>
      returns
      <code class="literal">
       NULL
      </code>
      .
     </p>
     <p>
      This function executes as a window function if
      <em class="replaceable">
       <code>
        over_clause
       </code>
      </em>
      is present.
      <em class="replaceable">
       <code>
        over_clause
       </code>
      </em>
      is as described in
      <a class="xref" href="window-functions-usage.html" title="14.20.2 Window Function Concepts and Syntax">
       Section 14.20.2, “Window Function Concepts and Syntax”
      </a>
      .
     </p>
    </li>
   </ul>
  </div>
 </div>
 <br/>
</div>
