SQL Keyword BETWEEN
The SQL keyword NOT BETWEEN is used within a WHERE condition to negate a condition that uses the BETWEEN keyword to compare a column value against a range of values.
- The keyword BETWEEN is used to compare a value with a range of values, which can be numbers, text, or dates.
- Always the beginning and end values of a range are included for BETWEEN keyword, so they are excluded for the NOT BETWEEN keyword.
SELECT With NOT BETWEEN Number Range
The below SQL fetches all the products having prices, that are not between the specified number range.
SELECT * FROM products
WHERE price NOT BETWEEN 10 AND 20;
SELECT With NOT BETWEEN Strings
The below SQL fetches all the employees having names, that are not between the specified string range.
SELECT * FROM employees
WHERE employee_name NOT BETWEEN 'Arun' AND 'Kumar';
SELECT With NOT BETWEEN Dates
The below SQL fetches all the employees having birth dates, that are not between the specified date range.
SELECT * FROM employees
WHERE birth_date NOT BETWEEN CAST('1990-01-01' AS DATE) AND CAST('2020-01-01' AS DATE);
UPDATE With NOT BETWEEN Number Range
The below SQL updates all the products having prices, that are not between the specified number range.
UPDATE products
SET price = 15
WHERE price NOT BETWEEN 10 AND 20;
DELETE With NOT BETWEEN Number Range
The below SQL deletes all the products having prices, that are not between the specified number range.
DELETE products
WHERE price NOT BETWEEN 10 AND 20;