Saturday, April 4, 2015

Order by clause in SQL server using multiple columns

Lets suppose we have a table containing order details or a bill and we want to sort the observations in descending order of price and ascending order of units we need to follow below steps:

select * from Order_Wallmart




















Now lets order the observations in descending order according to price


select * from Order_Wallmart order by price desc




Now observe that the Price column is arranged in descending order, but the Units column does not have any order. We will now sort it in ascending order

select * from Order_Wallmart order by price desc , units asc






















As we can see, the units are sorted in ascending order. We have to observe that the records first get sorted according to price in descending order and then are sorted in ascending order with respect to units.

No comments:

Post a Comment