Db2 BETWEEN

Summary: in this tutorial, you will learn how to the Db2 BETWEEN operator to check whether a value lies between two other values.

Introduction to Db2 BETWEEN operator

The Db2 BETWEEN operator is a logical operator that determines whether a value lies between two values that are specified in ascending order. The BETWEEN operator is often used in the WHERE clause of the SELECT statement to form the search condition for the rows returned by a query.

The following shows the syntax of the BETWEEN operator:

expression BETWEEN low AND high
Code language: SQL (Structured Query Language) (sql)

In this syntax, low and high can be literal values or expressions. The BETWEEN operator returns true if the value of the expression lies between the low and high values, or simply:

expression >= low AND expression <= high
Code language: SQL (Structured Query Language) (sql)

The NOT BETWEEN negates the BETWEEN operator. It returns true if the value is not between two other values:

expression NOT BETWEEN low AND high
Code language: SQL (Structured Query Language) (sql)

It is equivalent to the following expression:

expression < low OR expression > high
Code language: SQL (Structured Query Language) (sql)

Db2 BETWEEN operator examples

We will use the books table from the sample database to demonstrate the BETWEEN operator:

1) Using Db2 BETWEEN operator with numbers example

This example uses the BETWEEN operator to find books that have the number of pages between 100 and 125:

SELECT title, total_pages FROM books WHERE total_pages BETWEEN 100 AND 125 ORDER BY total_pages;
Code language: SQL (Structured Query Language) (sql)

Here is the result set:

Db2 Between with numbers example

It is equivalent to the following query:

SELECT title, total_pages FROM books WHERE total_pages >= 100 and total_pages <= 125 ORDER BY total_pages;
Code language: SQL (Structured Query Language) (sql)

2) Using Db2 BETWEEN operator with dates example

The following example uses the BETWEEN operator finds books whose published dates are between Jan 01, 2017 and June 30, 2017:

SELECT title, published_date FROM books WHERE published_date BETWEEN '2017-01-01' AND '2017-06-30' ORDER BY published_date;
Code language: SQL (Structured Query Language) (sql)

The following picture shows the output:

Db2 Between with dates example

3) Db2 NOT BETWEEN operator example

This example uses the NOT BETWEEN operator to find books whose ratings are not between 3 and 4.5:

SELECT title, rating FROM books WHERE rating NOT BETWEEN 3 AND 4.5 ORDER BY rating;
Code language: SQL (Structured Query Language) (sql)
Db2 Not Between example

The query above is equivalent to the following query that uses comparison operators with the logical operator OR:

SELECT title, rating FROM books WHERE rating < 3 OR rating > 4.5 ORDER BY rating;
Code language: SQL (Structured Query Language) (sql)

In this tutorial, you have learned how to use the Db2 BETWEEN operator to determine whether a value lies between two values.

Was this tutorial helpful ?