SQL BETWEEN Tutorial With Examples

In the previous SQL tutorial we covered how the clause can use to filter data on multiple conditions. In this tutorial, we'll on another way to filter rows on multiple conditions in SQL – the operator!

WHERE with

is a logical operator that selects values within a given range. The values can be numbers, text, or dates. It has to be paired with the operator.


SQL BETWEEN Example

Suppose a journalist for the Olympics 2024 is writing a "Where Are They Know" article on gold medalists from the 2000s and is requesting data from the Olympics team. They could use the following query:


In essence, using is a more concise option compared to using multiple statements. For example, we could have achieved similar results for 2000s Olympic winners in a more long-winded way with :


Clearly, is much more elegant!

Is BETWEEN Inclusive?

In SQL, the operator's range is inclusive. That means both the start and end values of the range are included!

To demonstrate what this means, imagine you were doing sales analytics and needed to find products whose price is above $10,000, but not more than $20,000 (inclusive). Here's what that SQL query would look like using and :


Note that this query's output is DIFFERENT than if we had used !


So, why the difference?

  • means the total price is more than $10,000, but less than or equal to $20,000.
  • is inclusive which means the total price is more than or equal to $10,000, but less than or equal to $20,000.

SQL BETWEEN Practice Exercise

Imagine you were a Data Analyst working at CVS Pharmacy, and were trying to analyze medicine sales.

Use the SQL command to find all data on:

  • medicines which sold between 100,000 units and 105,000 units
  • AND were manufactured by either Biogen, AbbVie, or Eli Lilly

The output should look like this:

manufacturerdrugunits_sold
AbbVieLidocaine Hydrochloride and Epinephri101102
AbbVieHydralazine Hydrochloride104368
BiogenQUETIAPINE FUMARATE103246
Eli LillyAndrogel102027

Hint: this problem requires not just , but also , , and clauses!

If you are struggling, don't be afraid to review the old tutorials!

Another Way To Avoid Multiple ORs

In the next tutorial, we'll cover another way to filter on multiple conditions, while avoiding multiple statements, thanks to the SQL keyword .


Next Lesson

SQL IN πŸ”Ž

Β© 2024 DataLemur, Inc

Career Resources

Free 9-Day Data Interview Crash CourseFree SQL Tutorial for Data AnalyticsUltimate SQL Interview GuideAce the Data Job Hunt Video CourseAce the Data Science InterviewBest Books for Data Analysts