ORDER BY Clause

Description

Sorts a query's resulting records on a specified field or fields in ascending or descending order.

Syntax

SELECT fieldlist
FROM table
WHERE selectcriteria
[ORDER BY field1 [ASC | DESC ][, field2 [ASC | DESC ]][, ...]]]

A SELECT statement containing an ORDER BY clause has these parts:

Part

Description

fieldlist

The name of the field or fields to be retrieved along with any field-name aliases, SQL aggregate functions, selection predicates (ALL, DISTINCT, DISTINCTROW, or TOP), or other SELECT statement options.

table

The name of the table from which records are retrieved. For more information, see the FROM clause.

selectcriteria

Selection criteria. If the statement includes a WHERE clause, the Microsoft Jet database engine orders values after applying the WHERE conditions to the records.

field1, field2

The names of the fields on which to sort records.


Remarks   ORDER BY is optional. However, if you want your data displayed in sorted order, then you must use ORDER BY.

The default sort order is ascending (A to Z, 0 to 9). Both of the following examples sort employee names in last name order:

SELECT LastName, FirstName
FROM Employees
ORDER BY LastName;
SELECT LastName, FirstName
FROM Employees
ORDER BY LastName ASC;
To sort in descending order (Z to A, 9 to 0), add the DESC reserved word to the end of each field you want to sort in descending order. The following example selects salaries and sorts them in descending order:

SELECT LastName, Salary
FROM Employees
ORDER BY Salary DESC, LastName;
If you specify a field containing Memo or OLE Object data in the ORDER BY clause, an error occurs. The Microsoft Jet database engine doesn't sort on fields of these types.

ORDER BY is usually the last item in an SQL statement.

You can include additional fields in the ORDER BY clause. Records are sorted first by the first field listed after ORDER BY. Records that have equal values in that field are then sorted by the value in the second field listed, and so on.

See Also   ALL, DISTINCT, DISTINCTROW, TOP predicates, FROM clause, GROUP BY clause, HAVING clause, SELECT statement, SELECT...INTO statement, WHERE clause.

Specifics (Microsoft Access)

In Microsoft Access, an ORDER BY clause sorts the displayed data on a specified field or fields in ascending or descending order. The use of an ORDER BY clause is equivalent to clicking Ascending or Descending in a Sort cell in the query design grid.

Example

The SQL statement shown in the following example uses the ORDER BY clause to sort records by last name in descending order (Z to A).

This example calls the EnumFields procedure, which you can find in the SELECT statement example.

Sub OrderByX()

    Dim dbs As Database, rst As Recordset

    ' Modify this line to include the path to Northwind
    ' on your computer.
    Set dbs = OpenDatabase("Northwind.mdb")

    ' Select the last name and first name values from
    ' the Employees table, and sort them in descending
    ' order.
    Set rst = dbs.OpenRecordset("SELECT LastName, " _
        & "FirstName FROM Employees " _
        & "ORDER BY LastName DESC;")

    ' Populate the Recordset.
    rst.MoveLast

    ' Call EnumFields to print recordset contents.
    EnumFields rst, 12

    dbs.Close

End Sub
Example (Microsoft Access)

To try the following examples in Microsoft Access, first create a new query in the Northwind sample database. Close the Show Table dialog box without specifying a table or query. Switch to SQL view, paste an individual example into the SQL window, and run the query.

The following SQL statements use the ORDER BY clause to sort records alphabetically and then by category.

This example sorts the records by last name in descending order (Z to A):

SELECT LastName, FirstName FROM Employees ORDER BY LastName DESC;
The next example sorts by category ID first, and then by product name:

SELECT CategoryID, ProductName, UnitPrice FROM Products
ORDER BY CategoryID, ProductName;