SQL BETWEEN

The BETWEEN operator in SQL is used to filter data based on a range of values. It allows you to retrieve rows from a table where a specified column has a value that is within a specified range. The BETWEEN operator is inclusive, meaning it will return rows that are equal to the lower and upper bounds of the range.

The syntax for using the BETWEEN operator is as follows:

1SELECT column1, column2, ... 2FROM table_name 3WHERE column_name BETWEEN value1 AND value2;

For example, consider a table named employees with a column named salary. If you want to retrieve all employees who earn a salary between $50,000 and $70,000, you would use the following query:

1SELECT * 2FROM employees 3WHERE salary BETWEEN 50000 AND 70000;

It is also possible to use the NOT BETWEEN operator to retrieve rows where the specified column does not have a value within the specified range. The syntax for using the NOT BETWEEN operator is similar to the BETWEEN operator:

1SELECT column1, column2, ... 2FROM table_name 3WHERE column_name NOT BETWEEN value1 AND value2;