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. The SQL BETWEEN operator filters rows where a column's value falls within a continuous range — it is inclusive of both boundary values and works with numbers, dates, and text. Use NOT BETWEEN to exclude a range.
SyntaxThe 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. 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,
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:
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:
Result:
Frequently Asked QuestionsHow is BETWEEN different from using >= and <=?They are functionally equivalent. Does BETWEEN work with text (string) values?Yes. BETWEEN can be applied to text columns, and comparisons use alphabetical (lexicographic) ordering. For example, Can BETWEEN be used in an UPDATE or DELETE statement?Yes. BETWEEN can appear in the WHERE clause of UPDATE or DELETE just like in a SELECT. For example: Is BETWEEN supported in all SQL databases?Yes. BETWEEN is part of the SQL standard and is supported in all major databases including MySQL, PostgreSQL, SQL Server, Oracle, and SQLite. 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.