How to Find the Maximum Value of a Numeric Column in SQL (2024)

Problem:

You’d like to find the maximum value of a numeric column.

Example:

Our database has a table named product with data in the following columns: id, name, year, and items.

idnameyearitems
1bread roll2018345
2chocolate2017123
3butter201934
4bread roll2019456
5butter201856
6butter201778
7chocolate201987
8chocolate201876

Let’s find the maximum number of items sold over all years.

Solution:

SELECT MAX(items) as max_itemsFROM product;

Here’s the result:

max_items
456

Discussion:

To find the max value of a column, use the MAX() aggregate function; it takes as its argument the name of the column for which you want to find the maximum value. If you have not specified any other columns in the SELECT clause, the maximum will be calculated for all records in the table. In our example, the query returns the maximum number among all items.

How to Find the Maximum Value of a Numeric Column in SQL (1)

Of course, since it’s an aggregate function, MAX() can also be used with groups. For example, if we’d like to see the maximum number of items sold in each year, we can write this query:

SELECT year, MAX(items) AS max_itemsFROM productGROUP BY year;

The maximum is calculated for each group:

yearmax_items
2018345
2017123
2019456
How to Find the Maximum Value of a Numeric Column in SQL (2024)
Top Articles
Latest Posts
Article information

Author: Lakeisha Bayer VM

Last Updated:

Views: 5529

Rating: 4.9 / 5 (69 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Lakeisha Bayer VM

Birthday: 1997-10-17

Address: Suite 835 34136 Adrian Mountains, Floydton, UT 81036

Phone: +3571527672278

Job: Manufacturing Agent

Hobby: Skimboarding, Photography, Roller skating, Knife making, Paintball, Embroidery, Gunsmithing

Introduction: My name is Lakeisha Bayer VM, I am a brainy, kind, enchanting, healthy, lovely, clean, witty person who loves writing and wants to share my knowledge and understanding with you.