The BETWEEN operator in MySQL filters records that fall within a specific range of values such as numbers, dates, or text. The NOT BETWEEN operator does the opposite by excluding values inside the range. These operators are widely used in PHP–MySQL applications for data filtering.
BETWEEN checks a value within a rangeNOT BETWEEN excludes values inside the range
// Generic syntax for BETWEEN operator
SELECT column_name FROM table_name
WHERE column_name BETWEEN value1 AND value2;
// Fetch employees aged between 30 and 40
SELECT * FROM employees
WHERE age BETWEEN 30 AND 40;
// Retrieve employees who joined in 2020
SELECT * FROM employees
WHERE join_date BETWEEN '2020-01-01' AND '2020-12-31';
// Exclude employees aged between 30 and 40
SELECT * FROM employees
WHERE age NOT BETWEEN 30 AND 40;
// Select products with names between A and M
SELECT * FROM products
WHERE product_name BETWEEN 'A' AND 'M';
// PHP query using BETWEEN operator
$sql = "SELECT * FROM employees WHERE age BETWEEN 25 AND 35";
$result = mysqli_query($conn, $sql);
The queries return records that match the defined range conditions. When used in PHP, the filtered results are typically displayed in tables, reports, or dashboards.
BETWEEN includes both limitsNOT BETWEEN to exclude products priced between 100 and 500