SQL BETWEEN operator
Here is a clear and practical guide to the SQL BETWEEN operator, with examples for numbers, dates, text, and best-practice notes.
✅ What the BETWEEN Operator Does
BETWEEN is used to filter values within a range, including the lower and upper bounds.
Basic syntax:
This is equivalent to:
📌 Numeric Examples
1. Find prices between 10 and 50
2. Find scores outside a range
📅 Date Examples
1. Find orders created between two dates
2. Safer version for end-of-day timestamp
If created_at includes timestamps:
(Recommended for PostgreSQL/MySQL.)
🔤 Text Examples (lexicographical order)
BETWEEN can compare text alphabetically.
This returns last names starting with A through M (inclusive).
🧠 Important Behavior Notes
✔ BETWEEN is inclusive (>= and <=)
✔ Works on numbers, dates, and text
✔ Avoid using it on DATETIME unless careful with upper bound
✔ Better readability than writing two comparisons
