ORDER BY Clause
Note
Indexing Service is no longer supported as of Windows XP and is unavailable for use as of Windows 8. Instead, use Windows Search for client side search and Microsoft Search Server Express for server side search.
The ORDER BY clause sorts the rows returned in the rowset according to a specified set of criteria. The ORDER BY clause is an optional part of the SELECT statement.
SELECT Select_List | *
FROM_Clause
[WHERE_Clause]
[ORDER BY Sort_Column [ASC | DESC]
[,Sort_Column [ASC | DESC]]
... ]
Parameters
-
Select_List
-
Specifies the list of column aliases (properties) making up the table (rowset) that is returned as a result of the query.
-
* (asterisk)
-
Specifies all columns. This option is valid only when the FROM_Clause parameter references a predefined view or a temporary view.
-
FROM_Clause
-
Specifies the files on which to perform the search. For details about this parameter, see FROM Clause.
-
WHERE_Clause
-
Specifies the search condition for selecting rows in the virtual table defined by the FROM_Clause parameter. The matching rows make up the resulting rowset. This clause is optional. For details about this parameter, see WHERE Clause.
-
Sort_Column
-
Specifies the name of the column to be sorted or the ordinal position of the column.
-
ASC | DESC
-
Specifies the sorting order, either ascending (ASC) or descending (DESC). If you do not specify the sort order, the columns are sorted in ascending order. However, if a column is explicitly marked ascending or descending, succeeding columns will use that same sort order until another column in the list is explicitly marked in the other order.
Examples
Notice the order in the columns in the following example:
- Col1 is ascending (by default).
- Col2 is descending (explicitly stated).
- Col3 and Col4 is descending (implicit, same as last keyword).
- Col5 is ascending (explicitly stated).
SELECT Col1, Col2, Col3, Col4, Col5
FROM SCOPE()
WHERE Col1 > 10
ORDER BY Col1, Col2 DESC, Col3, Col4, Col5 ASC
The following example is equivalent to the previous example, but it refers to the columns by their ordinal position.
SELECT Col1, Col2, Col3, Col4, Col5
FROM SCOPE()
WHERE Col1 > 10
ORDER BY 1, 2 DESC, 3, 4, 5 ASC
Related topics