MySQL order by from highest to lowest value?

MySQLMySQLi Database

To order by from highest to lowest value, you can use ORDER BY DESC command −

select *from yourTableName order by yourColumnName DESC;

If you want the result from lowest to highest, you can use ORDER BY ASC command −

select *from yourTableName order by yourColumnName ASC;

Let us first create a table −

mysql> create table DemoTable
(
   Value int
);
Query OK, 0 rows affected (0.56 sec)

Insert records in the table using insert command −

mysql> insert into DemoTable values(134);
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable values(245);
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable values(451);
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable values(1090);
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable values(789);
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable values(434);
Query OK, 1 row affected (0.22 sec)

Display records from the table using select command −

mysql> select *from DemoTable;

This will produce the following output −

+-------+
| Value |
+-------+
| 134   |
| 245   |
| 451   |
| 1090  |
| 789   |
| 434   |
+-------+
6 rows in set (0.00 sec)

Case 1 −Order by from highest to lowest value −

mysql> select *from DemoTable order by Value DESC;

This will produce the following output −

+-------+
| Value |
+-------+
| 1090  |
| 789   |
| 451   |
| 434   |
| 245   |
| 134   |
+-------+
6 rows in set (0.07 sec)

Case 2 −Order by from lowest to highest value −

mysql> select *from DemoTable order by Value ASC;

This will produce the following output −

+-------+
| Value |
+-------+
| 134   |
| 245   |
| 434   |
| 451   |
| 789   |
| 1090  |
+-------+
6 rows in set (0.00 sec)
raja
Published on 29-Apr-2019 15:39:25
Advertisements