
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
Get Maximum Value of a Column with MySQL Aggregate Function
To get the maximum value of a column, MySQL has a predefined aggregate function MAX(). Let us first create a table −
mysql> create table DemoTable ( Id int ); Query OK, 0 rows affected (0.96 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(100); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(988); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(1000); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(99); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(999); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+------+ | Id | +------+ | 100 | | 988 | | 1000 | | 99 | | 999 | +------+ 5 rows in set (0.00 sec)
Following is the query to get the maximum value of a column −
mysql> select max(Id) AS MaxId from DemoTable;
This will produce the following output −
+-------+ | MaxId | +-------+ | 1000 | +-------+ 1 row in set (0.03 sec)
Advertisements