895

How can I sort multiple columns in SQL and in different directions? For instance, 'column1' would be sorted descendingly and 'column2' ascendingly.

Señor Reginold Francis
  • 16,318
  • 16
  • 57
  • 73

9 Answers9

1403
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.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • 4
    here how to order either column1 or column2? – PoliDev Jun 18 '13 at 08:47
  • 1
    @PoliDev, It first orders by column1 in DESCending order and the by column2 (in ASCending order) – zaheer Mar 13 '14 at 12:33
  • 175
    For clarity, this sorts everything by `column1` first and then by `column2` whenever the `column1` fields for two rows are equal. – Nick Benes May 29 '14 at 18:53
  • I assume that this will work for additional columns also? – user001 Oct 16 '15 at 11:17
  • 3
    It 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
  • 5
    @NickBenes ...or you could say: it sorts by `column2` and then performs STABLE sorting by `column1`. This is more clear for people that knows what stable sorting is. – Atom Oct 03 '16 at 13:49
  • @IgnacioVazquez-Abrams The result remains the same I mean it only order by first column not after that. – 3 rules Jan 16 '17 at 10:06
  • say i am having a result set with three columns in it. I wish to have first column in my result set in DESC, Second Column in result Set in ASC and third column in result set in DESC. How will i do it? – Deepak Keynes Mar 10 '18 at 07:19
  • @DeepakKeynas Put desc after the third one, asc can be ommited because it's the default, he could also put asc after column2. – Steve Moretz Aug 03 '21 at 20:03
  • what is the opposite of this? column 1, column2 DESC? – PirateApp Nov 02 '22 at 08:36
487

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
Thomas C. G. de Vilhena
  • 13,819
  • 3
  • 50
  • 44
  • 47
    This answer is a great supplement to the very helpful and short accepted answer. – enderland May 30 '14 at 20:07
  • 5
    The 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 – mfs Sep 28 '14 at 11:51
  • 1
    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
  • 3
    @ParameshKorrakuti: That's the expected outcome. The resulting ordering in your example would only differ if there were duplicate `FirstName, LastName` entries with distinct `YearOfBirth` – Thomas C. G. de Vilhena Apr 20 '18 at 14:38
148
SELECT  *
FROM    mytable
ORDER BY
        column1 DESC, column2 ASC
Robusto
  • 31,447
  • 8
  • 56
  • 77
Quassnoi
  • 413,100
  • 91
  • 616
  • 614
26

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.

enter image description here

Now I perform Order By in these two columns by executing below command:

enter image description here

Now again I insert new values in these two columns, where Alphabet value in ASC order:

enter image description here

and the columns in Example table look like this. Now again perform the same operation:

enter image description here

You can see the values in the first column are in desc order but second column is not in ASC order.

Akhil
  • 271
  • 1
  • 3
  • 11
Jason Clark
  • 1,307
  • 6
  • 26
  • 51
  • 2
    also insert this data too `(g, 10),(g,12)`. Then, run your order-by query, you get second column as `ASC` order(that means `g-10,g-11,g-12)` – Pugal May 05 '18 at 12:13
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 
Mohammad Atiour Islam
  • 5,380
  • 3
  • 43
  • 48
8
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).

4

Compiled through Intellij DataGrip

SELECT * FROM EMP ORDER BY DEPTNO ASC, JOB DESC;

Gaganam Krishna
  • 121
  • 1
  • 4
4

TRY

'select * FROM users ORDER BY id DESC, name ASC, age DESC 
MUHINDO
  • 788
  • 6
  • 10
2

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);
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • note that you'd need to specifically create indexes for those cases as a normal index on `FirstName` and `Town` wouldn't be used because of obfuscation more at https://planetscale.com/courses/mysql-for-developers/queries/index-obfuscation – Can Rau Apr 30 '23 at 16:13