How can I sort multiple columns in SQL and in different directions? For instance, 'column1' would be sorted descendingly and 'column2' ascendingly.
9 Answers
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.
-
5
-
2@PoliDev, It first orders by column1 in DESCending order and the by column2 (in ASCending order)– zaheerMar 13, 2014 at 12:33
-
182For clarity, this sorts everything by
column1
first and then bycolumn2
whenever thecolumn1
fields for two rows are equal. May 29, 2014 at 18:53 -
4It will work for any number of expressions (not just columns), up to your RDBMS's limit. Oct 16, 2015 at 11:27
-
5@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.– AtomOct 3, 2016 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
-
48This answer is a great supplement to the very helpful and short accepted answer. May 30, 2014 at 20:07
-
5The 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– mfsSep 28, 2014 at 11:51
-
1It'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? Apr 17, 2018 at 14:11 -
3@ParameshKorrakuti: That's the expected outcome. The resulting ordering in your example would only differ if there were duplicate
FirstName, LastName
entries with distinctYearOfBirth
Apr 20, 2018 at 14:38
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.
-
2also 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)
– PugalMay 5, 2018 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
-
3
SELECT id,
first_name,
last_name,
salary
FROM employee
ORDER BY salary DESC, last_name;
If you want to select records from a table but would like to see them sorted according to two columns, you can do so with ORDER BY. This clause comes at the end of your SQL query.
After the ORDER BY keyword, add the name of the column by which you’d like to sort records first (in our example, salary). Then, after a comma, add the second column (in our example, last_name). You can modify the sorting order (ascending or descending) separately for each column. If you want to use ascending (low to high) order, you can use the ASC keyword; this keyword is optional, though, as that is the default order when none is specified. If you want to use descending order, put the DESC keyword after the appropriate column (in the example, we used descending order for the salary column).
You can also sort or order by the Number of Characters in each Column you wish to sort by. Shown below is a sample which sorts by the first three characters of the First Name and by the last two characters in the name of the town.
SELECT *
FROM table_name
ORDER BY LEFT(FirstName, 3) ASC, LEFT(Town, 2);
-
note that you'd need to specifically create indexes for those cases as a normal index on
FirstName
andTown
wouldn't be used because of obfuscation more at planetscale.com/courses/mysql-for-developers/queries/…– Can RauApr 30, 2023 at 16:13