SQL Order By Clause | Sorting Data in SQL | Learn SQL

SQL Order By

SQL Order by clause is used with Select command or statement for arranging retrieved data in sorted order based on one or more columns. The Order by clause by default sort data in ascending order it means A–>Z, To sort data in descending order DESC keyword is used with Order by clause.

Syntax

The syntax for the ORDER BY clause in SQL is:

SQL Order By | Tech-Mirror
SQL Order By | Tech-Mirror


Let us take a Employees table having the following records:

ID NAME AGE ADDRESS SALARY
1 Himani gandhi 21 Modinagar 25000
2 Shivam tia 25 Bhopal 26000
3 Ajay bhargav 40 Meerut 65000
4 Rita yadav 36 Azamgarh 21000
5 Baldev singh 42 Varanasi 35000

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

ID NAME AGE ADDRESS SALARY
3 Ajay bhargav 40 Meerut 65000
5 Baldev singh 42 Varanasi 35000
1 Himani gandhi 21 Modinagar 25000
4 Ritesh yadav 36 Azamgarh 21000
2 Shivam tia 25 Bhopal 26000

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

ID NAME AGE ADDRESS SALARY
2 Shivam tia 25 Bhopal 26000
4 Ritesh yadav 36 Azamgarh 21000
1 Himani gandhi 21 Modinagar 25000
5 Baldev singh 42 Varansi 35000
3 Ajay bhargav 40 Meerut 65000

For More SQL Select 

Leave a Reply

Your email address will not be published. Required fields are marked *