ORDER BY Clause in a SELECT Statement
SQL queries initiated by using a SELECT statement support the ORDER BY clause.
Syntax
<simple SELECT statement> [ORDER BY <ordering_term> [ASC | DESC]]The result of the SELECT statement is sorted in an ascending or descending order. An ordering term can be a column in the select list, an alias as specified in the select list, or a column index number in the select list, with or without ASC or DESC. The default sort order is ascending. For best performance, the ordering term used in the ORDER BY clause must match the primary or secondary index selected in the WHERE clause. The ORDER BY clause can be optionally followed by a LIMIT clause.
TIBCO recommends using columns in the
ORDER BY clause which are columns of a primary or secondary index defined for the table. Using columns of a primary or secondary index increases the efficiency and speed of the query. If you do not use columns from a primary or secondary index in the
ORDER BY clause, there is a limit of 128MB of data that can be processed by the query.
ORDER BY clauses with columns that match the order of defined indexes exactly or match the order of defined indexes in the reverse order are usually more efficient than other types of
ORDER BY clauses.
Example
For example, there is an index defined for the table by the name of
index1 that is indexed on
lastname,
firstname, and
city. The following queries use
index1 because the columns match the index column in the same order and the sorting is in a single direction (either in the ascending order or in the descending order):
SELECT * FROM t1 ORDER BY lastname ASC, firstname ASC, city ASC SELECT * FROM t1 ORDER BY lastname DESC, firstname DESC, city DESCThe following query cannot use index1 because some columns are sorted in the ascending order and some in the descending order:
SELECT * FROM t1 ORDER BY lastname DESC, firstname ASC, city ASC SELECT * FROM t1 ORDER BY lastname ASC, firstname DESC, city ASC
Related concepts
Copyright © Cloud Software Group, Inc. All rights reserved.