How can I sort by multiple columns in SQL and in different directions. column1
would be sorted descending, and column2
ascending.
-
94This IS the "googling answers" first result. At least it was when I googled "sql ordering by two columns". It's a hell of a lot more readable than the equivalent official doc page which didn't even appear in my first page of results until I changed my query to "mysql 'order by'" – Andrew Martin Jan 30 '14 at 12:14
-
13Given how often a SO question comes top of Google, I always find it terrible that people answer with it. SO is either here to answer or not, and I can not understand why directing site traffic away is a good thing – user001 Oct 16 '15 at 11:21
ORDER BY column1 DESC, column2
This sorts everything by column1
(descending) first, and then by column2
(ascending, which is the default) whenever the column1
fields for two or more rows are equal.
-
2
-
@PoliDev, It first orders by column1 in DESCending order and the by column2 (in ASCending order) – zaheer Mar 13 '14 at 12:33
-
132For clarity, this sorts everything by
column1
first and then bycolumn2
whenever thecolumn1
fields for two rows are equal. – Nick Benes May 29 '14 at 18:53 -
2It will work for any number of expressions (not just columns), up to your RDBMS's limit. – Ignacio Vazquez-Abrams Oct 16 '15 at 11:27
-
2@NickBenes ...or you could say: it sorts by
column2
and then performs STABLE sorting bycolumn1
. This is more clear for people that knows what stable sorting is. – Atom Oct 3 '16 at 13:49
The other answers lack a concrete example, so here it goes:
Given the following People table:
FirstName | LastName | YearOfBirth
----------------------------------------
Thomas | Alva Edison | 1847
Benjamin | Franklin | 1706
Thomas | More | 1478
Thomas | Jefferson | 1826
If you execute the query below:
SELECT * FROM People ORDER BY FirstName DESC, YearOfBirth ASC
The result set will look like this:
FirstName | LastName | YearOfBirth
----------------------------------------
Thomas | More | 1478
Thomas | Jefferson | 1826
Thomas | Alva Edison | 1847
Benjamin | Franklin | 1706
-
30This answer is a great supplement to the very helpful and short accepted answer. – enderland May 30 '14 at 20:07
-
3The is some good example, many think that how can you sort 2 columns at single time which actually does not happen even if you put 2 columns for order query – Muhammad Faraz Sep 28 '14 at 11:51
-
It's providing same results when we sort with three columns and first column sorting order is same and rest everything is differ. Ex: :
1.Firstname asc, Lastname desc, yearOfBirst asc and 2.Firstname asc, Lastname desc, yearOfBirst desc
Is there any way we can overcome this? – Paramesh Korrakuti Apr 17 '18 at 14:11 -
1@ParameshKorrakuti: That's the expected outcome. The resulting ordering in your example would only differ if there were duplicate
FirstName, LastName
entries with distinctYearOfBirth
– Thomas C. G. de Vilhena Apr 20 '18 at 14:38 -
1
Multiple column ordering depends on both column's corresponding values: Here is my table example where are two columns named with Alphabets and Numbers and the values in these two columns are asc and desc orders.
Now I perform Order By in these two columns by executing below command:
Now again I insert new values in these two columns, where Alphabet value in ASC order:
and the columns in Example table look like this. Now again perform the same operation:
You can see the values in the first column are in desc order but second column is not in ASC order.
-
also insert this data too
(g, 10),(g,12)
. Then, run your order-by query, you get second column asASC
order(that meansg-10,g-11,g-12)
– Pugal May 5 '18 at 12:13
You can use multiple ordering on multiple condition,
ORDER BY
(CASE
WHEN @AlphabetBy = 2 THEN [Drug Name]
END) ASC,
CASE
WHEN @TopBy = 1 THEN [Rx Count]
WHEN @TopBy = 2 THEN [Cost]
WHEN @TopBy = 3 THEN [Revenue]
END DESC
-
2