TurboSQL Guide

ORDER BY Clause

Previous  Top  Next

Sorts the rows retrieved by a SELECT statement.

ORDER BY column_reference [, column_reference...] [ASC|DESC]

Description

Use an ORDER BY clause to sort the rows retrieved by a SELECT statement based on the values from one or more columns.

The value for the ORDER BY clause is a comma-separated list of column names. The columns in this list must also be in the SELECT clause of the query statement. Columns in the ORDER BY list can be from one or multiple tables. A number representing the relative position of a column in the SELECT clause may be used in place of a column name. Column correlation names can also be used in an ORDER BY clause columns list.

Use ASC (or ASCENDING) to force the sort to be in ascending order (smallest to largest), or DESC (or DESCENDING) for a descending sort order (largest to smallest). When not specified, ASC is the implied by default.

The statement below sorts the result set ascending by the year extracted from the lastinvoicedate column, then descending by the state column, and then ascending by the uppercase conversion of the company column.

SELECT EXTRACT(YEAR FROM lastinvoicedate) AS YY, state, UPPER(company)
FROM customer
ORDER BY YY DESC, state ASC, 3

Column references cannot be passed to an ORDER BY clause via parameters.

Applicability

SELECT