MYSQL Tutorial



MySQL BETWEEN


MySQL BETWEEN Operator

The BETWEEN operator is used to filter the result set within a certain range. It selects values that are between two specified values (inclusive). It can be used with numbers, dates, and even text.

Note: BETWEEN a AND b includes both a and b in the results.

Syntax

SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
  

Example Usage

Suppose you have a products table with a column price. To find products priced between 50 and 150, use:

SELECT product_name, price
FROM products
WHERE price BETWEEN 50 AND 150;
  

BETWEEN with Dates

To select orders made between two dates, for example, between '2024-01-01' and '2024-03-31':

SELECT order_id, order_date
FROM orders
WHERE order_date BETWEEN '2024-01-01' AND '2024-03-31';
  

Important Points

  • Inclusive: BETWEEN includes both boundary values.
  • Equivalent to: column_name >= value1 AND column_name <= value2.
  • Works with: Numeric, date/time, and text types.
  • Case with text: It filters alphabetically, based on the character set and collation.
Quick Tip: Use NOT BETWEEN to exclude values within the range.

🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review