MySQL BETWEEN
The BETWEEN
operator is used to filter records where a column value is within a specified range, inclusive of both the lower and upper bounds.
Examples with Tamil Kings
1. Using BETWEEN with Numbers
SELECT * FROM tamil_kings_auto_increment
WHERE id BETWEEN 1 AND 5;
Code Explanation: This query selects records where the id
is between 1 and 5, inclusive.
2. Using BETWEEN with Dates
SELECT * FROM tamil_kings_dates
WHERE birth_date BETWEEN '0950-01-01' AND '1000-12-31';
Code Explanation: This query selects records where the birth_date
is between January 1, 950 CE, and December 31, 1000 CE.
Best Practices
- Use
BETWEEN
for filtering within a specific range, especially for date and numeric data. - Ensure the data types are compatible with the values used in the
BETWEEN
clause.
Key Takeaways
- The
BETWEEN
operator filters records within a range, inclusive of the endpoints. - It is useful for date and numeric comparisons.