Using MS SQL Order By
The ORDER BY function is used to sort the records in a query written in MS SQL. Straight or Reverse sorting can be done.
For reverse sorting, DESC must be written at the end of the field or fields to be sorted. For straight sorting, nothing needs to be written, but in some cases ASC is required.
Let's sort the records of our table named PRODUCTS according to the NAME field in reverse.
NAME | PRICE |
---|---|
Fruit Juice | 5 |
Cake | 10 |
Beer | 12 |
Soda | 8 |
Water | 6 |
Salt | 15 |
Sugar | 16 |
The query to be written for this should be as follows.
SELECT * FROM PRODUCTS ORDER BY NAME DESC
Bize döndüreceği sonuç aşağıdaki gibi olur.
NAME | PRICE |
---|---|
Water | 6 |
Sugar | 16 |
Soda | 8 |
Salt | 15 |
Fruit Juice | 5 |
Cake | 10 |
Beer | 12 |