
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Sorting max to min value in MySQL
To sort from max to min value, use ORDER BY length(). Let us first create a table −
mysql> create table DemoTable -> ( -> Price varchar(20) -> ); Query OK, 0 rows affected (0.92 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('80'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('800'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('108'); Query OK, 1 row affected (0.50 sec) mysql> insert into DemoTable values('765'); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------+ | Price | +-------+ | 80 | | 800 | | 108 | | 765 | +-------+ 4 rows in set (0.00 sec)
Here is the query to sort max to min value in MySQL −
mysql> select *from DemoTable -> order by length(Price) DESC,Price DESC;
This will produce the following output −
+-------+ | Price | +-------+ | 800 | | 765 | | 108 | | 80 | +-------+ 4 rows in set (0.00 sec)
- Related Articles
- How to SELECT min and max value from the part of a table in MySQL?
- Minimum value of “max + min” in a subarray in C++
- How to define a MIN and MAX value for EditText in Android?
- Python - Assign a specific value to Non Max-Min elements in Tuple
- Min-Max Heaps
- max() and min() in Python
- Explain the use of MIN() and MAX() using MySQL in Python?
- Symmetric Min-Max Heaps
- How to define a MIN and MAX value for EditText in Android using Kotlin?
- Convert min Heap to max Heap in C++
- Get MAX and MIN values along with their row id in MySQL?
- How to create a horizontal slider with custom min, max, and initial value in Java
- Create a vertical slider with custom min, max, and initial value in Java
- How to use min and max attributes in HTML?
- Program to fill Min-max game tree in Python

Advertisements