| SQL Order By |
|
In the previous few tutorials, you have learned how to select and filter out data from the database. However, there are times you might want to sort the data in a particular order and this could be either descending or ascending. When sorting the data you could sort it base on the numerical value or alphabetical value. In order to achieve the sorting result we must use the ORDER BY keyword. SYNTAX: The ORDER BY keyword does not need to be used in conjunction with the WHERE keyword, however if you have a WHERE clause in your SQL statement then the ORDER BY keyword must be place after the WHERE clause. The ASC means that the return result will be sorted in ascending order and the DESC means that the return result will be sorted in the descending order. The ORDER BY keyword also allow you to sort base on multiple column. For example if you want to sort by the First name and then by the last name then you would have to use the ORDER BY clause like this: Table: Student
Example #1 In this example we will attempt to sor the result by student Firstname ascending. SELECT * FROM Student ORDER BY FirstName ASC RESULT:
Example #2 Sort by multiple columns. SELECT * FROM Student ORDER BY FirstName, LastName ASC RESULT:
Example #3 Sort by descending. SELECT * FROM Student ORDER BY FirstName, LastName DESC RESULT:
|