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. |
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.
The following examples show ordering a result set.
Ordering by the numerical ProductID column. The default is ascending order.
USE AdventureWorks2008R2; GO SELECT ProductID, Name FROM Production.Product WHERE Name LIKE 'Lock Washer%' ORDER BY ProductID ;
Ordering by the numerical ProductID column in descending order.
SELECT ProductID, Name FROM Production.Product WHERE Name LIKE 'Lock Washer%' ORDER BY ProductID DESC;
Ordering by the Name column. Note that the characters are sorted alphabetically, not numerically. That is, 10 sorts before 2.
SELECT ProductID, Name FROM Production.Product WHERE Name LIKE 'Lock Washer%' ORDER BY Name ASC ;
Ordering by two columns. This query first sorts in ascending order by the FirstName column, then sorts in descending order by the LastName column.
SELECT LastName, FirstName FROM Person.Person WHERE LastName LIKE 'R%' ORDER BY FirstName ASC, LastName DESC ;
Just to mentioned one more thing here which i think is missing in ths article;
--Columns in the ORDER BY list must be unique
USE AdventureWorks
GO
SELECT * FROM Sales.Customer
ORDER BY CustomerID,CustomerID
SELECT * FROM Sales.Customer
ORDER BY CustomerID,1
SELECT * FROM Sales.Customer
ORDER BY 1,CustomerID
All of these above SQL Statements are incorrect and result in following error;
A column has been specified more than once in the order by list. Columns in the order by list must be unique.
- 10/18/2011
- Hasham Niaz
GO
SELECT FirstName, LastName
FROM Person.Contact
ORDER BY CASE LastName
WHEN 'Wright' THEN '0'
WHEN 'Jenkins' THEN '1'
WHEN 'Torres' THEN '2'
WHEN 'Sanchez' THEN '3'
ELSE LastName
END, FirstName
For more check the links:
http://sqlwithmanoj.wordpress.com/2011/02/22/order-by-with-case/
http://sqlwithmanoj.wordpress.com/2011/04/09/more-on-order-by-with-case/
The ORDER BY clause gives highest sorting precedence to the first expression, and lesser precedence to all subsequent expressions. For example:
Would return the following table:DECLARE @Person TABLE ( LastName VARCHAR(100), FirstName VARCHAR(100) )
INSERT INTO @Person ( LastName, FirstName )
SELECT 'Macintire', 'Mike'
UNION ALL SELECT 'MacDonnel', 'Mat'
UNION ALL SELECT 'MacHenry', 'Mat'
UNION ALL SELECT 'Maclintoc', 'Mark'
SELECT LastName, FirstName FROM @Person
ORDER BY FirstName ASC, LastName DESC ;
LastName FirstName
Maclintoc Mark
MacHenry Mat
MacDonnel Mat
Macintire Mike
- 2/1/2011
- Imponderable
- 2/1/2011
- Imponderable
Note