Progress
SQL-89
Guide and Reference


Sorting Data

The ORDER BY clause of a SELECT statement sorts query results by the values in one or more columns. You can specify the columns by name or by order within the SELECT statement. The ORDER BY clause has the following syntax.

SYNTAX
ORDER BY 
  { n | expression } [ ASC | DESC ]
  [ , n | , expression [ ASC | DESC ] ] ... 

The following example of the ORDER BY clause sorts the data alphabetically by state and, within each state, alphabetically by city:

SELECT Name, Address, City, State
  FROM Customer
  ORDER BY State, City. 

As an alternative to column names, you can specify integers in the ORDER BY clause. The integer refers to the position of the column in the column list. In the following example, the 2 refers to the SUM column.

SELECT Cat-Page, SUM(Price)
  FROM Item
  GROUP BY Cat-Page
  ORDER BY 2 DESC. 

Ascending is the default sort order. You can specify the keyword DESC to sort the results in descending order.

Two null values are considered equal within the context of GROUP BY, ORDER BY, and DISTINCT clauses. In ORDER BY clauses, null values are also considered greater than all non-null values. When the sort order is ascending, null values are sorted last. When the sort order is descending, null values are sorted first.

NOTE


Copyright © 2004 Progress Software Corporation
www.progress.com
Voice: (781) 280-4000
Fax: (781) 280-4095