AdBlock Detected!
Our website is made possible by displaying ads to our visitors. Please supporting us by whitelisting our website.
SQL BETWEEN Operator |
SQL > SQL Commands >
Between
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. SyntaxThe syntax for the BETWEEN operator is as follows: SELECT "column_name"
FROM "table_name" WHERE "column_name" BETWEEN 'max_value' AND 'min_value'; 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. ExamplesWe use the following table for our examples. Table Store_Information
Example 1To select view all sales information between January 6, 1999, and January 10, 1999, we key in, SELECT *
FROM Store_Information WHERE Txn_Date BETWEEN 'Jan-06-1999' AND 'Jan-10-1999'; 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: SELECT "column_name"
FROM "table_name" WHERE ("column_name" > 'value1') AND ("column_name" < 'value2'); Example 2We 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: SELECT *
FROM Store_Information WHERE Sales NOT BETWEEN 280 and 1000; Result:
ExercisesFor 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)
2. How many records will be returned by the following query?
3. How many records will be returned by the following query?
|
Our website is made possible by displaying ads to our visitors. Please supporting us by whitelisting our website.