ORDER BY in MariaDB

MariaDB ORDER BY
MariaDB ORDER BY clause is used with the MariaDB SELECT statement to sort or re-arrange the records in the result set.

Syntax:

SELECT expressions  
FROM table_name
WHERE conditions;   
ORDER BY expression [ ASC | DESC ];  

Parameters:
ASC: It is an optional parameter which is used to specify the sorting order to sort records in ascending order. By default the result set is sorted in ascending order with ORDER BY clause.
DESC: It is an optional parameter which is used to specify the sorting order to sort records in descending order.

Example 1: Default order.
Players table:

ID	NAME	        SPORTS
1	Sachin	        Cricket
2	Dhoni	        Cricket
3	Sunil	        Football
4	Srikanth	Badminton
5	Mary	        Boxing

Query:

SELECT * FROM Players  
WHERE  id > 2  
ORDER BY name;

Output:

ID	NAME	        SPORTS
5	Mary	        Boxing
4	Srikanth	Badminton
3	Sunil	        Football

Explanation:
The ‘players’ is an already existing table. Here we are selecting all the fields from the table with a condition that the selected row must have an ID greater than 2. Here we are asking the ORDER BY clause to order the result set by NAME in ascending order by default.

Example 2: Ascending order.
Players table:

ID	NAME	        SPORTS
1	Sachin	        Cricket
2	Dhoni	        Cricket
3	Sunil	        Football
4	Srikanth	Badminton
5	Mary	        Boxing

Query:

SELECT * FROM Players  
WHERE  id > 2  
ORDER BY name ASC;

Output:

ID	NAME	        SPORTS
5	Mary	        Boxing
4	Srikanth	Badminton
3	Sunil	        Football

Explanation:
The ‘players’ is an already existing table. Here we are selecting all the fields from the table with a condition that the selected row must have an ID greater than 2. Here we are asking the ORDER BY clause to order the result set by NAME in ascending order.

Example 3: Descending order.
Players table:

ID	NAME	         SPORTS
1	Sachin	         Cricket
2	Dhoni	         Cricket
3	Sunil	         Football
4	Srikanth	 Badminton
5	Mary	         Boxing

Query:

SELECT * FROM Players  
WHERE  id > 2  
ORDER BY name DESC; 
</re>
 
<strong><em>Output: </em></strong>
<pre>
ID	NAME	        SPORTS
3	Sunil	        Football
4	Srikanth	Badminton
5	Mary	        Boxing

Explanation:
The ‘players’ is an already existing table. Here we are selecting all the fields from the table with a condition that the selected row must have an ID greater than 2. Here we are asking the ORDER BY clause to order the result set by NAME in descending order.