The IN keyword
Part of the Fundamentals section of Coddy's SQL journey — lesson 18 of 72.
When we need to find rows where a column matches any one of several possible values, we can write it using multiple OR conditions. For example the following query is very long:
SELECT *
FROM table1
WHERE col1 = 'a' OR col1 = 'b' OR col1 = 'c' OR col1 = 'd' OR ...We can simplify it by using the IN keyword like this:
SELECT *
FROM table1
WHERE col1 IN ('a', 'b', 'c', 'd', 'e', 'f')This shorter version does exactly the same thing: it returns rows where col1 equals any of the values listed in the parentheses.
Challenge
EasyAvailable tables and columns:
<strong>countries</strong>:<strong>location_x</strong>,<strong>location_y</strong>,<strong>country</strong>
Return all the records from the following countries:
Oman, Nicaragua, Bhutan, Senegal, Belarus
Cheat sheet
Use IN to check if a column matches any value from a list:
SELECT *
FROM table1
WHERE col1 IN ('a', 'b', 'c', 'd', 'e', 'f')This is equivalent to multiple OR conditions but much shorter.
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