DB2 Tutorial

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

Db2 COUNT

Summary: in this tutorial, you will learn how to use the DB2 COUNT() function to return the number of values in a set or the number of rows from a table.

Introduction to Db2 COUNT() function

The Db2 COUNT() function is an aggregate function that returns the number of values in a set or the number of rows in a table.

The following is the syntax of the COUNT() function:

COUNT( ALL | DISTINCT expression)

The COUNT() function accepts a set of values which can be any built-in data type except for BLOB, CLOB, DBCLOB, and XML.

The COUNT(expression) is the same as COUNT(ALL expression) which returns the number of non-null values in a set, including duplicates.

The COUNT(DISTINCT expression) returns the number of distinct non-null values.

The COUNT(*) returns the number of rows in a set, including rows that contain NULL values.

The COUNT()  returns a result of INT type. It never returns NULL.

If the number of values in a set exceeds the maximum value of the INT type, which is 2,147,483,647, you can use the COUNT_BIG() function instead.

The COUNT_BIG() behaves the same as the COUNT() function except for the type of return value that supports a larger range, i.e., BIGINT.

Db2 COUNT() function illustration

First, create a new table named count_demo that contains one integer column:

CREATE TABLE count_demo(c1 INT);

Second, insert five rows into the count_demo table:

INSERT INTO count_demo(c1) VALUES(1),(2),(3),(NULL),(3);

Third, view the data from the count_demo table by using the following SELECT statement:

SELECT c1 FROM count_demo;

Fourth, this statement uses the COUNT(*) function to return the number of rows from the count_demo table:

SELECT COUNT(*) row_count FROM count_demo

Here is the output:

ROW_COUNT ----------- 5

Fifth, this statement uses the COUNT(DISTINCT expression) to get the number of non-null rows from the count_demo table:

SELECT COUNT(c1) result FROM count_demo;

The output is the following:

RESULT ----------- 4

Sixth, this statement uses the COUNT(DISTINCT expression) to return distinct non-null values from the count_demo table:

SELECT COUNT(DISTINCT c1) result FROM count_demo;

The number of distinct non-null values in the count_demo table is as follows:

RESULT ----------- 3

Db2 COUNT() examples

We’ll use the books table from the sample database to demonstrate the COUNT() function.

1) Using DB2 COUNT(*) function to return the number of rows from a table examples

This example uses the COUNT(*) function to find the number of books from the books table:

SELECT COUNT(*) FROM books;

Here is the output:

BOOK_COUNT ----------- 1091

To get the number of books which have ISBN, you add a WHERE clause as the following:

SELECT COUNT(*) book_isbn FROM books WHERE isbn IS NOT NULL;

Here is the result:

BOOK_ISBN ----------- 984

2) Using DB2 COUNT() function with the GROUP BY clause example

The COUNT() function is often used with the GROUP BY clause to return the number of values for each group.

This statement uses the COUNT(expression) returns the number of books with ISBN for each publisher:

SELECT p.name publisher, COUNT(isbn) book_with_isbn FROM books b INNER JOIN publishers p ON p.publisher_id = b.publisher_id GROUP BY p.name ORDER BY book_with_isbn DESC;

This picture illustrates the partial output:

Db2 COUNT function with GROUP BY clause

In this example, the GROUP BY clause groups books by publishers and the COUNT(ISBN) function returns the number of books with ISBN for every publisher.

3) Using DB2 COUNT() function with the HAVING clause example

The following statement finds the publishers that have more than 30 books, where all the books have ISBN:

SELECT p.name publisher, COUNT(isbn) book_with_isbn FROM books b INNER JOIN publishers p ON p.publisher_id = b.publisher_id GROUP BY p.name HAVING COUNT(isbn) > 30 ORDER BY book_with_isbn DESC;
Db2 COUNT function with HAVING clause

In this tutorial, you have learned how to use the Db2 COUNT() function to get the number of values in a set or the number of rows in a table.

  • Was this tutorial helpful ?
  • YesNo
Previous Db2 AVG
Next Db2 LISTAGG

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.