T
Tech Town
Log InSign Up Free
← Back to Blog

June 17, 2025 · admin

BETWEEN Operator

๐ŸŽฏ SQL BETWEEN Operator โ€“ Filter Values Within a Range Easily

Want to retrieve records that fall within a specific range of numbers, dates, or text?
The SQL BETWEEN operator is the perfect tool to simplify range-based filtering in your queries.


๐Ÿ“Œ What is SQL BETWEEN?

The SQL BETWEEN operator allows you to filter rows based on a range of values, including both lower and upper bounds.

๐Ÿง  It works with:

  • Numbers
  • Dates
  • Text (alphabetical ranges)

๐Ÿงพ SQL BETWEEN Syntax

SELECT column1, column2, ...
FROM table_name
WHERE column_name BETWEEN value1 AND value2;

๐Ÿ” This returns rows where column_name is:

  • Greater than or equal to value1
  • Less than or equal to value2

โœ… Example: Filter by Salary Range

SELECT employee_name, salary
FROM employees
WHERE salary BETWEEN 40000 AND 80000;

๐ŸŽฏ This returns employees earning โ‚น40,000 to โ‚น80,000, inclusive.


๐Ÿ“† Example: Filter by Date Range

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

Returns all orders placed in 2024.


๐Ÿ”  Example: Filter by Text Range

SELECT product_name
FROM products
WHERE product_name BETWEEN 'A' AND 'M';

This retrieves product names that alphabetically fall between A and M.


โŒ NOT BETWEEN

To exclude a range, use NOT BETWEEN:

SELECT employee_name, age
FROM employees
WHERE age NOT BETWEEN 25 AND 35;

๐Ÿ“Œ This returns employees younger than 25 or older than 35.


โš ๏ธ Important Notes

  • BETWEEN is inclusive โ€“ it includes both boundary values.
  • For date comparisons, use 'YYYY-MM-DD' format for compatibility.
  • Works with AND, OR, and other filters for more complex queries.

๐Ÿ” Combine BETWEEN with Other Conditions

SELECT *
FROM orders
WHERE order_date BETWEEN '2024-01-01' AND '2024-06-30'
AND status = 'Delivered';

๐Ÿ’ก Returns only orders delivered in H1 of 2024.


๐Ÿงช Practice Challenge

Task: Retrieve products priced between โ‚น1,000 and โ‚น5,000 that are in stock.

SELECT product_name, price, stock_status
FROM products
WHERE price BETWEEN 1000 AND 5000
AND stock_status = 'Available';

๐Ÿ“ Final Thoughts

The SQL BETWEEN operator is a clean, readable, and high-performance way to filter values within ranges.

โœ… Simplifies queries
โœ… Works across numbers, dates, and text
โœ… Makes conditions easier to understand for any SQL user