How to Use Order by а Functions in MySQL


The data stored in the databases does not follow any order; however its need arises when the data has to be displayed. For this purpose, we can use Order By mysql function.
The Order By function in MySQL is used to sort the result as required. With this function we can sort the results in ascending or descending order. In this tutorial we will learn to use this function.

Step 1 – Salaries
First of all, consider these salary records from an employee table.
Notice that the records are not sorted. What if we want to sort the data in ascending order.

Salaries View

Step 2 – Records in ascending order
For that, let us write a query in which we will select the Salary from the employee table, insert an Order By mysql clause, and insert the column name from where we need the data to be sorted, which in this case is the Salary column. Use the “asc” keyword and hit Enter.
The query would be:
select salary from employees
order by salary asc;
Ascending Order

Notice here that now the result is being displayed in ascending order.
Step 3- Records in Descending order
Similarly, in order to display the records in descending order, we will use the “desc” keyword over here and hit Enter.
Hence the query would be:
select salary from employees
order by salary desc;
Descending order

Now the data is being displayed in descending order
And this is how we can use functions in mysql.