
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
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)
- Related Articles
- Fetch the maximum value from a MySQL column?
- MySQL query to fetch the maximum corresponding value from duplicate column values
- Fetch maximum value from a column with values as string numbers like Value440, Value345, etc. in SQL
- MySQL rows concatenation to fetch maximum corresponding value from duplicate IDs?
- Find maximum value from a VARCHAR column in MySQL
- MySQL query to fetch the maximum cumulative value
- Fetch a specific column value (name) in MySQL
- Fetch a single ordered date from a column with MySQL LIMIT
- Fetch a specific record from a column with string values (string, numbers and special characters) in MySQL
- How to get the maximum value from a column with alphanumeric strings beginning with specific characters in MYSQL?
- Fetch specific rows from a MySQL table with duplicate column values (names)?
- Limiting numbers to a maximum value in MySQL?
- Fetch the first letter of a column value and insert it in another column with MySQL
- MySQL query to remove a value with only numbers in a column
- Get the maximum value of a column with MySQL Aggregate function

Advertisements