Db2 MIN

Summary: in this tutorial, you will learn how to use Db2 MIN() function to find the lowest value in a set of values.

Introduction to Db2 MIN() function

The MIN() function is an aggregate function that returns the minimum value in a set of value.

Here is the syntax of the MIN() function:

MIN(expression)
Code language: SQL (Structured Query Language) (sql)

The MIN() function returns the lowest value in a set. It returns NULL if the set is empty.

Similar to the MAX() function, the MIN() function also ignores NULL values.

Db2 MIN() function examples

We’ll use books table to demonstrate the MIN() function.

1) Using Db2 MIN() function to get the lowest value example

The following example uses the MIN() function to return the lowest rating of books:

SELECT MIN(rating) min_rating FROM books;
Code language: SQL (Structured Query Language) (sql)

Here is the output:

MIN_RATING ---------- 2.96
Code language: SQL (Structured Query Language) (sql)

To find the books that have the lowest ratings, you use this query:

SELECT title, isbn, rating FROM books WHERE rating = ( SELECT MIN(rating) FROM books );
Code language: SQL (Structured Query Language) (sql)

In this example:

  • First, the subquery returns the lowest rating of all books.
  • Then, the outer query returns the detailed information of the book whose rating is equal to the lowest rating.

2) Using Db2 MIN() function with GROUP BY clause example

We often use the MIN() function with the GROUP BY clause to find the minimum value for every group.

This example returns the lowest book rating for each publisher.

SELECT p.name publisher, MIN(b.rating) min_rating FROM books b INNER JOIN publishers p ON p.publisher_id = b.publisher_id GROUP BY p.name ORDER BY min_rating DESC;
Code language: SQL (Structured Query Language) (sql)

The following picture shows the partial list of publishers with the lowest book ratings:

Db2 MIN function with GROUP BY clause

3) Using Db2 MIN() function with HAVING clause example

To filter publishers whose the lowest book ratings are less than 3.3, you use the MIN() function in the HAVING clause:

SELECT p.name publisher, MIN(b.rating) min_rating FROM books b INNER JOIN publishers p ON p.publisher_id = b.publisher_id GROUP BY p.name HAVING MIN(b.rating) < 3.3 ORDER BY min_rating DESC;
Code language: SQL (Structured Query Language) (sql)

Here is the result set:

Db2 MIN function with HAVING clause

4) Using Db2 MIN() function with AVG() function example

To find the lowest book rating of average book ratings of publishers, you use the following statement:

WITH cte AS( SELECT p.name publisher, AVG(b.rating) avg_rating FROM books b INNER JOIN publishers p ON p.publisher_id = b.publisher_id GROUP BY p.name ) SELECT MIN(avg_rating) min_avg_rating FROM cte;
Code language: SQL (Structured Query Language) (sql)
Db2 MIN function with AVG example

In this example, first, the common table expression cte returns the average book ratings by publishers. Then, the MIN() function returns the lowest rating from these average ratings.

In this tutorial, you have learned how to use the Db2 MIN() function to find the lowest value in a set of values.

Was this tutorial helpful ?