Between in SQL
? BETWEEN Operator in SQL
The BETWEEN operator is used to filter the result set within a range of values. It can be used with numbers, dates, and text.
? Syntax
SELECT column1, column2FROM table_nameWHERE column_name BETWEEN value1 AND value2;Includes both
value1andvalue2in the range.Equivalent to:
column_name >= value1 AND column_name <= value2
? Examples
1. Numeric Range
SELECT * FROM employeesWHERE salary BETWEEN 30000 AND 60000;Selects employees with salary from 30,000 to 60,000 inclusive.
2. Date Range
SELECT * FROM ordersWHERE order_date BETWEEN '2023-01-01' AND '2023-03-31';Selects orders placed in first quarter of 2023.
3. Text Range (alphabetical)
SELECT * FROM productsWHERE product_name BETWEEN 'A' AND 'M';Selects products with names starting between A and M.
?? Notes
BETWEENis inclusive of the boundary values.Works for numeric, date/time, and text data types.
Be careful with date formats and time parts in datetime values.
Want to see NOT BETWEEN or combine BETWEEN with other conditions?