The BETWEEN keyword
Part of the Fundamentals section of Coddy's SQL journey — lesson 19 of 72.
As of now, we learned to use bigger > and smaller < to demand a range for a field. But there is another way.
Instead of writing:
WHERE col1 >= 5 AND col1 <= 10We can write:
WHERE col1 BETWEEN 5 AND 10The BETWEEN operator is inclusive, meaning it includes the boundary values (in this case, 5 and 10) in the results. This makes your SQL queries cleaner and more readable, especially when dealing with date ranges or numerical intervals.
Challenge
EasyAvailable tables and columns:
<strong>data</strong>:<strong>value</strong>
Fetch all of the records where the value column is between 7 and 13
Cheat sheet
The BETWEEN operator provides a cleaner way to specify ranges instead of using >= and <=:
WHERE col1 BETWEEN 5 AND 10This is equivalent to:
WHERE col1 >= 5 AND col1 <= 10The BETWEEN operator is inclusive, meaning it includes the boundary values in the results.
Try it yourself
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4More Keywords
The IN keywordThe BETWEEN keywordThe LIKE keywordThe AS keywordRecap - Cellphone Models2Conditions
Conditions BasicsThe AND keywordThe OR keywordThe NOT keywordMultiple Conditions CombinedParenthesisBooleans5Arithmetic Operations
Mathematical OperatorsMathematical ColumnsThe Modulo OperationThe ROUND() Function3Specific Return Format
Null valuesSort Results Part 1Sort Results Part 2Recap - Cyber Security FirmLimit number of recordsRecap - Vehicle Factory6Intro Challenges
Recap - Parliamentary ElectionRecap - Police Criminal ArrestRecap - Bar Beverage ContainerRecap - Engineer new columns