ORDER BY Clause

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 (alias (SQL): An alternative name for a table or field in expressions. Often used to shorten the table or field name for subsequent references in code, to prevent possible ambiguous references, or to provide a more descriptive name in query output.), 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.
selectcriteria Selection criteria. If the statement includes a WHERE clause, the Microsoft Access 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 (sort order: A way to arrange data based on value or data type. You can sort data alphabetically, numerically, or by date. Sort orders use an ascending (0 to 100, A to Z) or descending (100 to 0, Z to A) 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 (Memo data type: In a Microsoft Access database, this is a field data type. Memo fields can contain up to 65,535 characters.) or OLE Object (OLE Object data type: A field data type that you use for objects created in other applications that can be linked or embedded (inserted) in an Access database.) data in the ORDER BY clause, an error occurs. The Microsoft Access database engine does not sort on fields of these types.

ORDER BY is usually the last item in an SQL statement (SQL string/statement: An expression that defines an SQL command, such as SELECT, UPDATE, or DELETE, and includes clauses such as WHERE and ORDER BY. SQL strings/statements are typically used in queries and in aggregate functions.).

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.

 
 
Applies to:
Access 2007