DB2 Tutorial

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

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

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

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

It is equivalent to the following expression:

expression < low OR expression > high

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;

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;

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;

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

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 ?
  • YesNo
Previous Db2 OR
Next Db2 LIKE

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.