ORDER BY clause controls the order in which rows are returned or
processed. It can be used in any , including
as operand of or , as
well as with and
statements.
Synopsis
Parameters
TheORDER BY clause takes a comma-separated list of ordering specifications.
Each ordering specification is composed of a column selection followed optionally
by the keyword ASC or DESC.
Each column selection can take one of the following forms:
- A simple column selection, determined as follows:
- The name of a column label configured with
ASearlier in the . This uses the value computed by theSELECTclause as the sorting key. - A positive integer number, designating one of the columns in the data source, either the
FROMclause of theSELECTclause where it happens or the table being written to byDELETEorUPDATE. This uses the corresponding input value from the data source to use as the sorting key. - An arbitrary . This uses the result of evaluating that expression as the sorting key.
- The name of a column label configured with
- The notation
PRIMARY KEY <table_name>. This uses the primary key column(s) of the given table as sorting key. This table must be part of the data source. - The notation
INDEX <table_name>@<index_name. This uses the columns indexed by the given index as sorting key. This table must be part of the data source.
ASC after a column selection indicates to use
the sorting key as-is, and thus is meaningless.
The optional keyword DESC inverts the direction of the column(s)
selected by the selection that immediately precedes.
CockroachDB supports NULLS FIRST and NULLS LAST in ORDER BY clauses for compatibility with PostgreSQL row-sorting syntax.
The default CockroachDB ordering is NULLs first for ascending order and NULLs last for descending order, which is the opposite of the PostgreSQL default. To change the default CockroachDB ordering, set the null_ordered_last to true.
Order preservation
In general, the order of the intermediate results of a query is not guaranteed, even ifORDER BY is specified. In other words, the ORDER BY clause is only
effective at the top-level statement. For example, it is ignored by the query
planner when present in a sub-query in a FROM clause as follows:
ORDER BY clause in a sub-query
significant:
- The ordering of the operand of a
WITH ORDINALITYclause (within theFROMoperand of aSELECTclause) is preserved, to control the numbering of the rows. - The ordering of the operand of a stand-alone
LIMITorOFFSETclause (within aFROMoperand of aSELECTclause) is preserved, to determine which rows are kept in the result. - The ordering of the data source for an
statement or an statement that also uses
LIMITis preserved, to determine which rows are processed, but not their order. - The ordering indicated for an or
statement that also uses
LIMITis used to determine which rows are processed, but not their order. (This is a CockroachDB extension.) - The ordering of a sub-query used in a scalar expression is preserved.
WITH ORDINALITY:
LIMIT clause in FROM:
Ordering of rows without ORDER BY
Without ORDER BY, rows are processed or returned in a
non-deterministic order. “Non-deterministic” means that the actual order
can depend on the logical plan, the order of data on disk, the topology
of the CockroachDB cluster, and is generally variable over time.
Sorting using simple column selections
Considering the following table:AS aliases
take priority over the data source columns, for example:
Sorting using multiple columns
When more than one ordering specification is given, the later specifications are used to order rows that are equal over the earlier specifications, for example:b, and then if there are multiple
rows that have the same value in column b, it will then order these
rows by column a.
Inverting the sort order
The keywordDESC (“descending”) can be added after an ordering specification to
invert its order. This can be specified separately for each specification, for example:
Sorting in primary key order
TheORDER BY PRIMARY KEY notation guarantees that the results are
presented in primary key order.
The particular advantage is that for queries using the primary index,
this guarantees the order while also guaranteeing there will not be an
additional sorting computation to achieve it, for example:
DESC already, then its meaning
will be flipped (cancelled) if the ORDER BY clause also uses
DESC, for example:
Sorting in index order
TheORDER BY INDEX notation guarantees that the results are presented
in the order of a given index.
The particular advantage is that for queries using that index, this
guarantees the order while also guaranteeing there will not be an
additional sorting computation to achieve it, for example:
DESC already, then its meaning
will be flipped (cancelled) if the ORDER BY clause also uses
DESC, for example:
Ordering rows in DML statements
When usingORDER BY with an ,
, or
(i.e., a DML statement), the ORDER BY clause is
ignored if it is not used in combination with .
The combination of both ORDER BY and LIMIT/OFFSET determines
which rows of the input are used to insert, update or delete the table
data, but it does not determine in which order the mutation takes
place.
For example, using LIMIT in INSERT:
ORDER BY does not control the final order of the rows
in the table is that the ordering of rows in the target table is
determined by its .
To order the result of the RETURNING clause, see Sorting the output
of deletes.
Sorting the output of deletes
To sort the output of aDELETE statement, use:

