SQL BETWEEN
The BETWEEN operator in SQL is used in a WHERE clause to filter records where a column’s value falls within a specified range. It is inclusive, meaning it includes the starting and ending values.
Key Points:
Inclusive: Both boundary values are considered in the result.
Data Types: Can be used with numeric, date/time, and text columns.
Logical Operator: Often combined with AND, OR, or other conditions.
Readable: Makes queries shorter and easier to understand compared to multiple comparison operators.
Syntax
column_name >= value1 AND column_name <= value2 -- value1 – lower bound of the range -- value2 – upper bound of the range
Numeric Example
SELECT OrderID, Price FROM Orders WHERE Price BETWEEN 500 AND 1500; -- Returns orders with Price >= 500 and Price <= 1500.
Date Example
SELECT OrderID, OrderDate FROM Orders WHERE OrderDate BETWEEN '2026-03-10' AND '2026-03-15'; -- Returns orders with OrderDate from 10th March 2026 to 15th March 2026 inclusive.
Text Example
SELECT ProductName FROM Orders WHERE ProductName BETWEEN 'Keyboard' AND 'Laptop'; -- Returns product names alphabetically between “Keyboard” and “Laptop”.
Use BETWEEN for cleaner queries instead of writing:
column >= value1 AND column <= value2 -- Works well with numeric ranges, date ranges, or alphabetic ranges.
Summary Definition for Exam/Interviews:
BETWEEN in SQL Server is a conditional operator that selects records whose values fall within a specified inclusive range. It can be applied to numbers, dates, and text.