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:

ID NAME AGE ADDRESS SALARY
1 Himani gupta 21 Modinagar 22000
2 Shiva tiwari 22 Bhopal 21000
3 Ajeet bhargav 45 Meerut 65000
4 Ritesh yadav 36 Azamgarh 26000
5 Balwant singh 45 Varanasi 36000
6 Mahesh sharma 26 Mathura 22000

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

This would produce the following result.

ID NAME AGE ADDRESS SALARY
3 Ajeet bhargav 45 Meerut 65000
5 Balwant singh 45 Varanasi 36000
1 Himani gupta 21 Modinagar 22000
6 Mahesh sharma 26 Mathura 22000
4 Ritesh yadav 36 Azamgarh 26000
2 Shiva tiwari 22 Bhopal 21000

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

This would produce the following result.

ID NAME AGE ADDRESS SALARY
2 Shiva tiwari 22 Bhopal 21000
4 Ritesh yadav 36 Azamgarh 26000
6 Mahesh sharma 26 Mathura 22000
1 Himani gupta 21 Modinagar 22000
5 Balwant singh 45 Varanasi 36000
3 Ajeet bhargav 45 Meerut 65000

Topics of SQL ORDER BY Clause





Contact US

Email:[email protected]

ORDER BY Clause
10/30