SQL Basics | DML: Data Manipulation Language | BETWEEN
The IN operator retrieves matching values from a database by limiting them to one or more discrete values. BETWEEN retrieves matching values from a database within a certain range.
BETWEEN syntax
SELECT "field_name"
FROM "table_name"
WHERE "field_name" BETWEEN 'value1' AND 'value2';
This lets you query data whose field value is between value1 and value2.
BETWEEN example
For example, suppose you want to retrieve data from the store_information table between January 6, 2018 and January 10, 2018.
store_information table
| store_name | sales | txn_date |
|---|---|---|
| Los Angeles | 1500 | Jan-05-2018 |
| San Diego | 250 | Jan-07-2018 |
| San Francisco | 300 | Jan-08-2018 |
| Boston | 700 | Jan-08-2018 |
Note: Depending on the database, dates may be stored in different ways. The format shown here is one of those storage methods.
Enter the following command.
SELECT *
FROM Store_Information
WHERE Txn_Date BETWEEN 'Jan-06-2018' AND 'Jan-10-2018';
The result is as follows.
| store_name | sales | txn_date |
|---|---|---|
| San Diego | 250 | Jan-07-2018 |
| San Francisco | 300 | Jan-08-2018 |
| Boston | 700 | Jan-08-2018 |