
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Fastest Way to Get a Column's Maximum Value in MySQL
You can try to get a column’s maximum value using two ways. The first way is as follows −
select max(yourColumnName) from yourTableName;
The second way is as follows −
select yourColumnName from yourTableName order by yourColumnName DESC LIMIT 1;
NOTE − The first query takes less time than the second query because the second query first sort n number of values then gives the highest value using LIMIT 1. Therefore, use the first query.
Let us first create a table −
mysql> create table DemoTable ( Number int ); Query OK, 0 rows affected (0.59 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(78); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(87); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable values(89); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(69); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(87); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+--------+ | Number | +--------+ | 78 | | 87 | | 89 | | 69 | | 87 | +--------+ 5 rows in set (0.00 sec)
Following is the query to get a column’s maximum value −
mysql> select max(Number) from DemoTable;
This will produce the following output −
+-------------+ | max(Number) | +-------------+ | 89 | +-------------+ 1 row in set (0.06 sec)
Advertisements