Day 15 – SQL BETWEEN Operator
In SQL, the
BETWEEN operator is used to specify a range of values in a
WHERE clause. The
BETWEEN operator allows you to specify a range of values for a column, and the
WHERE clause will return any rows where the column value falls within that range.
The basic syntax for using the
BETWEEN operator is as follows:
SELECT column_name FROM table_name WHERE column_name BETWEEN value1 AND value2;
column_name is the name of the column you want to search,
table_name is the name of the table containing the column, and
value2 are the two values that define the range.
For example, suppose you have a table called
employees with columns
salary. If you want to find all employees with salaries between $50,000 and $100,000, you would use the following SQL statement:
SELECT * FROM employees WHERE salary BETWEEN 50000 AND 100000;
This statement will return all rows from the
employees table where the
salary column falls within the range of $50,000 to $100,000.
Note that the
BETWEEN operator is inclusive, meaning that it includes the values specified in the range. So in the example above, the SQL statement will return all employees with salaries of exactly $50,000 or $100,000, in addition to those with salaries between those values.
You can also use the
NOT BETWEEN operator to exclude rows that fall within a certain range, or you can use the
BETWEEN operator with a
NOT keyword to exclude rows that do not fall within a certain range.