Using SELECT Statement with ORDER BY Clause - SQL Query
SQL ORDER BY clause is used to sort data in ascending and descending order. Some databases sort query results in ascending order by default.
Consider the following table 'Students'.
Stud_ID | Name | Phone | City | Country |
---|
1 | Alex | 654124 | Perth | Australia |
2 | Martin | 654125 | Sydney | Australia |
3 | Shruti | 910001 | Delhi | India |
4 | Jaya | 910002 | Mumbai | India |
5 | Paul | 450525 | London | England |
6 | Andrew | 450526 | London | England |
I) Display column Name in ascending order from table Students.
The following query will sort name column in ascending order.
SELECT Name FROM Students
ORDER BY Name
Result:
Name |
---|
Alex |
Andrew |
Jaya |
Martin |
Paul |
Shruti |
II) Display names of the students from the table 'Students' in descending order.
SELECT * FROM Students
ORDER BY Name DESC
The following query will display the names of the students in descending order.
Stud_ID | Name | Phone | City | Country |
---|
3 | Shruti | 910001 | Delhi | India |
5 | Paul | 450525 | London | England |
2 | Martin | 654125 | Sydney | Australia |
4 | Jaya | 910002 | Mumbai | India |
6 | Andrew | 450526 | London | England |
1 | Alex | 654124 | Perth | Australia |