The order by function is used to help us order our results, this could be in ascending order, in descending order, or could be based on either numerical value or text value.
In such cases, we can use the ORDER BY keyword to achieve our goal.
Let's create the table we will use for our examples:
Script:
Check the table to see the data and the structure.
Very good
Ex 1 :
-we will order our result by alphabetical order.
-to do so we need to apply our ORDER BY function to a field that holds string values.
-we see that the id changed their order and the order manner is dictated by Name column, which is in alphabetical order now (from a to z)
We can also order them from z to a by using the DESC (descending) function at the end of our query.
We can observe that the order is descending now (from z-a).
Note : by default the order by function will sort the values in ascending way, still we have a ASC function we can use.
Example of ASC function
-we can see that the result is the same as above when not using the ASC function.
Still the ASC is often used .
Note : all of the above rules apply for numeric and other values as well.