MySQL WHERE Clause: Using the BETWEEN Clause
Quick Answer
The MySQL BETWEEN clause allows you to filter query results by selecting values within a specified range, inclusive of the boundary values. It is commonly used in the WHERE clause to simplify range conditions for numbers, dates, and text.
Learning Objectives
- Understand the purpose and syntax of the MySQL BETWEEN clause.
- Learn how to use BETWEEN with numeric, date, and text data types.
- Identify when to use BETWEEN versus other comparison operators.
Introduction
The BETWEEN clause in MySQL is a powerful tool for filtering data within a specific range.
It is often used in the WHERE clause to make queries more readable and concise when checking if a value falls between two boundaries.
BETWEEN simplifies range conditions in SQL queries.
Understanding the BETWEEN Clause
The BETWEEN clause allows you to specify a range to filter query results. It checks if a value lies between two given values, including the boundary values themselves.
The general syntax is: column_name BETWEEN value1 AND value2.
- BETWEEN is inclusive of both value1 and value2.
- It can be used with numeric, date, and string data types.
- BETWEEN is equivalent to using >= value1 AND <= value2.
Syntax of BETWEEN Clause
Here is the basic syntax of the BETWEEN clause in a WHERE condition:
- SELECT column_list FROM table_name
- WHERE column_name BETWEEN value1 AND value2;
Using BETWEEN with Different Data Types
BETWEEN works with various data types, including numbers, dates, and strings. Let's explore examples for each.
Numeric Range Example
To select rows where a numeric column falls between two values, use BETWEEN as follows:
Date Range Example
BETWEEN is very useful for filtering rows within a date range.
String Range Example
BETWEEN can also be used with strings, filtering based on alphabetical order.
Best Practices and Common Mistakes
Using BETWEEN correctly can improve query readability and performance, but some pitfalls should be avoided.
- Always ensure the lower value comes first and the higher value second.
- Remember BETWEEN is inclusive; use > and < if exclusivity is needed.
- Be cautious with NULL values as BETWEEN returns false if the column is NULL.
- Avoid using BETWEEN with incompatible data types to prevent unexpected results.
Practical Example
This query selects employees whose salary is between 30,000 and 60,000 inclusive.
This query retrieves orders placed in January 2023.
This query selects products with names starting from A through M alphabetically.
Examples
SELECT * FROM employees WHERE salary BETWEEN 30000 AND 60000;This query selects employees whose salary is between 30,000 and 60,000 inclusive.
SELECT * FROM orders WHERE order_date BETWEEN '2023-01-01' AND '2023-01-31';This query retrieves orders placed in January 2023.
SELECT * FROM products WHERE product_name BETWEEN 'A' AND 'M';This query selects products with names starting from A through M alphabetically.
Best Practices
- Use BETWEEN for clear and concise range filtering.
- Verify data types to avoid implicit conversions.
- Use explicit >= and <= operators if you need to exclude boundary values.
- Test queries with sample data to confirm expected results.
Common Mistakes
- Using BETWEEN with the higher value first (e.g., BETWEEN 100 AND 50).
- Assuming BETWEEN excludes boundary values.
- Ignoring NULL values which cause BETWEEN to return false.
- Using BETWEEN with incompatible data types without casting.
Hands-on Exercise
Filter Employees by Age Range
Write a query to select employees aged between 25 and 35 using the BETWEEN clause.
Expected output: A list of employees with ages from 25 to 35 inclusive.
Hint: Use the BETWEEN clause in the WHERE condition on the age column.
Retrieve Orders in a Date Range
Write a query to find all orders placed between '2023-03-01' and '2023-03-15'.
Expected output: Orders with order_date between March 1 and March 15, 2023.
Hint: Use BETWEEN with date literals in the WHERE clause.
Interview Questions
What does the BETWEEN clause do in MySQL?
InterviewThe BETWEEN clause filters query results to include rows where a column's value falls within a specified inclusive range.
Is the BETWEEN clause inclusive or exclusive?
InterviewBETWEEN is inclusive, meaning it includes the boundary values specified in the range.
Can BETWEEN be used with string values?
InterviewYes, BETWEEN can be used with strings, filtering based on alphabetical order.
MCQ Quiz
1. What is the best first step when learning BETWEEN Clause?
A. Understand the purpose and basic idea
B. Skip directly to advanced implementation
C. Ignore examples and practice
D. Memorize terms without context
Correct answer: A
Starting with the purpose and basic idea makes later examples and practice easier to understand.
2. Which activity helps reinforce BETWEEN Clause?
A. Reading once without practice
B. Building or writing a small practical example
C. Avoiding review questions
D. Skipping the summary
Correct answer: B
A small practical example helps connect the topic to real usage.
3. Which statement is most accurate about this topic?
A. The MySQL BETWEEN clause allows you to filter query results by selecting values within a specified range, inclusive of the boundary values.
B. BETWEEN Clause never needs examples
C. BETWEEN Clause is unrelated to practical work
D. BETWEEN Clause should be learned without checking results
Correct answer: A
The correct option is based on the available topic explanation.
Key Takeaways
- BETWEEN filters results inclusively between two values.
- It works with numbers, dates, and strings in MySQL.
- BETWEEN simplifies queries that require range filtering.
- Be cautious with NULL values and data types when using BETWEEN.
- The MySQL BETWEEN clause allows you to filter query results by selecting values within a specified range, inclusive of the boundary values.
Summary
The MySQL BETWEEN clause is a convenient way to filter data within a range, including the boundary values.
It works with numbers, dates, and strings, making it versatile for many query scenarios.
Using BETWEEN improves query readability and can simplify complex conditional statements.
Frequently Asked Questions
Does BETWEEN include the boundary values?
Yes, BETWEEN includes both the lower and upper boundary values in the result set.
Can BETWEEN be used with NULL values?
No, if the column value is NULL, BETWEEN will not return true for that row.
What is the difference between BETWEEN and using >= and <= operators?
BETWEEN is functionally equivalent to using >= and <= but provides a more concise and readable syntax.





