Skip to main content

SELECT Order By

Sorts data returned by a query. Use this clause to order the result set of a query by the specified column list. The order in which rows are returned in a result set are not guaranteed unless an ORDER BY clause is specified.

bSQL Syntax Conventions

Syntax​

ORDER BY order_by_expression [ ASC | DESC ] [ ,...n ]    

Arguments​

order_by_expression
Specifies a column on which to sort the query result set. A sort column can be specified as a name or column alias.

  • Multiple sort columns can be specified.
  • Column names must be unique.
  • For now, the columns specified must be included in the select list.
  • The sequence of the sort columns in the ORDER BY clause defines the organization of the sorted result set. That is, the result set is sorted by the first column and then that ordered list is sorted by the second column, and so on. ASC | DESC
    Specifies that the values in the specified column should be sorted in ascending or descending order. ASC sorts from the lowest value to highest value. DESC sorts from highest value to lowest value. ASC is the default sort order. Null values are treated as the lowest possible values.

Examples​

A. Specifying a single column defined in the select list​

The following example orders the result set by the numeric price column. Because a specific sort order is not specified, the default (ascending order) is used.

SELECT *
FROM financial.pricing
ORDER BY price

B. Specifying an alias as the sort column​

The following example specifies the column alias current_price as the sort order column.

SELECT symbol, price AS current_price
FROM financial.pricing
ORDER BY current_price

C. Specifying a descending order​

The following example orders the result set by the numeric column price in descending order.

SELECT symbol, price AS current_price
FROM financial.pricing
ORDER BY price DESC

D. Specifying an ascending order​

The following example orders the result set by the symbol column in ascending order. The characters are sorted alphabetically, not numerically.

SELECT symbol, price
FROM financial.pricing
ORDER BY symbol ASC

See Also​