Thursday, July 28, 2011

Multiple ORDER BY in SQL Query

If you want to sort your SQL Table with multiple columns then following article will give you an idea how to go about it.

Single ORDER BY can written as follows








And it will give following result which is ordered by assigned column:








Now you want to sort status  column as well while assigned column is already sorted.. 



So In general you can have SELECT * FROM TABLE ORDER BY column1 DESC, column2 DESC


If you want to include the WHERE statement as well then  
SELECT * FROM TABLE WHERE column1 > 100 ORDER BY column1 DESC, column2 DESC

And DESC is for descending order sorting and ASC is for ascending order sorting.


No comments:

Post a Comment