JetSQL » Clauses » ORDER BY

Syntax:
SELECT fieldlist
FROM table
WHERE criteria
[ ORDER BY field1 [ ASC | DESC ] [ , field2 [ ASC | DESC ] ] [, ...] ]
fieldlist
The list of fields that are to be retrieved from the table.
table
The name of the table the information is being retrieved from.
criteria
The condition(s) that dictate which rows are to be retrieved.
field1
The parameter(s) that specify the columns on which to sort the data.
ASC | DESC
Specifies whether the results of the query should be sorted in ascending or descending order.

The ORDER BY clause can be used to dictate the order of the records returned (i.e., how they are sorted).

Examples

Code:
SELECT ToonType AS Type, CharacterName AS Name FROM Toons
WHERE ToonType = 'Duck' OR ToonType = 'Mouse'
ORDER BY ToonType, CharacterName;
Output:
TypeName
DuckDaffy Duck
DuckDaisy Duck
DuckDonald Duck
MouseJerry
MouseMickey Mouse
MouseMinnie Mouse


(6 row(s) affected)
Explanation:

The preceding example returns records listed primarily in order of toon type (duck then mouse), and then for each type the relevant names are also listed in alphabetical order.

Language(s): MS SQL Server
Code:
SELECT Item, Unitprice AS Price
FROM Products
ORDER BY Item, UnitPrice DESC;
Output:
ItemPrice
Dell Axim X30379.00
Dell Axim X30279.00
Dell Axim X30249.00
Dell Flat Panel LCD Monitor1000.00
Dell Flat Panel LCD Monitor900.00
Dell Flat Panel LCD Monitor700.00
Dell Inspiron 26501800.00
Dell Inspiron 26501650.00
Dell Inspiron 26501500.00
Dell Inspiron 300m1600.00
Dell Inspiron 300m1600.00
Intellisense Mouse20.00
Intellisense Mouse18.00
Travelstar 30gb Hard Drive100.00


(14 row(s) affected)
Explanation:

You can specify descending order (the default is ascending order) by adding the reserved word DESC after the field name. This example returns a list of all items in ascending alphabetical order along with their prices in descending numerical order.

Language(s): MS SQL Server

See Also: