SQL BETWEEN Operator

‮h‬ttps://www.theitroad.com

In SQL, the BETWEEN operator is used to select values within a range of values, including the range boundaries. The syntax for the BETWEEN operator is as follows:

SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;

For example, suppose we have a table named "sales" with a column named "sale_amount". We can use the BETWEEN operator to find all sales that occurred between $10,000 and $20,000, like this:

SELECT *
FROM sales
WHERE sale_amount BETWEEN 10000 AND 20000;

This query will return all rows from the "sales" table where the "sale_amount" column contains a value between $10,000 and $20,000.

We can also use the NOT operator with BETWEEN to select values outside of a range. For example, to find all sales that did not occur between $10,000 and $20,000, we can use the following query:

SELECT *
FROM sales
WHERE sale_amount NOT BETWEEN 10000 AND 20000;

This query will return all rows from the "sales" table where the "sale_amount" column contains a value outside of the range $10,000 to $20,000.

Note that the BETWEEN operator is inclusive, meaning that values equal to the range boundaries will be included in the result set. If we want to exclude one or both of the range boundaries, we can use the comparison operators (< or >) instead of BETWEEN.