Whenever a MySQL statement is executed, its result is displayed. But this result is not in a particular order. The rows that are true for the statement are returned one after the other.
For example, if you want a list of category name names from category table, the name will be displayed in the order you were inserted in the name. But if you want all the names to be show in alphabet order (b followed by a name and then starting with c), you can use ORDER BY clause in the statement for it.
The basic syntax of ORDER BY clause is being given below.
mysql > SELECT <column_name> FROM <table_name> ORDER BY <column_name> <option>
In the above given syntax, the option is ascending and descending. If you do not use any options, then by default result is shown in the ascending order. If you want to show the result in the descending order, then you can use the desc option.
Let’s try to understand ORDER BY clause through an example.
mysql> select Name from category ORDER BY name;
The values in the name column are being shown from the category table in the above statement. All these values will be shown in the ascending order. If you show the whole table’s data without showing a particular column then all the rows will be shown in the ascending order according to the name column.
You can show Rows in descending order like this.
mysql> select name from category ORDER BY name desc;