The BETWEEN operator in MySQL filters results to values that fall within a specified inclusive range. It works with numeric, text (alphabetical range), and date/time types.
BETWEEN a AND b includes both a and b.x BETWEEN a AND b is the same as x >= a AND x <= b.
-- Syntax for BETWEEN
SELECT column_name
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
-- Get records where age is between 18 and 30 (inclusive)
SELECT * FROM students
WHERE age BETWEEN 18 AND 30;
-- Get records where price is between 100 and 500
SELECT * FROM products
WHERE price BETWEEN 100 AND 500;
-- Get records with dates between 2021-01-01 and 2021-12-31
SELECT * FROM orders
WHERE order_date BETWEEN '2021-01-01' AND '2021-12-31';
All three example queries return rows where the column value lies within the inclusive boundaries. For dates, ensure the literal format matches the column type. For text, BETWEEN 'A' AND 'M' will match alphabetical ordering based on collation.
Note: BETWEEN is inclusive of both ends.
>= and <=) when you need clarity in complex conditions.BETWEEN to retrieve orders between '2020-01-01' and '2020-12-31'.BETWEEN with text fields to filter names starting from 'A' to 'M'.