Friday 6 May 2016

Sorting 2 columns in different order : SQL

To sort by multiple columns in SQL, and in different directions. column1 would be sorted descending, and column2 ascending.
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

No comments:

Post a Comment