1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>7.5.Sorting Rows</title>
<link rel="stylesheet" href="stylesheet.css" type="text/css">
<link rev="made" href="pgsql-docs@postgresql.org">
<meta name="generator" content="DocBook XSL Stylesheets V1.70.0">
<link rel="start" href="index.html" title="PostgreSQL 8.1.4 Documentation">
<link rel="up" href="queries.html" title="Chapter7.Queries">
<link rel="prev" href="queries-union.html" title="7.4.Combining Queries">
<link rel="next" href="queries-limit.html" title="7.6.LIMIT and OFFSET">
<link rel="copyright" href="ln-legalnotice.html" title="Legal Notice">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="sect1" lang="en">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="queries-order"></a>7.5.Sorting Rows</h2></div></div></div>
<a name="id583098"></a><a name="id583109"></a><p> After a query has produced an output table (after the select list
has been processed) it can optionally be sorted. If sorting is not
chosen, the rows will be returned in an unspecified order. The actual
order in that case will depend on the scan and join plan types and
the order on disk, but it must not be relied on. A particular
output ordering can only be guaranteed if the sort step is explicitly
chosen.
</p>
<p> The <code class="literal">ORDER BY</code> clause specifies the sort order:
</p>
<pre class="synopsis">SELECT <em class="replaceable"><code>select_list</code></em>
FROM <em class="replaceable"><code>table_expression</code></em>
ORDER BY <em class="replaceable"><code>column1</code></em> [<span class="optional">ASC | DESC</span>] [<span class="optional">, <em class="replaceable"><code>column2</code></em> [<span class="optional">ASC | DESC</span>] ...</span>]</pre>
<p>
<em class="replaceable"><code>column1</code></em>, etc., refer to select list
columns. These can be either the output name of a column (see
<a href="queries-select-lists.html#queries-column-labels" title="7.3.2.Column Labels">Section7.3.2, “Column Labels”</a>) or the number of a column. Some
examples:
</p>
<pre class="programlisting">SELECT a, b FROM table1 ORDER BY a;
SELECT a + b AS sum, c FROM table1 ORDER BY sum;
SELECT a, sum(b) FROM table1 GROUP BY a ORDER BY 1;</pre>
<p>
</p>
<p> As an extension to the SQL standard, <span class="productname">PostgreSQL</span> also allows ordering
by arbitrary expressions:
</p>
<pre class="programlisting">SELECT a, b FROM table1 ORDER BY a + b;</pre>
<p>
References to column names of the <code class="literal">FROM</code> clause that are
not present in the select list are also allowed:
</p>
<pre class="programlisting">SELECT a FROM table1 ORDER BY b;</pre>
<p>
But these extensions do not work in queries involving
<code class="literal">UNION</code>, <code class="literal">INTERSECT</code>, or <code class="literal">EXCEPT</code>,
and are not portable to other SQL databases.
</p>
<p> Each column specification may be followed by an optional
<code class="literal">ASC</code> or <code class="literal">DESC</code> to set the sort direction to
ascending or descending. <code class="literal">ASC</code> order is the default.
Ascending order puts smaller values first, where
“<span class="quote">smaller</span>” is defined in terms of the
<code class="literal"><</code> operator. Similarly, descending order is
determined with the <code class="literal">></code> operator.
<sup>[<a name="id583286" href="#ftn.id583286">4</a>]</sup>
</p>
<p> If more than one sort column is specified, the later entries are
used to sort rows that are equal under the order imposed by the
earlier sort columns.
</p>
<div class="footnotes">
<br><hr width="100" align="left">
<div class="footnote"><p><sup>[<a name="ftn.id583286" href="#id583286">4</a>] </sup> Actually, <span class="productname">PostgreSQL</span> uses the <em class="firstterm">default B-tree
operator class</em> for the column's data type to determine the sort
ordering for <code class="literal">ASC</code> and <code class="literal">DESC</code>. Conventionally,
data types will be set up so that the <code class="literal"><</code> and
<code class="literal">></code> operators correspond to this sort ordering,
but a user-defined data type's designer could choose to do something
different.
</p></div>
</div>
</div></body>
</html>
|