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!
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.
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!
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?
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:
The output should look like this:
manufacturer | drug | units_sold |
---|---|---|
AbbVie | Lidocaine Hydrochloride and Epinephri | 101102 |
AbbVie | Hydralazine Hydrochloride | 104368 |
Biogen | QUETIAPINE FUMARATE | 103246 |
Eli Lilly | Androgel | 102027 |
Hint: this problem requires not just , but also , , and clauses!
If you are struggling, don't be afraid to review the old tutorials!
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 π