What Is the SQL BETWEEN Operator?

SQL BETWEEN selects values within an inclusive range. Write BETWEEN lower_bound AND upper_bound in a WHERE clause to filter rows whose column values fall between the two limits, including the limits themselves. Works with numbers, dates, and text when collation is ordered. Use with NOT to exclude a range.

Learning
June 10, 2025
Galaxy Team
Sign up for the latest notes from our team!
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
SQL BETWEEN filters rows whose column values fall inside an inclusive range: WHERE column BETWEEN lower AND upper. Supports numbers, dates, and text. Add NOT to exclude the range.

SQL BETWEEN Operator Explained

SQL BETWEEN filters rows whose column values fall inside an inclusive range. Syntax: WHERE column BETWEEN lower AND upper. Works on numbers, dates, and ordered text.

What Does SQL BETWEEN Do?

SQL BETWEEN selects rows where a columnbetween two boundary values, inclusive. The operator simplifies range checks that would otherwise need multiple 3C= and 3E= comparisons.

How Is BETWEEN Syntax Structured?

Write BETWEEN in a WHERE clause: WHERE column BETWEEN lower_bound AND upper_bound. Place the lower value first, the upper second. Both bounds may be literals, expressions, or parameters.

Is BETWEEN Inclusive or Exclusive?

BETWEEN is inclusive. Rows matching either boundary value are returned. Use greater-than or less-than operators if you need exclusive ranges.

Which Data Types Work with BETWEEN?

BETWEEN works with numeric, date, time, and text data types that have an ordering. For text, database collation determines the sort order.

When Should I Use NOT BETWEEN?

NOT BETWEEN excludes rows that fall inside a range. Combine with other predicates to keep queries clear: WHERE sale_date NOT BETWEEN '2023-01-01' AND '2023-01-31'.

Can BETWEEN Use Expressions or Functions?

Yes. You can use expressions: WHERE price BETWEEN discount_price() AND full_price * 1.1. The database evaluates each expression once per row during filtering.

Does Case Matter for BETWEEN?

SQL keywords are case-insensitive, so BETWEEN, between, and Between behave identically. Column and alias case sensitivity depends on the database.

How Does BETWEEN Compare to Multiple Conditions?

BETWEEN is syntactic sugar for column 3E= lower AND column 3C= upper. Databases convert it internally, so performance is the same, but BETWEEN improves readability.

What Are Real-World Use Cases?

Common uses include date filtering for reports, price ranges in e-commerce, age bands in demographics, and text ranges for alphabetical pagination.

Does BETWEEN Use Indexes?

Yes. If the column is indexed, BETWEEN predicates can use range scans. Ensure the index covers the column to avoid full table scans.

How Do I Combine BETWEEN with AND/OR?

Use parentheses for clarity: WHERE order_date BETWEEN '2023-01-01' AND '2023-03-31' OR amount BETWEEN 100 AND 200. Parentheses prevent logical errors.

What Happens with NULLs?

BETWEEN returns UNKNOWN when the column value is NULL, so the row is excluded unless you explicitly add OR column IS NULL.

Can BETWEEN Work with Subqueries?

Yes. Example: WHERE salary BETWEEN (SELECT MIN(salary) FROM staff) AND (SELECT MAX(salary) FROM staff). Ensure subqueries return single values.

How Do I Parameterize BETWEEN in Applications?

Use two bind parameters for the lower and upper bounds. Example in Python: cursor.execute("SELECT * FROM sales WHERE total BETWEEN %s AND %s", (min_total, max_total)).

What Are Best Practices for BETWEEN?

Always order bounds correctly, use indexes, avoid string dates, and document inclusive behavior. Use explicit casts if comparing mixed types.

How Can I Test BETWEEN Quickly?

Run small sample queries first: SELECT COUNT(*) FROM orders WHERE order_date BETWEEN '2023-01-01' AND '2023-12-31'. Check counts against expectations before pushing to production.

Where Can I Learn More?

Review database docs, practice in Galaxy27s free SQL editor, and experiment with the AI copilot to generate and explain BETWEEN queries.

Key Takeaways on SQL BETWEEN

BETWEEN offers concise, inclusive range filtering for numbers, dates, and ordered text. Use NOT BETWEEN to exclude ranges, remember NULLs are skipped, and leverage indexes for speed.

Frequently Asked Questions (FAQs)

Does BETWEEN work on timestamps with time zones?

Yes. Ensure both bounds share the same time zone for consistent results, or cast them to a common zone.

How do I use BETWEEN with dynamic dates like last 7 days?

Use expressions: WHERE order_date BETWEEN CURRENT_DATE - INTERVAL '7 days' AND CURRENT_DATE. Most databases accept interval arithmetic.

Can I use BETWEEN on text columns for alphabet ranges?

Yes. Example: WHERE last_name BETWEEN 'A' AND 'M' returns names starting with A through M, depending on collation.

Is performance different between BETWEEN and >= AND <=?

No. Databases convert BETWEEN to >= AND <= internally, so execution plans are identical. The benefit is readability.

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Check out our other posts!

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie
Truvideo Logo