How do you use the BETWEEN operator?
The BETWEEN
operator in SQL is used to filter the result set within a specific range. It checks whether a value is within a given lower and upper bound (inclusive of both ends).
🔹 Syntax:
column_name BETWEEN lower_bound AND upper_bound
This is equivalent to:
column_name >= lower_bound AND column_name <= upper_bound
🔹 Example 1: Numeric Range
SELECT * FROM employees
WHERE salary BETWEEN 30000 AND 50000;
✅ This query will return all employees whose salary is between 30,000 and 50,000, inclusive.
🔹 Example 2: Date Range
SELECT * FROM orders
WHERE order_date BETWEEN '2024-01-01' AND '2024-12-31';
✅ This fetches all orders placed in the year 2024.
🔹 Example 3: Text Range (Alphabetical)
SELECT * FROM products
WHERE product_name BETWEEN 'A' AND 'M';
✅ This returns all products where the name starts with a letter between A and M.
🔸 Notes:
Let me know if you want to try it with real-world data or combine it with JOIN
, ORDER BY
, etc.