Find the Maximum Value in a Column in MySQL

MySQLMySQLi Database

We need to use the MAX(columnName) to find the Maximum value in a column. But, at first, we will understand about database and tables in MySQL.

Before installing MySQL, it is important to determine which version and which distribution format (it could be a binary file or from source files) should be used. If the database was newly created, it is obvious that there would be no tables in it.

One of the most important part is to decide the structure of the database, the tables that would be needed, the columns in every table and the relationship between these tables. Once we decide what our database needs to contain, a proper name for it, and tables have been created with proper column names and row values, we can discuss what kind of manipulations can be performed with this database.

Note: We assume we have created a database named ‘DBNAME’ and a table named ‘tableName’.

Let us understand the query that will fetch the maximum value in a column in MySQL −

Query

SELECT MAX(columnName) AS alias FROM tableName;

Output

+-------------------+
| columnName        |
+-------------------+
| 45                |
+-------------------+

In the above query, ‘columnName’ refers to the name of the column, and ‘alias’ is a new name given to the column. The ‘tableName’ refers to the name of the table from which the maximum value is being queried.

The function ‘MAX’ is used on the ‘columnName’ to fetch the maximum value from a specific column. An alias is provided just to provide clarity to the name of the column in case it is not clear.

Published on 08-Mar-2021 12:24:32