In the last tutorial, we learned how to filter on a range of conditions using the keyword . In this tutorial, we'll talk about another way to do this for categorical data using the SQL operator .
The operator allows us to specify multiple values in a single line's clause, instead of the more tedious approach of having to use multiple conditions to filter for multiple values.
Here's the syntax:
This command checks if a particular column value matches anything in the list of values you specify.
To see how useful is, imagine you were a Data Analyst examining medicines sold at CVS Pharmacy. Here's a small sample of the data you have access too:
drug | manufacturer | units_sold |
---|---|---|
Acyclovir | Biogen | 89514 |
Hydrochlorothiazide | Roche | 97765 |
LBel | Eli Lilly | 126866 |
Topcare Tussin | Roche | 51707 |
Losartan Potassium | Biogen | 61467 |
Motrin | Johnson & Johnson | 104637 |
Wal-Zan | Biogen | 128494 |
ENALAPRIL MALEATE | Bayer | 126265 |
Suppose you wanted to focus your analysis just on the drugs sold by Biogen, Bayer, and Eli Lilly.
You could use the operator to check if the manufacturer name is your list of values:
This is much more concise than writing out:
Try practicing on this real-world pharmacy analytics dataset. Write a SQL query to find all medicines which:
Output the manufacturer name, drug name, and the # of units sold. for all the medicines which match that criteria. The output should look something like this:
manufacturer | drug | units_sold |
---|---|---|
Roche | Topcare Tussin | 51707 |
AstraZeneca | Armour Thyroid | 47310 |
Hint: Besides the operator, don't forget about the operator either!
Remember how tiresome it was to list multiple times, but then saved the day, allowing us to easily filter based on if multiple values?
What if you could filter based on if your value matched some arbitrary PATTERN, instead of just a fixed list of values?
For example, what if we wanted to find all the 5-letter words in the dictionary that end in "er"?
It's impractical to use here because... we can't specify the list of matching words.
To solve this problem, head over to the next lesson on which allows us to filter strings based on patterns!
Next Lesson
SQL LIKE β€οΈ