DB2 Tutorial

  • Home
  • Start Here
  • Basics
  • Views
  • Triggers
  • Indexes
  • Functions
    • Aggregate Functions
    • Date Functions
    • String Functions
    • Window Functions
Home » Db2 Aggregate Functions » Db2 MIN

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)

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;

Here is the output:

MIN_RATING ---------- 2.96

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 );

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;

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;

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;
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 ?
  • YesNo
Previous Db2 MAX
Next Db2 SUM

Getting Started

  • What is Db2
  • Installing Db2 Database Server
  • Db2 Sample Database
  • Creating a Db2 Sample Database
  • Connecting to a Db2 Database
  • Interacting with Db2 using SQL Developer

Data Manipulation

  • SELECT
  • ORDER BY
  • WHERE
  • SELECT DISTINCT
  • AND
  • OR
  • BETWEEN
  • LIKE
  • IN
  • LIMIT
  • FETCH
  • Join
  • INNER JOIN
  • LEFT JOIN
  • RIGHT JOIN
  • FULL OUTER JOIN
  • Self-Join
  • CROSS JOIN
  • GROUP BY
  • Subquery
  • HAVING
  • UNION
  • INTERSECT
  • EXCEPT
  • Common Table Expression or CTE
  • INSERT
  • INSERT Multiple Rows
  • INSERT INTO SELECT
  • UPDATE
  • DELETE

Managing Database Objects

  • CREATE TABLE
  • Identity Columns
  • ALTER COLUMN
  • ADD COLUMN
  • DROP COLUMN
  • DROP TABLE
  • TRUNCATE TABLE
  • RENAME TABLE

Db2 Constraints

  • Primary Key
  • Foreign Key
  • DEFAULT
  • NOT NULL
  • CHECK
  • UNIQUE

Db2 Data Types

  • Integer
  • Decimal
  • VARCHAR
  • CHAR
  • DATE
  • TIME
  • TIMESTAMP

Useful Functions & Expressions

  • CAST
  • CASE Expression
  • COALESCE

About db2tutorial.com

The db2tutorial.com website provides you with a comprehensive IBM DB2 tutorial with many practical examples and hands-on sessions.

Recent Tutorials

  • Db2 Functions
  • Db2 RANK
  • Db2 ROW_NUMBER
  • Db2 Window Functions
  • What is Db2

Site Links

  • Home
  • About Us
  • Contact Us
  • Privacy Policy
Copyright © © 2021 by www.db2tutorial.com. All Rights Reserved.