SQL BETWEEN operator

SQL Tutorial

Here is a clear and practical guide to the SQL BETWEEN operator, with examples for numbers, dates, text, and best-practice notes.


What the SQL 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

 Real-World Use Cases

Get all employees with salaries in a range

Get log entries from the last 24 hours

Filter rows by alphabetical range


 

You may also like...