The MySQL NOT BETWEEN ... AND ...
operator is used to filter records that are not within a specified range. This operator is essential for querying data that falls outside of a particular range in SQL queries.
SELECT column1, column2, ...
FROM table_name
WHERE column1 NOT BETWEEN value1 AND value2;
The NOT BETWEEN ... AND ...
operator has the following components:
column1, column2, ...
: The columns to be retrieved.table_name
: The name of the table from which to retrieve the data.column1 NOT BETWEEN value1 AND value2
: The condition to filter the records, where column1
is not within the range of value1
and value2
.Let's look at some examples of the MySQL NOT BETWEEN ... AND ...
operator:
USE mydatabase;
This query sets the context to the database named mydatabase
.
Create a table to work with:
CREATE TABLE employees (
id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
department VARCHAR(50),
salary DECIMAL(10, 2)
);
This query creates a table named employees
with columns for id
, first_name
, last_name
, department
, and salary
.
Insert some initial rows into the table:
INSERT INTO employees (first_name, last_name, department, salary)
VALUES ('John', 'Doe', 'HR', 50000.00),
('Jane', 'Smith', 'IT', 60000.00),
('Jim', 'Brown', 'IT', 55000.00),
('Jake', 'White', 'HR', 52000.00),
('Jill', 'Green', 'Marketing', 45000.00);
This query inserts five rows into the employees
table.
Use the NOT BETWEEN ... AND ...
operator to filter records that fall outside a range:
SELECT *
FROM employees
WHERE salary NOT BETWEEN 50000 AND 60000;
This query retrieves all columns from the employees
table where the salary
is not between 50000 and 60000.
Use the NOT BETWEEN ... AND ...
operator with multiple conditions:
SELECT *
FROM employees
WHERE department = 'IT' AND salary NOT BETWEEN 50000 AND 60000;
This query retrieves all columns from the employees
table where the department
is 'IT' and the salary
is not between 50000 and 60000.
The MySQL NOT BETWEEN ... AND ...
operator is a powerful tool for filtering records that fall outside a specified range. Understanding how to use the NOT BETWEEN ... AND ...
operator is essential for effective data querying and analysis in MySQL.