Order by keyword in MS SQL Server


ORDER BY keyword in SQL Server is used to sort / order result by ascending or descending format.

SQL Server provides tow keyword to sort result that are
1) ASC : To sort result in ascending format.
2) DESC : To sort result in descending format.

Let consider below list of shopping items, and we are going to apply ORDER BY keyword in this table.

select record using order by clause in sql server

Sort table data ascending format in SQL Server using ORDER BY


Select the product from Shopping table where product price greater than 15000 and order by product name ascending order.

Example

SELECT * FROM tblShoppingList
WHERE Price >= 15000 ORDER BY ProductName ASC

Output

order record by asacending order in sql server


Note : SQL Server by default provide the sorting in ascending format ASC keyword is not mandatory to sort record set in ascending format.


Sort table data descending format in SQL Server using ORDER BY


Select the product from Shopping table order by price in descending format.
In descending order data get display from large to small value.

Example

SELECT * FROM tblShoppingList
ORDER BY Price DESC

Output

order record in descending order in sql server


Order By on multiple columns


SQL Server provides the facility to sort data in ascending and descending format.

Example

SELECT * FROM tblShoppingList
ORDER BY DealerCountry, ProductName DESC , Price ASC

Output

order by record using asacending and descending example order in sql server


Note : If you see the above example we use order by ASC, DESC in same query. In above example we use the DealerCountry, Price in ascending format and ProductName in descending order.