Javatpoint Logo

91-9990449935

 0120-4256464

SQL ORDER BY Clause

The SQL ORDER BY clause is used for sorting data in ascending and descending order based on one or more columns.

Some databases sort query results in ascending order by default.

SQL ORDER BY syntax:

Let us take a CUSTOMERS table having the following records:

IDNAMEAGEADDRESSSALARY
1Himani gupta21Modinagar22000
2Shiva tiwari22Bhopal21000
3Ajeet bhargav45Meerut65000
4Ritesh yadav36Azamgarh26000
5Balwant singh45Varanasi36000
6Mahesh sharma26Mathura22000

This is an example that would sort the result in ascending order by NAME and SALARY.

This would produce the following result.

IDNAMEAGEADDRESSSALARY
3Ajeet bhargav45Meerut65000
5Balwant singh45Varanasi36000
1Himani gupta21Modinagar22000
6Mahesh sharma26Mathura22000
4Ritesh yadav36Azamgarh26000
2Shiva tiwari22Bhopal21000

This is an example to sort the result in descending order by NAME.

This would produce the following result.

IDNAMEAGEADDRESSSALARY
2Shiva tiwari22Bhopal21000
4Ritesh yadav36Azamgarh26000
6Mahesh sharma26Mathura22000
1Himani gupta21Modinagar22000
5Balwant singh45Varanasi36000
3Ajeet bhargav45Meerut65000

Topics of SQL ORDER BY Clause