

- 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
Which is the 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)
- Related Questions & Answers
- Which is the fastest method to get the total row count for a MySQL Query?
- Is there a way to select a value which matches partially in MySQL?
- Get the maximum value of a column with MySQL Aggregate function
- Which is the fastest implementation of Python
- What's the fastest way of checking if a point is inside a polygon in Python?
- Fetch the maximum value from a MySQL column?
- Find the Maximum Value in a Column in MySQL
- Fastest way to search for a date from the date records in MySQL
- What is the fastest way to insert a large number of rows into a MySQL table?
- Fastest way to count number of rows in MySQL table?
- How to select the maximum value of a column in MySQL?
- Fastest way to insert with multiple values in a single MySQL query?
- Fastest Way to multiply two Numbers
- What is the proper way to retrieve the value stored in INT column as MySQL TIMESTAMP?
- Find maximum value from a VARCHAR column in MySQL
Advertisements