Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
A frequent query pattern is filtering values that fall within a specified range: finding products priced between ₹500 and ₹2,000, filtering transactions that occurred between two dates, or identifying students scoring between 75% and 90%. In SQL, this is expressed cleanly using the BETWEEN operator.
BETWEEN OperatorBETWEEN is Strictly Inclusive!In standard SQL and MySQL, the BETWEEN operator is always inclusive:
Both boundary endpoints (100 and 500) are included in the results!
BETWEEN with Numbers and DatesDATETIME Trap with BETWEEN!When filtering columns of type DATETIME or TIMESTAMP, using BETWEEN can cause subtle data omission bugs:
Because the literal '2026-03-31' is implicitly converted by MySQL to '2026-03-31 00:00:00' (midnight at the very beginning of the day)! Any order placed after midnight on March 31 is greater than the upper boundary and gets excluded!
Always use half-open intervals with >= and <:
NOT BETWEENTo retrieve rows that fall outside a given range:
WHERE price BETWEEN 500 AND 100 will return 0 rows! In SQL, BETWEEN val1 AND val2 is defined as col >= val1 AND col <= val2. If val1 > val2, the condition is mathematically impossible to satisfy.BETWEEN on text strings (e.g., WHERE last_name BETWEEN 'A' AND 'D'), but be careful: a name like 'Dhoni' is alphabetically greater than 'D' and will be excluded!BETWEEN operator in SQL inclusive or exclusive of its boundary values?SELECT * FROM products WHERE price BETWEEN 1000 AND 500; return?DATETIME column with BETWEEN '2026-01-01' AND '2026-01-31' fail to return records placed in the afternoon of January 31?WHERE score NOT BETWEEN 50 AND 100 logically equivalent to?BETWEEN be used to filter character strings alphabetically in MySQL?Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Logical Operators: AND, OR, NOT | Membership Filtering with IN Operator |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.