3. MySQL: ORDER BY Keyword

3. MySQL: ORDER BY Keyword

We ended the last blog by learning the use of the three operators; now, we will look at a keyword that sets our output in order.

Points to remember:

  • When you want to arrange the result in ascending or descending order, we use ORDER-BY.

  • The results are automatically sorted in ascending order by the ORDER BY keyword.

  • Use the DESC keyword to sort the records such that they are in descending order.

  • When we have multiple parameters(columns) with ORDER BY, it will sort by the first parameter(column). If values for the first parameter(column) are equal, it sorts according to the second one. In short, the first column has the highest priority.

ORDER BY Keyword:

The ORDER BY keyword is a SQL command used to sort the results of a SELECT, UPDATE, DELETE, or INSERT statement in a specific order. It allows you to specify the criteria by which the rows in the result set will be sorted.

To use the ORDER BY keyword, specify the column or columns you want to use for sorting and whether the sort should be in ascending or descending order.

  • The syntax is :
SELECT column_1, column_2, column_3 
FROM table_name 
WHERE condition_1 
ORDER BY column_1;
  • Let us look at a snippet of the contents of the film table.
SELECT * FROM film;

Output:

  1. Now we want to find the films with the lowest replacement costs. So the query goes like this:
Select title, replacement_cost 
from film 
order by replacement_cost;

Output *:

  1. When we have multiple parameters(columns) with ORDER BY, it will sort by the first parameter(column). If values for the first parameter(column) are equal, it sorts according to the second one. Eg.

     Select title, length, replacement_cost
     from film
     order by length, replacement_cost;
    

Output *: Here, we see that when the length is the same, we use the replacement_cost

  1. To Sort in Descending, we use the DESC keyword after the parameter(column) name. E.g. the same query is in point 1, but now we want the replacement costs to be arranged in descending order.

     Select title, replacement_cost 
     from film 
     order by replacement_cost desc;
    

Output *:

  1. Now we use Ascending and Descending together; the output displayed is just how we read the query.

    Suppose we have mentioned ascending first and descending second. In that case, we will get the result sorted by ascending for the 1st column and descending for the 2nd if the ascending column has similar values and vice versa.

    (Reread it slowly. It will make sense:p)

    Compare the below output with the output for point number 2 and notice the difference.

     Select title, length, replacement_cost
     from film
     order by length asc, replacement_cost desc;
    

Output *: Here, we sort by ascending length, and if the length value is the same, we sort it by replacement_cost desc.

So here we have covered the ORDER BY Keyword.

We'll meet again tomorrow :)

*****All the outputs provided are a snippet of the actual result.