The ORDER BY clause sorts query results by one or more columns. By default the rows in a result set returned from a SELECT query are not in any particular order. To have your result set in a particular order, you will need to sort the results using the order by clause.
To sort by a single column, simply name that column in the ORDER BY clause.
SELECT column, column
FROM table
ORDER BY column;
Note that columns in the ORDER BY clause do not have to appear in the SELECT clause.
To sort by multiple columns, comma-delimit the column names in the ORDER BY clause.
SELECT column, column
FROM table
ORDER BY column, column;
Sample Database All the examples on this page use the:
AdventureWorks Lightweight (LT) Sample Database.
Pubs Sample Database or
Northwind Sample Database.
if you don't have an instance of these databases running on your pc, please follow the links to download and install them.
.
The following query example SELECTs the Name and ListPrice the FROM the Products table with the results presented in no particular order.
SELECT Name, ListPrice
FROM SalesLT.Product;
This next example adds an ORDER BY clause to the query that sorts the results in alphabetical order by Name.
SELECT Name, ListPrice
FROM SalesLT.Product
ORDER BY Name;
The following example orders the result set by a column that is not included in the select list, but is defined in the table specified in the FROM clause.
.
-- Note that column ListPrice in the ORDER BY clause does not have to appear in the SELECT clause.
SELECT ProductNumber, Name, Color
FROM SalesLT.Product
ORDER BY ListPrice;
GO
Note: The query results are listed in ascending order of ListPrice. By default, the ORDER BY clause applies an ascending sort order to the specified field. To sort the results into descending order of ListPrice, i.e. ORDER BY ListPrice DESC
You have the option of sorting in ascending (ASC) or descending (DESC). If neither is specified, records are sorted in ascending order (ASC) by default. To sort records in descending order, you will need to add the DESC keyword.
SELECT column, column
FROM table
ORDER BY column DESC, column ASC;
For example, the following query orders the result set by the ListPrice column in descending order, sorted by the most expensive items first.
.
/*
Select the Name and ListPrice columns from the SalesLT.Product table.
Sort by ListPrice in descending order.
*/
SELECT Name, ListPrice
FROM SalesLT.Product
ORDER BY ListPrice DESC;
GO
The following query example orders the result set by the Name column in ascending order. The characters are sorted alphabetically, not numerically. That is, 10 sorts before 2.
.
--
USE AdventureWorks2022;
GO
SELECT ProductID, Name FROM SalesLT.Product
WHERE Name LIKE 'LL Road Frame - Red, 52%'
ORDER BY Name ASC;
GO
This query sorts the results into descending order of ListPrice, and then into ascending order of Name.
.
--
SELECT Name, ListPrice
FROM SalesLT.Product
ORDER BY ListPrice DESC, Name ASC;
GO
The following example orders the result set by two columns. The query result set is first sorted in ascending order by the FirstName column and then sorted in descending order by the LastName column.
.
--
SELECT LastName, FirstName FROM SalesLT.Customer
WHERE LastName LIKE 'C%'
ORDER BY FirstName ASC, LastName DESC;
GO
.
--
GO
.
--
GO
.
--
GO
This query returns results ordered by ascending pub_id:
Here is the result set:
If more than one column is named in the ORDER BY clause, sorts are nested. The following statement sorts the rows in the titles table, first by publisher in descending order, and then by type in ascending order within each publisher, and finally by price (also ascending, because DESC is not specified).
You cannot use ORDER BY on columns that have the text or image data types.
Subqueries, aggregates, and constant expressions are not allowed in the ORDER BY list.
You can however, use a user-specified name in the select list for aggregates or expressions, as in the example below: