Saturday, May 7, 2022

ORDER BY Clause [MS]

The ORDER BY clause in Access sorts a query's resulting records on a specified field or fields in ascending or descending order.


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:




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.


The name of the table from which records are retrieved.


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.


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

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 Access database engine does not 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.

No comments:

Post a Comment

Search This Blog

Use a headset or Bluetooth device for a Lync call [MS]

Use a headset or Bluetooth device for a Lync call Lync 2013 for Android ...