<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h3 class="title">
      <a name="parenthesized-query-expressions">
      </a>
      15.2.11 Parenthesized Query Expressions
     </h3>
    </div>
   </div>
  </div>
  <a class="indexterm" name="idm46045181236336">
  </a>
  <a class="indexterm" name="idm46045181235248">
  </a>
  <a class="indexterm" name="idm46045181233744">
  </a>
  <a class="indexterm" name="idm46045181232240">
  </a>
  <a class="indexterm" name="idm46045181230736">
  </a>
  <a class="indexterm" name="idm46045181229232">
  </a>
  <a class="indexterm" name="idm46045181227728">
  </a>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa16844814"><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"><em class="replaceable">parenthesized_query_expression</em>:
    <span class="token punctuation">(</span> <em class="replaceable">query_expression</em> <span class="token punctuation">[</span><em class="replaceable">order_by_clause</em><span class="token punctuation">]</span> <span class="token punctuation">[</span><em class="replaceable">limit_clause</em><span class="token punctuation">]</span> <span class="token punctuation">)</span>
      <span class="token punctuation">[</span><em class="replaceable">order_by_clause</em><span class="token punctuation">]</span>
      <span class="token punctuation">[</span><em class="replaceable">limit_clause</em><span class="token punctuation">]</span>
      <span class="token punctuation">[</span><em class="replaceable">into_clause</em><span class="token punctuation">]</span>

<em class="replaceable">query_expression</em>:
    <em class="replaceable">query_block</em> <span class="token punctuation">[</span><em class="replaceable">set_op</em> <em class="replaceable">query_block</em> <span class="token punctuation">[</span><em class="replaceable">set_op</em> <em class="replaceable">query_block</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><em class="replaceable">order_by_clause</em><span class="token punctuation">]</span>
      <span class="token punctuation">[</span><em class="replaceable">limit_clause</em><span class="token punctuation">]</span>
      <span class="token punctuation">[</span><em class="replaceable">into_clause</em><span class="token punctuation">]</span>

<em class="replaceable">query_block</em>:
    <span class="token keyword">SELECT</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> <span class="token operator">|</span> <span class="token keyword">TABLE</span> <span class="token operator">|</span> <span class="token keyword">VALUES</span>

<em class="replaceable">order_by_clause</em>:
    <span class="token keyword">ORDER</span> <span class="token keyword">BY</span> <span class="token keyword">as</span> <span class="token keyword">for</span> <span class="token keyword">SELECT</span>

<em class="replaceable">limit_clause</em>:
    <span class="token keyword">LIMIT</span> <span class="token keyword">as</span> <span class="token keyword">for</span> <span class="token keyword">SELECT</span>

<em class="replaceable">into_clause</em>:
    <span class="token keyword">INTO</span> <span class="token keyword">as</span> <span class="token keyword">for</span> <span class="token keyword">SELECT</span>

<em class="replaceable">set_op</em>:
    <span class="token keyword">UNION</span> <span class="token operator">|</span> <span class="token keyword">INTERSECT</span> <span class="token operator">|</span> <span class="token keyword">EXCEPT</span></code></pre>
  </div>
  <p>
   MySQL 8.4 supports parenthesized query expressions
      according to the preceding syntax. At its simplest, a
      parenthesized query expression contains a single
   <a class="link" href="select.html" title="15.2.13 SELECT Statement">
    <code class="literal">
     SELECT
    </code>
   </a>
   or other statement returning
      a result set and no following optional clauses:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa53438008"><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 punctuation">(</span><span class="token keyword">SELECT</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">(</span><span class="token keyword">SELECT</span> <span class="token operator">*</span> <span class="token keyword">FROM</span> INFORMATION_SCHEMA<span class="token punctuation">.</span>SCHEMATA <span class="token keyword">WHERE</span> <span class="token keyword">SCHEMA_NAME</span> <span class="token operator">=</span> <span class="token string">'mysql'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">TABLE</span> t<span class="token punctuation">;</span>

<span class="token keyword">VALUES</span> <span class="token keyword">ROW</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</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 keyword">ROW</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
  </div>
  <p>
   A parenthesized query expression can also contain queries linked
      by one or more set operations such as
   <a class="link" href="union.html" title="15.2.18 UNION Clause">
    <code class="literal">
     UNION
    </code>
   </a>
   , and end with any or all of
      the optional clauses:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa23278125"><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 punctuation">(</span><span class="token keyword">SELECT</span> <span class="token number">1</span> <span class="token keyword">AS</span> result <span class="token keyword">UNION</span> <span class="token keyword">SELECT</span> <span class="token number">2</span><span class="token punctuation">)</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>
<span class="token output"><span class="token punctuation">|</span> result <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>
<span class="token output"><span class="token punctuation">|</span>      1 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span>      2 <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>
<span class="token prompt">mysql&gt;</span> <span class="token punctuation">(</span><span class="token keyword">SELECT</span> <span class="token number">1</span> <span class="token keyword">AS</span> result <span class="token keyword">UNION</span> <span class="token keyword">SELECT</span> <span class="token number">2</span><span class="token punctuation">)</span> <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>
<span class="token output"><span class="token punctuation">|</span> result <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>
<span class="token output"><span class="token punctuation">|</span>      1 <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>
<span class="token prompt">mysql&gt;</span> <span class="token punctuation">(</span><span class="token keyword">SELECT</span> <span class="token number">1</span> <span class="token keyword">AS</span> result <span class="token keyword">UNION</span> <span class="token keyword">SELECT</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token keyword">LIMIT</span> <span class="token number">1</span> <span class="token keyword">OFFSET</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>
<span class="token output"><span class="token punctuation">|</span> result <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>
<span class="token output"><span class="token punctuation">|</span>      2 <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>
<span class="token prompt">mysql&gt;</span> <span class="token punctuation">(</span><span class="token keyword">SELECT</span> <span class="token number">1</span> <span class="token keyword">AS</span> result <span class="token keyword">UNION</span> <span class="token keyword">SELECT</span> <span class="token number">2</span><span class="token punctuation">)</span>
       <span class="token keyword">ORDER</span> <span class="token keyword">BY</span> result <span class="token keyword">DESC</span> <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>
<span class="token output"><span class="token punctuation">|</span> result <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>
<span class="token output"><span class="token punctuation">|</span>      2 <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>
<span class="token prompt">mysql&gt;</span> <span class="token punctuation">(</span><span class="token keyword">SELECT</span> <span class="token number">1</span> <span class="token keyword">AS</span> result <span class="token keyword">UNION</span> <span class="token keyword">SELECT</span> <span class="token number">2</span><span class="token punctuation">)</span>
       <span class="token keyword">ORDER</span> <span class="token keyword">BY</span> result <span class="token keyword">DESC</span> <span class="token keyword">LIMIT</span> <span class="token number">1</span> <span class="token keyword">OFFSET</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>
<span class="token output"><span class="token punctuation">|</span> result <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>
<span class="token output"><span class="token punctuation">|</span>      1 <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>
<span class="token prompt">mysql&gt;</span> <span class="token punctuation">(</span><span class="token keyword">SELECT</span> <span class="token number">1</span> <span class="token keyword">AS</span> result <span class="token keyword">UNION</span> <span class="token keyword">SELECT</span> <span class="token number">3</span> <span class="token keyword">UNION</span> <span class="token keyword">SELECT</span> <span class="token number">2</span><span class="token punctuation">)</span>
       <span class="token keyword">ORDER</span> <span class="token keyword">BY</span> result <span class="token keyword">LIMIT</span> <span class="token number">1</span> <span class="token keyword">OFFSET</span> <span class="token number">1</span> <span class="token keyword">INTO</span> <span class="token variable">@var</span><span class="token punctuation">;</span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token variable">@var</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>
<span class="token output"><span class="token punctuation">|</span> @var <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>
<span class="token output"><span class="token punctuation">|</span>    2 <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></code></pre>
  </div>
  <p>
   <code class="literal">
    INTERSECT
   </code>
   acts before
   <code class="literal">
    UNION
   </code>
   and
   <code class="literal">
    EXCEPT
   </code>
   , so that the following two
      statements are equivalent:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa95376996"><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> a <span class="token keyword">FROM</span> t1 <span class="token keyword">EXCEPT</span> <span class="token keyword">SELECT</span> b <span class="token keyword">FROM</span> t2 <span class="token keyword">INTERSECT</span> <span class="token keyword">SELECT</span> c <span class="token keyword">FROM</span> t3<span class="token punctuation">;</span>

<span class="token keyword">SELECT</span> a <span class="token keyword">FROM</span> t1 <span class="token keyword">EXCEPT</span> <span class="token punctuation">(</span><span class="token keyword">SELECT</span> b <span class="token keyword">FROM</span> t2 <span class="token keyword">INTERSECT</span> <span class="token keyword">SELECT</span> c <span class="token keyword">FROM</span> t3<span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
  </div>
  <p>
   Parenthesized query expressions are also used as query
      expressions, so a query expression, usually composed of query
      blocks, may also consist of parenthesized query expressions:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting one-line language-sql"><div class="docs-select-all right" id="sa21321631"><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 punctuation">(</span><span class="token keyword">TABLE</span> t1 <span class="token keyword">ORDER</span> <span class="token keyword">BY</span> a<span class="token punctuation">)</span> <span class="token keyword">UNION</span> <span class="token punctuation">(</span><span class="token keyword">TABLE</span> t2 <span class="token keyword">ORDER</span> <span class="token keyword">BY</span> b<span class="token punctuation">)</span> <span class="token keyword">ORDER</span> <span class="token keyword">BY</span> z<span class="token punctuation">;</span></code></pre>
  </div>
  <p>
   Query blocks may have trailing
   <code class="literal">
    ORDER BY
   </code>
   and
   <code class="literal">
    LIMIT
   </code>
   clauses, which are applied before the
      outer set operation,
   <code class="literal">
    ORDER BY
   </code>
   , and
   <code class="literal">
    LIMIT
   </code>
   .
  </p>
  <p>
   You cannot have a query block with a trailing
   <code class="literal">
    ORDER
      BY
   </code>
   or
   <code class="literal">
    LIMIT
   </code>
   without wrapping it in
      parentheses but parentheses may be used for enforcement in various
      ways:
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      To enforce
      <code class="literal">
       LIMIT
      </code>
      on each query block:
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa37238856"><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 punctuation">(</span><span class="token keyword">SELECT</span> <span class="token number">1</span> <span class="token keyword">LIMIT</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">UNION</span> <span class="token punctuation">(</span><span class="token keyword">VALUES</span> <span class="token keyword">ROW</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token keyword">LIMIT</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token punctuation">(</span><span class="token keyword">VALUES</span> <span class="token keyword">ROW</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token keyword">ROW</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token keyword">LIMIT</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token keyword">EXCEPT</span> <span class="token punctuation">(</span><span class="token keyword">SELECT</span> <span class="token number">2</span> <span class="token keyword">LIMIT</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
     </div>
    </li>
    <li class="listitem">
     <p>
      To enforce
      <code class="literal">
       LIMIT
      </code>
      on both query blocks and
          the entire query expression:
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting one-line language-sql"><div class="docs-select-all right" id="sa28718545"><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 punctuation">(</span><span class="token keyword">SELECT</span> <span class="token number">1</span> <span class="token keyword">LIMIT</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">UNION</span> <span class="token punctuation">(</span><span class="token keyword">SELECT</span> <span class="token number">2</span> <span class="token keyword">LIMIT</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">LIMIT</span> <span class="token number">1</span><span class="token punctuation">;</span></code></pre>
     </div>
    </li>
    <li class="listitem">
     <p>
      To enforce
      <code class="literal">
       LIMIT
      </code>
      on the entire query
          expression (with no parentheses):
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting one-line language-sql"><div class="docs-select-all right" id="sa26046936"><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">VALUES</span> <span class="token keyword">ROW</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token keyword">ROW</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token keyword">INTERSECT</span> <span class="token keyword">VALUES</span> <span class="token keyword">ROW</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token keyword">ROW</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">LIMIT</span> <span class="token number">1</span><span class="token punctuation">;</span></code></pre>
     </div>
    </li>
    <li class="listitem">
     <p>
      Hybrid enforcement:
      <code class="literal">
       LIMIT
      </code>
      on the first
          query block and on the entire query expression:
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting one-line language-sql"><div class="docs-select-all right" id="sa53613336"><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 punctuation">(</span><span class="token keyword">SELECT</span> <span class="token number">1</span> <span class="token keyword">LIMIT</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">UNION</span> <span class="token keyword">SELECT</span> <span class="token number">2</span> <span class="token keyword">LIMIT</span> <span class="token number">1</span><span class="token punctuation">;</span></code></pre>
     </div>
    </li>
   </ul>
  </div>
  <p>
   The syntax described in this section is subject to certain
      restrictions:
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      A trailing
      <code class="literal">
       INTO
      </code>
      clause for a query
          expression is not permitted if there is another
      <code class="literal">
       INTO
      </code>
      clause inside parentheses.
     </p>
    </li>
    <li class="listitem">
     <p>
      An
      <code class="literal">
       ORDER BY
      </code>
      or
      <code class="literal">
       LIMIT
      </code>
      within a parenthesized query expression which is also applied
          in the outer query is handled in accordance with the SQL
          standard.
     </p>
     <p>
      Nested parenthesized query expressions are permitted. The
          maximum level of nesting supported is 63; this is after any
          simplifications or merges have been performed by the parser.
     </p>
     <p>
      An example of such a statement is shown here:
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa18465339"><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 punctuation">(</span><span class="token keyword">SELECT</span> <span class="token string">'a'</span> <span class="token keyword">UNION</span> <span class="token keyword">SELECT</span> <span class="token string">'b'</span> <span class="token keyword">LIMIT</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token keyword">LIMIT</span> <span class="token number">3</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>
<span class="token output"><span class="token punctuation">|</span> a <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>
<span class="token output"><span class="token punctuation">|</span> a <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> b <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>
<span class="token output">2 rows in set (0.00 sec)</span></code></pre>
     </div>
     <p>
      You should be aware that, when collapsing parenthesized
          expression bodies, MySQL follows SQL standard semantics, so
          that a higher outer limit cannot override an inner lower one.
          For example,
      <code class="literal">
       (SELECT ... LIMIT 5) LIMIT 10
      </code>
      can return no more than five rows.
     </p>
    </li>
   </ul>
  </div>
 </div>
 <br/>
</div>
