TSO-ISPF JCL COBOL VSAM DB2 CICS Tools Articles Job Portal Forum Quiz Interview Q&A

DB2 - SQL MIN() and MAX() Functions


SQL MIN() and MAX() Functions

The MIN() function returns the smallest value of the selected column.

The MAX() function returns the largest value of the selected column.

Let us see the syntax for each function below.

Syntax: MIN()

SELECT MIN(column_name)
  FROM table_name
  WHERE condition;

Syntax: MAX()

SELECT MAX(column_name)
  FROM table_name
  WHERE condition;

DB2 Database:

Below is a selection from the "Product" table in the DB2 database.

ProductIDProductNameCategoryPriceQtyOnHandTotalValue
7001MouseAccessories75.00
7002Harddrive65.00201,300
7003KeyboardAccessories36.00331,118.00
7004RAMComponents23.5016376.00
7005HondaBikes1,200
7006PEN7.451074.50
7007CddriveAccessories75.00
7008SpeakerAccessories75.00

Example 1: Min Function

The following SQL statement finds the price of the cheapest product in the "Product" table.

SELECT MIN(Price) AS SmallestPrice
  FROM Products;
Result:
SmallestPrice
7.45


Example 2: Max Function

The following SQL statement finds the price of the most expensive product in the "Product" table.

SELECT MAX(Price) AS LargestPrice
  FROM Products;
Result:
LargestPrice
1,200


If you have any doubts or queries related to this chapter, get them clarified from our Mainframe experts on ibmmainframer Community!

Are you looking for Job Change? Job Portal