codekingpro/portable-devtools
114k
1<?xml version="1.0" encoding="UTF-8" standalone="no"?>2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>7.5. Sorting Rows (ORDER BY)</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="queries-union.html" title="7.4. Combining Queries (UNION, INTERSECT, EXCEPT)" /><link rel="next" href="queries-limit.html" title="7.6. LIMIT and OFFSET" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">7.5. Sorting Rows (<code class="literal">ORDER BY</code>)</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="queries-union.html" title="7.4. Combining Queries (UNION, INTERSECT, EXCEPT)">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="queries.html" title="Chapter 7. Queries">Up</a></td><th width="60%" align="center">Chapter 7. Queries</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 16.3 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="queries-limit.html" title="7.6. LIMIT and OFFSET">Next</a></td></tr></table><hr /></div><div class="sect1" id="QUERIES-ORDER"><div class="titlepage"><div><div><h2 class="title" style="clear: both">7.5. Sorting Rows (<code class="literal">ORDER BY</code>) <a href="#QUERIES-ORDER" class="id_link">#</a></h2></div></div></div><a id="id-1.5.6.9.2" class="indexterm"></a><a id="id-1.5.6.9.3" class="indexterm"></a><p>3 After a query has produced an output table (after the select list4 has been processed) it can optionally be sorted. If sorting is not5 chosen, the rows will be returned in an unspecified order. The actual6 order in that case will depend on the scan and join plan types and7 the order on disk, but it must not be relied on. A particular8 output ordering can only be guaranteed if the sort step is explicitly9 chosen.10 </p><p>11 The <code class="literal">ORDER BY</code> clause specifies the sort order:12</p><pre class="synopsis">13SELECT <em class="replaceable"><code>select_list</code></em>14 FROM <em class="replaceable"><code>table_expression</code></em>15 ORDER BY <em class="replaceable"><code>sort_expression1</code></em> [<span class="optional">ASC | DESC</span>] [<span class="optional">NULLS { FIRST | LAST }</span>]16 [<span class="optional">, <em class="replaceable"><code>sort_expression2</code></em> [<span class="optional">ASC | DESC</span>] [<span class="optional">NULLS { FIRST | LAST }</span>] ...</span>]17</pre><p>18 The sort expression(s) can be any expression that would be valid in the19 query's select list. An example is:20</p><pre class="programlisting">21SELECT a, b FROM table1 ORDER BY a + b, c;22</pre><p>23 When more than one expression is specified,24 the later values are used to sort rows that are equal according to the25 earlier values. Each expression can be followed by an optional26 <code class="literal">ASC</code> or <code class="literal">DESC</code> keyword to set the sort direction to27 ascending or descending. <code class="literal">ASC</code> order is the default.28 Ascending order puts smaller values first, where29 <span class="quote">“<span class="quote">smaller</span>”</span> is defined in terms of the30 <code class="literal"><</code> operator. Similarly, descending order is31 determined with the <code class="literal">></code> operator.32 <a href="#ftn.id-1.5.6.9.5.10" class="footnote"><sup class="footnote" id="id-1.5.6.9.5.10">[6]</sup></a>33 </p><p>34 The <code class="literal">NULLS FIRST</code> and <code class="literal">NULLS LAST</code> options can be35 used to determine whether nulls appear before or after non-null values36 in the sort ordering. By default, null values sort as if larger than any37 non-null value; that is, <code class="literal">NULLS FIRST</code> is the default for38 <code class="literal">DESC</code> order, and <code class="literal">NULLS LAST</code> otherwise.39 </p><p>40 Note that the ordering options are considered independently for each41 sort column. For example <code class="literal">ORDER BY x, y DESC</code> means42 <code class="literal">ORDER BY x ASC, y DESC</code>, which is not the same as43 <code class="literal">ORDER BY x DESC, y DESC</code>.44 </p><p>45 A <em class="replaceable"><code>sort_expression</code></em> can also be the column label or number46 of an output column, as in:47</p><pre class="programlisting">48SELECT a + b AS sum, c FROM table1 ORDER BY sum;49SELECT a, max(b) FROM table1 GROUP BY a ORDER BY 1;50</pre><p>51 both of which sort by the first output column. Note that an output52 column name has to stand alone, that is, it cannot be used in an expression53 — for example, this is <span class="emphasis"><em>not</em></span> correct:54</p><pre class="programlisting">55SELECT a + b AS sum, c FROM table1 ORDER BY sum + c; -- wrong56</pre><p>57 This restriction is made to reduce ambiguity. There is still58 ambiguity if an <code class="literal">ORDER BY</code> item is a simple name that59 could match either an output column name or a column from the table60 expression. The output column is used in such cases. This would61 only cause confusion if you use <code class="literal">AS</code> to rename an output62 column to match some other table column's name.63 </p><p>64 <code class="literal">ORDER BY</code> can be applied to the result of a65 <code class="literal">UNION</code>, <code class="literal">INTERSECT</code>, or <code class="literal">EXCEPT</code>66 combination, but in this case it is only permitted to sort by67 output column names or numbers, not by expressions.68 </p><div class="footnotes"><br /><hr style="width:100; text-align:left;margin-left: 0" /><div id="ftn.id-1.5.6.9.5.10" class="footnote"><p><a href="#id-1.5.6.9.5.10" class="para"><sup class="para">[6] </sup></a>69 Actually, <span class="productname">PostgreSQL</span> uses the <em class="firstterm">default B-tree70 operator class</em> for the expression's data type to determine the sort71 ordering for <code class="literal">ASC</code> and <code class="literal">DESC</code>. Conventionally,72 data types will be set up so that the <code class="literal"><</code> and73 <code class="literal">></code> operators correspond to this sort ordering,74 but a user-defined data type's designer could choose to do something75 different.76 </p></div></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="queries-union.html" title="7.4. Combining Queries (UNION, INTERSECT, EXCEPT)">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="queries.html" title="Chapter 7. Queries">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="queries-limit.html" title="7.6. LIMIT and OFFSET">Next</a></td></tr><tr><td width="40%" align="left" valign="top">7.4. Combining Queries (<code class="literal">UNION</code>, <code class="literal">INTERSECT</code>, <code class="literal">EXCEPT</code>) </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 16.3 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 7.6. <code class="literal">LIMIT</code> and <code class="literal">OFFSET</code></td></tr></table></div></body></html>