
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
Select Maximum Value of a Column in MySQL
You can use ORDER BY clause or aggregate function MAX() to select the maximum value.
Using ORDER BY
Following is the syntax −
select yourColumnName from yourTableName order by yourColumnName desc limit 0,1;
Let us first create a table −
mysql> create table DemoTable ( Number int ); Query OK, 0 rows affected (0.52 sec)
Insert records in the table using insert command −
mysql> insert into DemoTable values(790); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(746); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(480); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(880); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(879); Query OK, 1 row affected (0.20 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable;
This will produce the following output −
+--------+ | Number | +--------+ | 790 | | 746 | | 480 | | 880 | | 879 | +--------+ 5 rows in set (0.00 sec)
Following is the query to select the maximum value of a column in MySQL −
mysql> select Number from DemoTable order by Number desc limit 0,1;
This will produce the following output −
+--------+ | Number | +--------+ | 880 | +--------+ 1 row in set (0.00 sec)
Aggregate function MAX()
You can also use aggregate function MAX() for this −
mysql> select MAX(Number) from DemoTable;
This will produce the following output −
+-------------+ | MAX(Number) | +-------------+ | 880 | +-------------+ 1 row in set (0.00 sec)
Advertisements