In SQL predicates, the BETWEEN operator is used for which type of comparison when filtering rows in a WHERE clause?

Difficulty: Easy

Correct Answer: for ranges.

Explanation:


Introduction / Context:
When filtering data by a lower and upper bound, SQL provides a concise predicate to express inclusive range checks. Correct use makes queries more readable and reduces errors compared with chained comparisons.



Given Data / Assumptions:

  • You need to select values within a start and end boundary.
  • The comparison can be numeric, date/time, or even text (collation dependent).
  • Inclusivity of endpoints matters.


Concept / Approach:
BETWEEN is an inclusive range predicate equivalent to: column >= lower AND column <= upper. It is commonly used for dates (for example, order_date BETWEEN '2025-01-01' AND '2025-01-31') or numbers. It does not limit selected columns (that is SELECT list control) and is unrelated to wildcards or joins.



Step-by-Step Solution:

Identify lower and upper bounds for the filter.Apply WHERE column BETWEEN low AND high for inclusive ranges.Confirm that rows equal to low or high are included.


Verification / Alternative check:
Compare results of WHERE x BETWEEN 10 AND 20 with WHERE x >= 10 AND x <= 20; they are equivalent.



Why Other Options Are Wrong:

  • Limit columns displayed: Use the SELECT list, not BETWEEN.
  • Wildcard matching: Use LIKE with % or _ for patterns.
  • None of the above: Incorrect because BETWEEN exists for ranges.
  • Joining tables: Use JOIN syntax and ON conditions.


Common Pitfalls:
Using BETWEEN for datetime boundaries without considering time components; prefer half-open intervals or cast/normalize to avoid off-by-one-day errors.



Final Answer:
for ranges.

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion