

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Sort data column to retrieve max textual value in MySQL
For this, you can use ORDER BY along with some aggregate function right(). Let us first create a table −
mysql> create table DemoTable1487 -> ( -> StudentCode text -> ); Query OK, 0 rows affected (0.91 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1487 values('560'); Query OK, 1 row affected (0.36 sec) mysql> insert into DemoTable1487 values('789'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable1487 values('STUDENT78'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1487 values('John89'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1487 values('Carol101'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1487 values('STUDENT98'); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1487;
This will produce the following output −
+-------------+ | StudentCode | +-------------+ | 560 | | 789 | | STUDENT78 | | John89 | | Carol101 | | STUDENT98 | +-------------+ 6 rows in set (0.00 sec)
Here is the query to sort data column to retrieve maximum textual value −
mysql> select * from DemoTable1487 where StudentCode LIKE 'STUDENT%' -> order by cast(right(StudentCode,length(StudentCode)-length('STUDENT')) as UNSIGNED) desc -> limit 1;
This will produce the following output −
+-------------+ | StudentCode | +-------------+ | STUDENT98 | +-------------+ 1 row in set (0.04 sec)
- Related Questions & Answers
- Sorting max to min value in MySQL
- Sort a MySQL table column value by part of its value?
- Retrieve MIN and MAX date in a single MySQL query from a column with date values
- Retrieve data value as a pointer in C#
- Retrieve records whenever a column value starts with 2 vowel letters in MySQL
- Get MAX() on column in two MySQL tables?
- What is the proper way to retrieve the value stored in INT column as MySQL TIMESTAMP?
- How to sort an alphanumeric column in MySQL?
- Sort in MySQL and increment value?
- How to retrieve a value with MySQL count() having maximum upvote value?
- How to get max(id) of row data in MySQL?
- How to select max of mixed string/int column in MySQL?
- Add a new value to a column of data type enum in MySQL?
- Select data and set value to boolean based on timestamp column in MySQL
- MySQL query to retrieve only the column values with special characters?
Advertisements