The BETWEEN operator is used when the filtering criteria is a continuous range with a maximum value and a minimum value. It is always used in the WHERE clause.
The syntax for the BETWEEN operator is as follows:
This will select all rows whose column has a value between 'min_value' and 'max_value.' Please note the order: 'min_value' needs to be listed before AND and 'max_value.' needs to be listed after. If the order is switched, the SQL statement is still valid, but will not give the desired results.
We use the following table for our examples.
Table Store_Information
To select view all sales information between January 6, 1999, and January 10, 1999, we key in,
Note that date may be stored in different formats in different databases. This tutorial simply choose one of the formats.
Result:
BETWEEN is an inclusive operator, meaning that 'value1' and 'value2' are included in the result. If we wish to exclude 'value1' and 'value2' but include everything in between, we need to change the query to the following:
We can also use the BETWEEN operator to exclude a range of values by adding NOT in front of BETWEEN. In the above example, if we want to show all rows where the Sales column is not between 280 and 1000, we will use the following SQL:
Result:
For these exercises, assume we have a table called User_Sales with the following data:
Table User_Sales
1. Which of the following SQL statement is valid? (There can be more than one answer)
a) SELECT * FROM User_Sales WHERE Total_Sales BETWEEN 200 OR 300;
b) SELECT * FROM User_Sales WHERE Total_Sales IS BETWEEN 200 OR 300;
c) SELECT * FROM User_Sales WHERE Total_Sales IS BETWEEN 200 AND 300;
d) SELECT * FROM User_Sales WHERE Total_Sales BETWEEN 200 AND 300;
2. How many records will be returned by the following query?
SELECT * FROM User_Sales WHERE Join_Date BETWEEN 'Apr-05-2015' AND 'Apr-10-2015';
3. How many records will be returned by the following query?
SELECT * FROM User_Sales WHERE Gender = 'F' OR Total_Sales BETWEEN 50 AND 100;
1. d)
2. 4 records are returned. They are,
3. 3 record are returned. They are,
Our website is made possible by displaying ads to our visitors. Please supporting us by whitelisting our website.