
- 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
How to select the 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)
- Related Articles
- Find the Maximum Value in a Column in MySQL
- MySQL SELECT to sum a column value with previous value
- Select the maximum for each value in a MySQL table?
- Fetch the maximum value from a MySQL column?
- Show column value twice in MySQL Select?
- How to select distinct value from one MySQL column only?
- How to select row when column must satisfy multiple value in MySQL?
- Find maximum value from a VARCHAR column in MySQL
- Get the maximum value of a column with MySQL Aggregate function
- How to add a column from a select query but the value from the new column will be the row count of the MySQL select query?
- Select a specific value between two column values in MySQL?
- How to SELECT * and rename a column in MySQL?
- How to select the sum of the column values with higher value in reach row with MySQL?
- MySQL query to select the nth highest value in a column by skipping values
- Select minimum row value from a column with corresponding duplicate column values in MySQL

Advertisements