<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h4 class="title">
      <a name="sorting-rows">
      </a>
      5.3.4.4 Sorting Rows
     </h4>
    </div>
   </div>
  </div>
  <a class="indexterm" name="idm46045325215360">
  </a>
  <a class="indexterm" name="idm46045325214288">
  </a>
  <a class="indexterm" name="idm46045325212800">
  </a>
  <a class="indexterm" name="idm46045325211312">
  </a>
  <a class="indexterm" name="idm46045325209824">
  </a>
  <p>
   You may have noticed in the preceding examples that the result
          rows are displayed in no particular order. It is often easier
          to examine query output when the rows are sorted in some
          meaningful way. To sort a result, use an
   <code class="literal">
    ORDER
          BY
   </code>
   clause.
  </p>
  <p>
   Here are animal birthdays, sorted by date:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa71974274"><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 keyword">name</span><span class="token punctuation">,</span> birth <span class="token keyword">FROM</span> pet <span class="token keyword">ORDER</span> <span class="token keyword">BY</span> birth<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>
<span class="token output"><span class="token punctuation">|</span> name     <span class="token punctuation">|</span> birth      <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>
<span class="token output"><span class="token punctuation">|</span> Buffy    <span class="token punctuation">|</span> 1989<span class="token punctuation">-</span>05<span class="token punctuation">-</span>13 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Bowser   <span class="token punctuation">|</span> 1989<span class="token punctuation">-</span>08<span class="token punctuation">-</span>31 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Fang     <span class="token punctuation">|</span> 1990<span class="token punctuation">-</span>08<span class="token punctuation">-</span>27 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Fluffy   <span class="token punctuation">|</span> 1993<span class="token punctuation">-</span>02<span class="token punctuation">-</span>04 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Claws    <span class="token punctuation">|</span> 1994<span class="token punctuation">-</span>03<span class="token punctuation">-</span>17 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Slim     <span class="token punctuation">|</span> 1996<span class="token punctuation">-</span>04<span class="token punctuation">-</span>29 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Whistler <span class="token punctuation">|</span> 1997<span class="token punctuation">-</span>12<span class="token punctuation">-</span>09 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Chirpy   <span class="token punctuation">|</span> 1998<span class="token punctuation">-</span>09<span class="token punctuation">-</span>11 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Puffball <span class="token punctuation">|</span> 1999<span class="token punctuation">-</span>03<span class="token punctuation">-</span>30 <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></code></pre>
  </div>
  <p>
   On character type columns, sorting—like all other
          comparison operations—is normally performed in a
          case-insensitive fashion. This means that the order is
          undefined for columns that are identical except for their
          case. You can force a case-sensitive sort for a column by
          using
   <a class="link" href="cast-functions.html#operator_binary">
    <code class="literal">
     BINARY
    </code>
   </a>
   like so:
   <code class="literal">
    ORDER BY BINARY
    <em class="replaceable">
     <code>
      col_name
     </code>
    </em>
   </code>
   .
  </p>
  <p>
   The default sort order is ascending, with smallest values
          first. To sort in reverse (descending) order, add the
   <code class="literal">
    DESC
   </code>
   keyword to the name of the column you
          are sorting by:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa99563586"><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 keyword">name</span><span class="token punctuation">,</span> birth <span class="token keyword">FROM</span> pet <span class="token keyword">ORDER</span> <span class="token keyword">BY</span> birth <span class="token keyword">DESC</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 class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="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> name     <span class="token punctuation">|</span> birth      <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>
<span class="token output"><span class="token punctuation">|</span> Puffball <span class="token punctuation">|</span> 1999<span class="token punctuation">-</span>03<span class="token punctuation">-</span>30 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Chirpy   <span class="token punctuation">|</span> 1998<span class="token punctuation">-</span>09<span class="token punctuation">-</span>11 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Whistler <span class="token punctuation">|</span> 1997<span class="token punctuation">-</span>12<span class="token punctuation">-</span>09 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Slim     <span class="token punctuation">|</span> 1996<span class="token punctuation">-</span>04<span class="token punctuation">-</span>29 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Claws    <span class="token punctuation">|</span> 1994<span class="token punctuation">-</span>03<span class="token punctuation">-</span>17 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Fluffy   <span class="token punctuation">|</span> 1993<span class="token punctuation">-</span>02<span class="token punctuation">-</span>04 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Fang     <span class="token punctuation">|</span> 1990<span class="token punctuation">-</span>08<span class="token punctuation">-</span>27 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Bowser   <span class="token punctuation">|</span> 1989<span class="token punctuation">-</span>08<span class="token punctuation">-</span>31 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Buffy    <span class="token punctuation">|</span> 1989<span class="token punctuation">-</span>05<span class="token punctuation">-</span>13 <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></code></pre>
  </div>
  <p>
   You can sort on multiple columns, and you can sort different
          columns in different directions. For example, to sort by type
          of animal in ascending order, then by birth date within animal
          type in descending order (youngest animals first), use the
          following query:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa37386622"><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 keyword">name</span><span class="token punctuation">,</span> species<span class="token punctuation">,</span> birth <span class="token keyword">FROM</span> pet
       <span class="token keyword">ORDER</span> <span class="token keyword">BY</span> species<span class="token punctuation">,</span> birth <span class="token keyword">DESC</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 class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="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> name     <span class="token punctuation">|</span> species <span class="token punctuation">|</span> birth      <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>
<span class="token output"><span class="token punctuation">|</span> Chirpy   <span class="token punctuation">|</span> bird    <span class="token punctuation">|</span> 1998<span class="token punctuation">-</span>09<span class="token punctuation">-</span>11 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Whistler <span class="token punctuation">|</span> bird    <span class="token punctuation">|</span> 1997<span class="token punctuation">-</span>12<span class="token punctuation">-</span>09 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Claws    <span class="token punctuation">|</span> cat     <span class="token punctuation">|</span> 1994<span class="token punctuation">-</span>03<span class="token punctuation">-</span>17 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Fluffy   <span class="token punctuation">|</span> cat     <span class="token punctuation">|</span> 1993<span class="token punctuation">-</span>02<span class="token punctuation">-</span>04 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Fang     <span class="token punctuation">|</span> dog     <span class="token punctuation">|</span> 1990<span class="token punctuation">-</span>08<span class="token punctuation">-</span>27 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Bowser   <span class="token punctuation">|</span> dog     <span class="token punctuation">|</span> 1989<span class="token punctuation">-</span>08<span class="token punctuation">-</span>31 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Buffy    <span class="token punctuation">|</span> dog     <span class="token punctuation">|</span> 1989<span class="token punctuation">-</span>05<span class="token punctuation">-</span>13 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Puffball <span class="token punctuation">|</span> hamster <span class="token punctuation">|</span> 1999<span class="token punctuation">-</span>03<span class="token punctuation">-</span>30 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Slim     <span class="token punctuation">|</span> snake   <span class="token punctuation">|</span> 1996<span class="token punctuation">-</span>04<span class="token punctuation">-</span>29 <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></code></pre>
  </div>
  <p>
   The
   <code class="literal">
    DESC
   </code>
   keyword applies only to the column
          name immediately preceding it (
   <code class="literal">
    birth
   </code>
   ); it
          does not affect the
   <code class="literal">
    species
   </code>
   column sort
          order.
  </p>
 </div>
 <br/>
</div>
