ORDER BY Clause (Transact-SQL)

Specifies the sort order used on columns returned in a SELECT statement. The ORDER BY clause is not valid in views, inline functions, derived tables, and subqueries, unless TOP is also specified.

Note

When ORDER BY is used in the definition of a view, inline function, derived table, or subquery, the clause is used only to determine the rows returned by the TOP clause. The ORDER BY clause does not guarantee ordered results when these constructs are queried, unless ORDER BY is also specified in the query itself.

Topic link iconTransact-SQL Syntax Conventions

Syntax

[ ORDER BY 
    {
    order_by_expression 
  [ COLLATE collation_name ] 
  [ ASC | DESC ] 
    } [ ,...n ] 
] 

Arguments

  • order_by_expression
    Specifies a column on which to sort. A sort column can be specified as a name or column alias, or a nonnegative integer representing the position of the name or alias in the select list. An integer cannot be specified when the order_by_expression appears in a ranking function. A sort column can include an expression, but when the database is in SQL Server 2005 (90) compatibility mode the expression cannot resolve to a constant. Column names and aliases can be qualified by the table or view name. In SQL Server 2005, qualified column names and aliases are resolved to columns listed in the FROM clause. If order_by_expression is not qualified, the value must be unique among all columns listed in the SELECT statement.

    Multiple sort columns can be specified. The sequence of the sort columns in the ORDER BY clause defines the organization of the sorted result set.

    The ORDER BY clause can include items that do not appear in the select list. However, if SELECT DISTINCT is specified, or if the statement contains a GROUP BY clause, or if the SELECT statement contains a UNION operator, the sort columns must appear in the select list.

    Additionally, when the SELECT statement includes a UNION operator, the column names or column aliases must be those specified in the first select list.

    Note

    ntext, text, image, or xml columns cannot be used in an ORDER BY clause.

  • COLLATE {collation_name}
    Specifies that the ORDER BY operation should be performed according to the collation specified in collation_name, and not according to the collation of the column as defined in the table or view. collation_name can be either a Windows collation name or a SQL collation name. For more information, see Collation Settings in Setup and Using SQL Collations. COLLATE is applicable only for columns of the char, varchar, nchar, and nvarchar data types.
  • ASC
    Specifies that the values in the specified column should be sorted in ascending order, from lowest value to highest value.
  • DESC
    Specifies that the values in the specified column should be sorted in descending order, from highest value to lowest value.

Remarks

Null values are treated as the lowest possible values.

There is no limit to the number of items in the ORDER BY clause. However, there is a limit of 8,060 bytes for the row size of intermediate worktables needed for sort operations. This limits the total size of columns specified in an ORDER BY clause.

When used together with a SELECT...INTO statement to insert rows from another source, the ORDER BY clause does not guarantee the rows are inserted in the specified order.

See Also

Reference

Expressions (Transact-SQL)
SELECT (Transact-SQL)
FROM (Transact-SQL)

Other Resources

Sorting Rows with ORDER BY

Help and Information

Getting SQL Server 2005 Assistance

Change History

Release History

14 April 2006

New content:
  • Added clarification about the purpose of ORDER BY in an inline function, derived table, or subquery definition.

5 December 2005

New content:
  • Added clarification about the purpose of ORDER BY in a view definition.