SQL Server / T-SQL Tutorial Scenario:
How to use Sorting ( Order By) in Select Statement in SQL Server – SQL Server / TSQL Tutoria
The Order by Keyword can be used to sort the result set. We can sort the results by single or multiple columns.
Let’s create dbo.Customer table with couple of test records and then use Order by keyword to sort them.
1) When we only use Order by ColumnName
Let’s say if you would like to sort the data by using Id column and in ascending order, you can use below query.
Notice that in first query, I did not write ASC to order the data in ascending order. In second query I did use ASC key word. If you don’t use any keyword after column names, SQL Server will order the data in ascending order.
2) How to sorting data in desending order
To order results in desending order, you have to add DESC keyword after column names. Let’s say if we would like to order the data by using FName in desending oder, we will use below query.
3) Using Column Numbers…
sql tutorials for beginners