Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

BETWEEN in SQL: Syntax, Examples & Common Pitfalls

· 5 min read · Home Tech

In this article
  1. What Exactly Does BETWEEN Do?
  2. BETWEEN with Numbers: Simple Examples
  3. BETWEEN with Dates: The Hidden Time Trap
  4. BETWEEN with Text: Collation Matters
  5. Performance and Indexes
  6. BETWEEN vs. IN vs. Comparison Operators
  7. Common Mistakes and How to Avoid Them
  8. Alternatives in Different SQL Dialects
  9. Frequently Asked Questions

In SQL, BETWEEN is a conditional operator used to filter results within a specified range, inclusive of both boundary values. For example, WHERE age BETWEEN 18 AND 30 returns rows where age is 18, 30, or any value in between.

What Exactly Does BETWEEN Do?

BETWEEN is shorthand for two comparison conditions combined with AND. The expression column BETWEEN low AND high is equivalent to column >= low AND column <= high. It works with numbers, text, and dates, but the way it handles each type has important nuances.

  • Numbers: Straightforward inclusive range check.
  • Text: Uses lexicographic (dictionary) order, so 'b' BETWEEN 'a' AND 'c' is true, but 'B' BETWEEN 'a' AND 'c' may be false depending on the database's collation settings.
  • Dates: Compares date values including the time component if present. A common mistake is using BETWEEN '2024-01-01' AND '2024-12-31' and missing rows on Dec 31 after midnight because the actual timestamp is '2024-12-31 14:30:00', which is still within the range, but if the column is a timestamp, the boundary is treated as '2024-12-31 00:00:00', so rows later that day are excluded.

BETWEEN with Numbers: Simple Examples

Consider a table products with a price column. To find products costing between $10 and $20:

SELECT * FROM products WHERE price BETWEEN 10 AND 20;

This includes products priced exactly $10 and exactly $20. If you want to exclude the boundaries, you must use explicit comparisons: price > 10 AND price < 20.

You can also use NOT BETWEEN to exclude a range: WHERE price NOT BETWEEN 10 AND 20 returns all products outside that range, again inclusive of the boundaries when negated.

BETWEEN with Dates: The Hidden Time Trap

Dates are the most common source of confusion. If your column is a DATE type (no time), BETWEEN '2024-01-01' AND '2024-12-31' works as expected. But if the column is a TIMESTAMP or DATETIME, the upper bound is interpreted as midnight at the start of that day.

To correctly include all of December 31, use an exclusive upper bound:

SELECT * FROM orders WHERE order_date >= '2024-01-01' AND order_date < '2025-01-01';

Alternatively, you can use BETWEEN '2024-01-01' AND '2024-12-31 23:59:59', but that still misses rows with microseconds. The < approach is safer and is the recommended pattern in most SQL style guides.

BETWEEN with Text: Collation Matters

Text comparisons use the database's collation, which defines the sort order and case sensitivity. In a case-insensitive collation (common in MySQL with utf8_general_ci), 'B' BETWEEN 'a' AND 'c' is true. In a case-sensitive collation (like PostgreSQL's default), uppercase letters sort before lowercase, so 'B' is less than 'a' and the condition is false.

This can lead to unexpected results when filtering names or codes. If you need a case-insensitive range check, use LOWER(column) BETWEEN 'a' AND 'c' or set the appropriate collation on the column.

Performance and Indexes

When BETWEEN is used on an indexed column, the database can typically use the index for a range scan, which is efficient. However, if you wrap the column in a function (e.g., LOWER(column) BETWEEN ...), the index is usually ignored unless you create a functional index.

For large tables, the inclusive nature of BETWEEN can sometimes cause the optimizer to choose a less efficient plan than two separate >= and < conditions, but in practice the difference is negligible. The main performance concern is ensuring the column is indexed.

BETWEEN vs. IN vs. Comparison Operators

BETWEEN is for continuous ranges. IN is for discrete lists. For example, WHERE status IN ('active', 'pending', 'review') checks against a fixed set. They are not interchangeable.

  • Use BETWEEN when you want everything in a numeric or date interval.
  • Use IN when you have a short list of specific values.
  • Use >, <, >=, <= when you need to exclude one boundary or combine with other conditions in complex ways.

Some databases also support BETWEEN SYMMETRIC (PostgreSQL) which automatically swaps the boundaries if the lower value is greater than the upper, but this is not standard SQL.

Common Mistakes and How to Avoid Them

  1. Assuming exclusivity: Remember BETWEEN is always inclusive. If you need exclusive, use explicit comparisons.
  2. Date boundary errors: Always consider the time component. Use < 'next_day' instead of <= 'current_day 23:59:59'.
  3. Text collation surprises: Test with your actual data and collation.
  4. NULL values: If the column contains NULL, the condition evaluates to UNKNOWN and the row is excluded. This is correct behavior but can be confusing if you expect NULLs to be included.
  5. Reversing arguments: BETWEEN 20 AND 10 returns no rows in most databases (except PostgreSQL's BETWEEN SYMMETRIC). Always put the lower value first.

Alternatives in Different SQL Dialects

While BETWEEN is standard SQL, some databases offer extensions:

  • PostgreSQL: BETWEEN SYMMETRIC for unordered ranges.
  • SQL Server: No direct alternative, but you can use >= and < as described.
  • MySQL: Supports BETWEEN with dates and times, but beware of the date-only string interpretation.
  • Oracle: Same as standard; also supports BETWEEN on dates with time.

In all cases, the explicit comparison form is more portable and often clearer for complex conditions.

Frequently Asked Questions

Does BETWEEN include the endpoints?

Yes, BETWEEN is inclusive of both the lower and upper values. WHERE x BETWEEN 1 AND 3 includes 1, 2, and 3.

How do I use BETWEEN with dates in SQL?

Use WHERE date_col BETWEEN '2024-01-01' AND '2024-12-31' for date-only columns. For timestamp columns, use date_col >= '2024-01-01' AND date_col < '2025-01-01' to include all of December 31.

Can I use BETWEEN with strings?

Yes, but the result depends on the collation. It compares strings lexicographically. For case-insensitive behavior, ensure your collation is case-insensitive or use LOWER().

What is the difference between BETWEEN and IN?

BETWEEN checks if a value falls within a continuous range. IN checks if a value matches any value in a discrete list. Use IN for specific values, BETWEEN for intervals.

Why does my BETWEEN query return no rows for dates?

Most likely because your column includes a time component and the upper bound is interpreted as midnight. Use an exclusive upper bound (< 'next_day') or add the time to the boundary.

Is BETWEEN efficient on large tables?

Yes, if the column is indexed. The database can use a range scan. Avoid wrapping the column in functions to maintain index usage.