TSO-ISPF JCL COBOL VSAM DB2 CICS Tools Articles Job Portal Forum Quiz Interview Q&A

DB2 - SQL BETWEEN Operator


The BETWEEN operator selects values within a given range. The values can be numbers, text, or dates.

The BETWEEN operator is inclusive: begin and end values are included.

Syntax:

SELECT column_name(s)
  FROM table_name
  WHERE column_name BETWEEN value1 AND value2;

DB2 Database:

Below is a selection from the "Product" table in the DB2 database.

ProductIDProductNameCategoryPriceQtyOnHandTotalValue
7001MouseAccessories75.00
7002Harddrive65.00201,300
7003KeyboardAccessories36.00331,118.00
7004RAMComponents23.5016376.00
7005HondaBikes1,200
7006PEN7.451074.50
7007CddriveAccessories75.00
7008SpeakerAccessories75.00
7009AmplifierComponents25.00
7010HeadphoneAccessories100.00


Example 1:

The following SQL statement selects all products with a price BETWEEN 10 and 50:

SELECT * FROM Product
  WHERE Price BETWEEN 10 AND 50;
Result:
ProductIDProductNameCategoryPriceQtyOnHandTotalValue
7003KeyboardAccessories36.00331,118.00
7004RAMComponents23.5016376.00
7006PEN7.451074.50
7009AmplifierComponents25.00


Example 2:

The following SQL statement selects all products outside the range of the previous example, use NOT BETWEEN::

SELECT * FROM Products
  WHERE Price NOT BETWEEN 10 AND 50;
Result:
ProductIDProductNameCategoryPriceQtyOnHandTotalValue
7001MouseAccessories75.00
7002Harddrive65.00201,300
7005HondaBikes1,200
7007CddriveAccessories75.00
7008SpeakerAccessories75.00
7010HeadphoneAccessories100.00


If you have any doubts or queries related to this chapter, get them clarified from our Mainframe experts on ibmmainframer Community!

Are you looking for Job Change? Job Portal