SQL ORDER BY Descending

The SQL ORDER BY DESC is used to sort records in the result set in descending order. In order to sort the data in descending order, you should use the DESC modifier after the ORDER BY clause.

Syntax

SELECT expressions 
FROM tables 
[WHERE conditions] 
ORDER BY expression DESC;

Here,

  • expressions – expressions defined here the column(s) or calculation you want to retrieve. If you want to retrieve all the columns simply use * in the place of expressions.
  • tables – one or more than one table from where you want to retrieve data.
  • WHERE conditions – Optional. This is used to specify some conditions while selecting data. In case you are not using the WHERE clause, all the rows available will be selected.
  • ORDER BY – This argument is used to sort the result set. If you want to sort on more than one column, you need to provide them in comma-separated.
  • DESC – DESC sorts the result set in descending order. This is the default behavior if no modifier is mentioned.

Example

Consider an Employee table with the following data.

ID NAME GENDER AGE SALARY
1510 Avinash M 35 33000
1511 Ramjan M 31 56000
1512 Priyanka F 32 35000
1513 Priyam M 25 42000
1514 Jitu M 23 43000
1515 Raman M 26 49000
1516 Lata F 25 27000
1517 Prakash M 38 45000
1518 Nitu F 40 45000
1519 Pallab M 33 42000
1520 Pankaj M 39 55000

The below statement sorts the result in ascending order by NAME.

SELECT * FROM Employee
ORDER BY NAME DESC;

This produces the following result:

ID NAME GENDER AGE SALARY
1511 Ramjan M 31 56000
1515 Raman M 26 49000
1517 Prakash M 38 45000
1512 Priyanka F 32 35000
1513 Priyam M 25 42000
1520 Pankaj M 39 55000
1519 Pallab M 33 42000
1518 Nitu F 40 45000
1516 Lata F 25 27000
1514 Jitu M 23 43000
1510 Avinash M 35 33000

 

Leave a Comment