- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Two ways to fetch maximum value from a MySQL column with numbers
To fetch the maximum value, use any of the below-given syntaxes −
select max(yourColumnName) from yourTableName; OR select *from yourTableName order by yourColumnName desc limit 1;
Let us first create a table −
mysql> create table DemoTable ( Value int ); Query OK, 0 rows affected (0.84 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(45); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(87); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(56); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(77); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(85); Query OK, 1 row affected (0.17 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------+ | Value | +-------+ | 45 | | 87 | | 56 | | 77 | | 85 | +-------+ 5 rows in set (0.00 sec)
Let us now get the maximum value from the above table −
mysql> select max(Value) from DemoTable;
This will produce the following output −
+------------+ | max(Value) | +------------+ | 87 | +------------+ 1 row in set (0.00 sec)
Let us now see the alternative way to fetch maximum value −
mysql> select *from DemoTable order by Value desc limit 1;
This will produce the following output −
+-------+ | Value | +-------+ | 87 | +-------+ 1 row in set (0.00 sec)
Advertisements