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.
SELECT column_name(s) FROM table_name WHERE column_name BETWEEN value1 AND value2; |
Below is a selection from the "Product" table in the DB2 database.
ProductID | ProductName | Category | Price | QtyOnHand | TotalValue |
---|---|---|---|---|---|
7001 | Mouse | Accessories | 75.00 | ||
7002 | Harddrive | 65.00 | 20 | 1,300 | |
7003 | Keyboard | Accessories | 36.00 | 33 | 1,118.00 |
7004 | RAM | Components | 23.50 | 16 | 376.00 |
7005 | Honda | Bikes | 1,200 | ||
7006 | PEN | 7.45 | 10 | 74.50 | |
7007 | Cddrive | Accessories | 75.00 | ||
7008 | Speaker | Accessories | 75.00 | ||
7009 | Amplifier | Components | 25.00 | ||
7010 | Headphone | Accessories | 100.00 |
The following SQL statement selects all products with a price BETWEEN 10 and 50:
SELECT * FROM Product WHERE Price BETWEEN 10 AND 50; |
ProductID | ProductName | Category | Price | QtyOnHand | TotalValue |
---|---|---|---|---|---|
7003 | Keyboard | Accessories | 36.00 | 33 | 1,118.00 |
7004 | RAM | Components | 23.50 | 16 | 376.00 |
7006 | PEN | 7.45 | 10 | 74.50 | |
7009 | Amplifier | Components | 25.00 |
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; |
ProductID | ProductName | Category | Price | QtyOnHand | TotalValue |
---|---|---|---|---|---|
7001 | Mouse | Accessories | 75.00 | ||
7002 | Harddrive | 65.00 | 20 | 1,300 | |
7005 | Honda | Bikes | 1,200 | ||
7007 | Cddrive | Accessories | 75.00 | ||
7008 | Speaker | Accessories | 75.00 | ||
7010 | Headphone | Accessories | 100.00 |
If you have any doubts or queries related to this chapter, get them clarified from our Mainframe experts on ibmmainframer Community!